/**
* @author fyp
*/
public class DateUtils {
public static SimpleDateFormat sdf() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
public static DateTimeFormatter dtf(){
return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
}
/**
* 获取当天的开始时间
*/
public static String getDayBegin() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return sdf().format(cal.getTime());
}
/**
* 获取当天的结束时间
*/
public static String getDayEnd() {
LocalDateTime localDateTime = LocalDateTime.now();
// LocalDateTime 转换 Date
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return sdf().format(cal.getTime());
}
/**
* 获取昨天的开始时间
* @return
*/
public static LocalDateTime getYesterdayBegin(LocalDateTime localDateTime) {
// LocalDateTime 转换 Date
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.DAY_OF_MONTH, -1);
return LocalDateTime.parse(sdf().format(cal.getTime()),dtf());
}
/**
* 获取昨天的结束时间
* @return
*/
public static LocalDateTime getYesterdayEnd(LocalDateTime localDateTime) {
// LocalDateTime 转换 Date
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.add(Calendar.DAY_OF_MONTH, -1);
return LocalDateTime.parse(sdf().format(cal.getTime()),dtf());
}
/**
* 获取n天前的开始时间
* @return
*/
public static LocalDateTime getNDayBegin(LocalDateTime localDateTime,int n) {
// LocalDateTime 转换 Date
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.DAY_OF_MONTH, -n);
return LocalDateTime.parse(sdf().format(cal.getTime()),dtf());
}
/**
* 获取n天后的结束时间
* @return
*/
public static LocalDateTime getNDayEnd(LocalDateTime localDateTime,int n) {
// LocalDateTime 转换 Date
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.add(Calendar.DAY_OF_MONTH, -n);
return LocalDateTime.parse(sdf().format(cal.getTime()),dtf());
}
/**
* 根据当前日期获得最近n周的日期区间(不包含本周)
*/
public static String getFromToDate(SimpleDateFormat sdf, Date date, int n, int option, int k) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int offset = 0 == option ? 1 - dayOfWeek : 7 - dayOfWeek;
int amount = 0 == option ? offset - (n - 1 + k) * 7 : offset - k * 7;
calendar.add(Calendar.DATE, amount);
return sdf.format(calendar.getTime());
}
/**
* 获取上周的开始时间
*/
public static LocalDateTime getLastWeekBegin(LocalDateTime localDateTime) {
// LocalDateTime 转换 Date
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
Calendar c = Calendar.getInstance();
c.setTime(date);
//上周日期
SimpleDateFormat sdf = sdf();
String beginDate = getFromToDate(sdf, new Date(), 1, 0, 1);
try {
c.setTime(sdf.parse(beginDate));
}catch (Exception e){
e.printStackTrace();
}
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return LocalDateTime.parse(sdf.format(c.getTime()),dtf());
}
/**
* 获取上周的结束时间
*/
public static LocalDateTime getLastWeekEnd(LocalDateTime localDateTime) {
// LocalDateTime 转换 Date
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
Calendar c = Calendar.getInstance();
c.setTime(date);
//上周日期
SimpleDateFormat sdf = sdf();
String endDate = getFromToDate(sdf, new Date(), 1, 1, 1);
try {
c.setTime(sdf.parse(endDate));
}catch (Exception e){
e.printStackTrace();
}
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
return LocalDateTime.parse(sdf.format(c.getTime()),dtf());
}
/**
* 获得上月第一天0点时间
*/
public static LocalDateTime getLastMonthBegin(LocalDateTime localDateTime) {
// LocalDateTime 转换 Date
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
//上月日期
Calendar c=Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, -1);
SimpleDateFormat sdf = sdf();
String gtimelast = sdf.format(c.getTime()); //上月
int lastMonthMaxDay=c.getActualMaximum(Calendar.DAY_OF_MONTH);
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);
//按格式输出
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-01 00:00:00");
String gtime = sdf2.format(c.getTime()); //上月第一天
return LocalDateTime.parse(gtime,dtf());
}
/**
* 获得上月最后一天24点时间
*/
public static LocalDateTime getLastMonthEnd(LocalDateTime localDateTime) {
// LocalDateTime 转换 Date
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
//上月日期
Calendar c=Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, -1);
SimpleDateFormat sdf = sdf();
String gtimelast = sdf.format(c.getTime()); //上月
int lastMonthMaxDay=c.getActualMaximum(Calendar.DAY_OF_MONTH);
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);
//按格式输出
String gtime = sdf.format(c.getTime()); //上月最后一天
return LocalDateTime.parse(gtime,dtf());
}
}
LocalDataTime时间处理
最新推荐文章于 2024-08-15 14:47:41 发布