如何获得上个月的第一个日期和最后一个日期? (Java)

问题描述:

在今天的日期,我应该得到上个月的第一个日期和最后一个日期。我无法提出任何逻辑。

With today's date, I should get the first date and last date of the previous month. I am unable to come up with any logic.

例如,通过 05/30/2012 ,我应该得到 04 / 01/2012 04/30/2012

For example, on passing 05/30/2012, I should get 04/01/2012 and 04/30/2012.

任何帮助将会很多赞赏谢谢。

Any help will be much appreciated. Thanks.

使用 日历

Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DATE, 1);
Date firstDateOfPreviousMonth = cal.getTime();

cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE)); // changed calendar to cal

Date lastDateOfPreviousMonth = cal.getTime();

查看

  • ideone demo