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 @   comliang  阅读(76)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示