public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = format.parse("2019-03-15");
Calendar c = Calendar.getInstance();
//设置为指定日期
c.setTime(date);
//指定日期月份减去一
c.add(Calendar.MONTH, -1);
//指定日期月份减去一后的 最大天数
c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
//获取最终的时间
Date lastDateOfPrevMonth = c.getTime();
System.out.println("上月最后一天:" + format.format(lastDateOfPrevMonth));
c.set(Calendar.DATE, 1);
//获取最初的时间
Date firstDateOfPrevMonth = c.getTime();
System.out.println("上月最后第一天:" + format.format(firstDateOfPrevMonth));
} catch (ParseException e) {
System.out.println(e.getMessage());
}
}
//输出结果:
//上月最后一天:2019-02-28
//上月最后第一天:2019-02-01
- 转载请注明来源:获取上个月最后一天、第一天
- 本文永久链接地址:http://icehill.cn/post/single/info/247.html