public static void main(String [] args){
//当年
String year = Year.now().toString();
//去年
String jyear = Year.now().plusYears(-1).toString();
//上个月
String a = DateUtil.lastMonth();
//上个月第一天
String b = DateUtil.lastMonthFirstDay();
//上个月最后一天
String c = DateUtil.lastMonthLastDay();
//上个年第一天
String d = DateUtil.yearAndOneTime(new Date());
//上个年最后一天
String e = DateUtil.yearAndLastTime(new Date());
String nowMonth = DateUtil.lastMonth();
String range = nowMonth.substring(0,4);
//上三月第一天
String startTime = range+"-01-01 00:00:00";
System.out.println(nowMonth);
System.out.println(range);
System.out.println(startTime);
}
/**
* 获取上个月份
* yyyy-MM
*
* @return
*/
public static String lastMonth() {
return YearMonth.now().plusMonths(-1).toString();
}
/**
* 获取上个月份第一天
* yyyy-MM-dd
*
* @return
*/
public static String lastMonthFirstDay() {
LocalDate lastMonthDate = LocalDate.now().plusMonths(-1);
return lastMonthDate.with(TemporalAdjusters.firstDayOfMonth()).toString();
}
/**
* 获取三个月份第一天
* yyyy-MM-dd
*
* @return
*/
public static String lastThreeMonthFirstDay() {
LocalDate lastMonthDate = LocalDate.now().plusMonths(-3);
return lastMonthDate.with(TemporalAdjusters.firstDayOfMonth()).toString();
}
/**
* 获取六个月份第一天
* yyyy-MM-dd
*
* @return
*/
public static String lastSixMonthFirstDay() {
LocalDate lastMonthDate = LocalDate.now().plusMonths(-6);
return lastMonthDate.with(TemporalAdjusters.firstDayOfMonth()).toString();
}
/**
* 获取上个月份最后一天
* yyyy-MM-dd
*
* @return
*/
public static String lastMonthLastDay() {
LocalDate lastMonthDate = LocalDate.now().plusMonths(-1);
return lastMonthDate.with(TemporalAdjusters.lastDayOfMonth()).toString();
}
/**
* 年加一年的第一天
* 格式pattern
*
* @param
* @return
*/
public static String yearAndOneTime(Date fixedTime) {
//时间格式化
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-01-01");
//使用Calendar来实现加减1小时,1天,1个月和1年
Calendar calendar = Calendar.getInstance();
//指定时间加1年
calendar.setTime(fixedTime);
calendar.add(Calendar.YEAR, -1);
System.out.println(String.format("1年之后的时间:%s", dateFormat.format(calendar.getTime())));
return dateFormat.format(calendar.getTime());
}
/**
* 年加一年的最后一天
* 格式pattern
*
* @param
* @return
*/
public static String yearAndLastTime(Date fixedTime) {
//时间格式化
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-12-31");
//使用Calendar来实现加减1小时,1天,1个月和1年
Calendar calendar = Calendar.getInstance();
//指定时间加1年
calendar.setTime(fixedTime);
calendar.add(Calendar.YEAR, -1);
System.out.println(String.format("1年之后的时间:%s", dateFormat.format(calendar.getTime())));
return dateFormat.format(calendar.getTime());
}