如何用Java获取当前日期下一个月的当天日期?
- 内容介绍
- 文章标签
- 相关推荐
本文共计213个文字,预计阅读时间需要1分钟。
java
1.获取当前日期的上一个月
private static String getLastMonthDate(String dateTime) throws ParseException { LocalDate today=LocalDate.parse(dateTime); LocalDate lastMonth=today.minusMonths(1); return lastMonth.toString();}1、当前日期的下一月的当前日期
private static String getLastMonthDate(String dataTime) throws ParseException {
//时间字符串转 LocalDate 类型
LocalDate today = LocalDate.parse(dataTime);
//当前月份+(-1)
today = today.minusMonths(-1);
//LocalDate日期格式是"YYYY-MM-DD",只需要用toString()就可以转化成字符串类型
return today.toString();
}
2、另外一种方式
public static final SimpleDateFormat SHORT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
private static String getLastMonthDate(String repeatDate) {
Calendar calendar = Calendar.getInstance();
try {
if(repeatDate!=null && !"".equals(repeatDate)){
calendar.setTime(SHORT_DATE_FORMAT.parse(repeatDate));
}
} catch (ParseException e) {
e.printStackTrace();
}
calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1,calendar.get(Calendar.MINUTE));
return SHORT_DATE_FORMAT.format(calendar.getTime());
}
本文共计213个文字,预计阅读时间需要1分钟。
java
1.获取当前日期的上一个月
private static String getLastMonthDate(String dateTime) throws ParseException { LocalDate today=LocalDate.parse(dateTime); LocalDate lastMonth=today.minusMonths(1); return lastMonth.toString();}1、当前日期的下一月的当前日期
private static String getLastMonthDate(String dataTime) throws ParseException {
//时间字符串转 LocalDate 类型
LocalDate today = LocalDate.parse(dataTime);
//当前月份+(-1)
today = today.minusMonths(-1);
//LocalDate日期格式是"YYYY-MM-DD",只需要用toString()就可以转化成字符串类型
return today.toString();
}
2、另外一种方式
public static final SimpleDateFormat SHORT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
private static String getLastMonthDate(String repeatDate) {
Calendar calendar = Calendar.getInstance();
try {
if(repeatDate!=null && !"".equals(repeatDate)){
calendar.setTime(SHORT_DATE_FORMAT.parse(repeatDate));
}
} catch (ParseException e) {
e.printStackTrace();
}
calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1,calendar.get(Calendar.MINUTE));
return SHORT_DATE_FORMAT.format(calendar.getTime());
}

