DateUtil

下列操作Date时间使用的是JDK8的LocalDate,JDK8以下则不方便使用。

import java.time.LocalDate;

import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class DateUtil {

private static String ymd = "yyyyMMdd";

private DateUtil(){
throw new IllegalStateException("DateUtil class");
}
// 获取上个月日期
public static String getLastMonth(){
LocalDate localDate = LocalDate.now();
localDate = localDate.minusMonths(1);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMM");
return dateTimeFormatter.format(localDate);
}

//获取指定日期,自定义上几月份日期
public static String getLastMonth(int minus,String months){
LocalDate localDate = LocalDate.parse(months+"01",DateTimeFormatter.ofPattern(ymd));
localDate = localDate.minusMonths(minus);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMM");
return dateTimeFormatter.format(localDate);
}
// 获取指定月份的最后一天
public static String getLastDayOfMonth(String lastMonth){
LocalDate localDate = LocalDate.parse(lastMonth+"01", DateTimeFormatter.ofPattern(ymd));
LocalDate last = localDate.with(TemporalAdjusters.lastDayOfMonth());
return last.format(DateTimeFormatter.ofPattern(ymd));
}
// 获取指定月份的第一天
public static String getFirstDayOfMonth(String lastMonth){
LocalDate localDate = LocalDate.parse(lastMonth+"01", DateTimeFormatter.ofPattern(ymd));
LocalDate last = localDate.with(TemporalAdjusters.firstDayOfMonth());
return last.format(DateTimeFormatter.ofPattern(ymd));
}

// 获取昨天日期
public static String getYesterday(){
LocalDate localDate = LocalDate.now();
LocalDate plusDays = localDate.plusDays(-1);
LocalDate last = plusDays.with(TemporalAdjusters.lastDayOfMonth());
return last.format(DateTimeFormatter.ofPattern(ymd));
}

// 获取今日日期
public static String getToday(){
LocalDate localDate = LocalDate.now();
return localDate.format(DateTimeFormatter.ofPattern(ymd));
}

// 获取明日日期
public static String getTomorrow(){
LocalDate localDate = LocalDate.now();
LocalDate plusDays = localDate.plusDays(1);
return plusDays.format(DateTimeFormatter.ofPattern(ymd));
}
// 获取前天日期
public static String getBeforeYesterday(){
LocalDate localDate = LocalDate.now();
LocalDate plusDays = localDate.plusDays(-2);
LocalDate last = plusDays.with(TemporalAdjusters.lastDayOfMonth());
return last.format(DateTimeFormatter.ofPattern(ymd));
}
// 获取上年12月份
public static String getLastYearLastMonth(String month){
String lastYear = month.substring(0, 4);
int year = Integer.parseInt(lastYear);
lastYear = String.valueOf(year - 1);
return lastYear+"12";
}
// 获取今年份
public static String getToYear(){
LocalDate localDate = LocalDate.now();
return localDate.format(DateTimeFormatter.ofPattern("yyyy"));
}
//获取组装季度
public static String getJd(String month){
String jd = "";
if("03".equals(month.substring(4,6))){
jd = month.substring(0,4) + "-1";
}
if("06".equals(month.substring(4,6))){
jd = month.substring(0,4) + "-2";
}
if("09".equals(month.substring(4,6))){
jd = month.substring(0,4) + "-3";
}
if("12".equals(month.substring(4,6))){
jd = month.substring(0,4) + "-4";
}
return jd;
}

}

posted @ 2022-03-10 23:18  comliang  阅读(89)  评论(0)    收藏  举报