(Java) Date工具类
package com.newpay.utils;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class DateUtil {
/**
* 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
*
* @param nowTime 当前时间
* @param startTime 开始时间
* @param endTime 结束时间
* @return
* @author zyu
*/
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if (nowTime.getTime() == startTime.getTime()
|| nowTime.getTime() == endTime.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
/**
* 获取当月1号的时间戳
*
* @param timeStamp
* @param timeZone
* @return
*/
public static Long getMonthStartTime(Long timeStamp, String timeZone) {
Calendar calendar = Calendar.getInstance();// 获取当前日期
calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
calendar.setTimeInMillis(timeStamp);
calendar.add(Calendar.YEAR, 0);
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis() / 1000;
}
/**
* 获取前M天时间戳
*
* @param days 需要返回前几天
* @return 例如:返回昨天 00:00:00时间戳, days = 1
*/
public static Long getMinusDays(Integer days) {
ZonedDateTime ztime = LocalDate.now().minusDays(days).atStartOfDay(ZoneId.systemDefault());
return ztime.toEpochSecond();
}
/**
* 获取本月1号记录时间戳
*
* @return 例如:2020-01-01 00:00:00
*/
public static int getMonthStartDate() {
return (int) LocalDate.now()
.with(TemporalAdjusters.firstDayOfMonth())
.atStartOfDay()
.atZone(ZoneId.systemDefault())
.toEpochSecond();
}
/**
* 获取本月最后一日
*
* @return 例如:2020-01-31 00:00:00
*/
public static long getMonthEndDate() {
return LocalDate.now()
.atTime(00, 00, 00)
.with(TemporalAdjusters.lastDayOfMonth())
.atZone(ZoneId.systemDefault())
.toEpochSecond();
}
/**
* 获取上个月最后一天
*
* @return
*/
public static long getLastMonthLastDate() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DATE, -1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime().getTime() / 1000;
}
/**
* 获取上个月第一天
*
* @return
*/
public static long getLastMonthFirstDate() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime().getTime() / 1000;
}
/**
* 获取当前时间戳
*
* @return
*/
public static long getNowTime() {
return System.currentTimeMillis() / 1000;
}
/**
* 获取今天凌晨零点零分零秒的时间戳
*
* @return
*/
public static long getTodayZeroTimeTemp() {
// 获取现在的时间毫秒数
long current = System.currentTimeMillis();
// 获取今天的零分零秒的毫秒数
long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
return zero / 1000;
}
/**
* 获取昨天凌晨零点零分零秒的时间戳
*
* @return
*/
public static long getLastDayTimeTemp() {
long lastTimeTwmp = getTodayZeroTimeTemp() - (24 * 60 * 60);
return lastTimeTwmp;
}
/**
* 依据传入字符串 返回 unix 时间戳
*
* @param strTime
* @return
*/
public static long getUnixTime(String strTime) {
String retTime = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date;
try {
date = dateFormat.parse(strTime);
long unixTime = date.getTime();
retTime = String.valueOf(unixTime);
retTime = retTime.substring(0, 10);
} catch (ParseException e) {
}
return Long.parseLong(retTime);
}
/**
* 根据月份获取天数
*
* @param month 年和月 例:(2018-5)
* @return count_day
*/
public static int getCountDayByMonth(String month) {
Calendar cld = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
try {
cld.setTime(sdf.parse(month));
} catch (ParseException e) {
e.printStackTrace();
}
int count_day = cld.getActualMaximum(Calendar.DAY_OF_MONTH);
return count_day;
}
/**
* 返回当前日期时间
*
* @return
*/
public static String getCurrDate() {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String strDateNow = dateFormat.format(now);
return strDateNow;
}
/**
* 返回当前年
*
* @return 2018
*/
public static String getCurrYearDate() {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
String strDateNow = dateFormat.format(now);
return strDateNow;
}
/**
* 返回当前年和月
*
* @return 2018
*/
public static String getCurrMonthDate() {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
String strDateNow = dateFormat.format(now);
return strDateNow;
}
}
以上是 (Java) Date工具类 的全部内容, 来源链接: utcz.com/z/390580.html