Joda-Time:获得月的第一个/第二个/最后一个星期日

在普通的Java中,我有这个代码来获取本月的最后一个星期日。

Calendar getNthOfMonth(int n, int day_of_week, int month, int year) { Calendar compareDate = Date(1, month, year); compareDate.set(DAY_OF_WEEK, day_of_week); compareDate.set(DAY_OF_WEEK_IN_MONTH, n); return compareDate; } // Usage Calendar lastSundayOfNovember = getNthOfMonth(-1, SUNDAY, NOVEMBER, 2012) 

使用Joda-Time实现相同结果的干净而优雅的方法是什么?

 public class Time { public static void main(String[] args) { System.out.println(getNthOfMonth(DateTimeConstants.SUNDAY, DateTimeConstants.SEP, 2012)); } public static LocalDate getNthOfMonth(int day_of_week, int month, int year) { LocalDate date = new LocalDate(year, month, 1).dayOfMonth() .withMaximumValue() .dayOfWeek() .setCopy(day_of_week); if(date.getMonthOfYear() != month) { return date.dayOfWeek().addToCopy(-7); } return date; } } 

你可以试试这样的东西:

 public class Foo { public static LocalDate getNthSundayOfMonth(final int n, final int month, final int year) { final LocalDate firstSunday = new LocalDate(year, month, 1).withDayOfWeek(DateTimeConstants.SUNDAY); if (n > 1) { final LocalDate nThSunday = firstSunday.plusWeeks(n - 1); final LocalDate lastDayInMonth = firstSunday.dayOfMonth().withMaximumValue(); if (nThSunday.isAfter(lastDayInMonth)) { throw new IllegalArgumentException("There is no " + n + "th Sunday in this month!"); } return nThSunday; } return firstSunday; } public static void main(final String[] args) { System.out.println(getNthSundayOfMonth(1, DateTimeConstants.SEPTEMBER, 2012)); System.out.println(getNthSundayOfMonth(2, DateTimeConstants.SEPTEMBER, 2012)); System.out.println(getNthSundayOfMonth(3, DateTimeConstants.SEPTEMBER, 2012)); System.out.println(getNthSundayOfMonth(4, DateTimeConstants.SEPTEMBER, 2012)); System.out.println(getNthSundayOfMonth(5, DateTimeConstants.SEPTEMBER, 2012)); } } 

输出:

 2012-09-02 2012-09-09 2012-09-16 2012-09-23 2012-09-30 

这是一个相当古老的post,但这个答案可能对某人有帮助。 使用替换Joda-Time的java.time类。

 private static LocalDate getNthOfMonth(int type, DayOfWeek dayOfWeek, int month, int year){ return LocalDate.now().withMonth(month).withYear(year).with(TemporalAdjusters.dayOfWeekInMonth(type, dayOfWeek)); } 
  static LocalDate getNthOfMonth(int n, int day_of_week, int month, int year) { if (n == -1) { return getNthOfMonth(0, day_of_week, month + 1, year); } final LocalDate compareDate = new LocalDate(year, month, 1); if (compareDate.getDayOfWeek() > day_of_week) { return compareDate.withDayOfWeek(day_of_week).plusDays(7 * n); } else { return compareDate.withDayOfWeek(day_of_week).plusDays(7 * (n - 1)); } } 

//以下方法将给出给定月份和年份的最后一个星期日

 public Date getLastSunday(int month, int year) { Calendar cal = Calendar.getInstance(); cal.set(year, month + 1, 1); int dayOftheWeek = cal.get(Calendar.DAY_OF_WEEK); int val = dayOftheWeek == Calendar.SUNDAY ? -7: -(dayOftheWeek-1); cal.add(Calendar.DAY_OF_MONTH, val); return cal.getTime(); } 

// obj.getLastSunday(Calendar.OCTOBER, 2014);

与乔达

假设我们有一个约会日期:

 DateTime date = new DateTime(2017, 6, 1, 0, 0, 0); 

`首先我们需要知道这个月是31,30,28还是29天:

 int lastofMonth = date.dayOfMonth().getMaximumValue(); 

从这里我们创建一个新的日期:

 DateTime endOfMonth = new DateTime(2017, 6, lastOfMonth, 0, 0, 0); 

我们发现最后一天的哪一天是:

 int whatDayIsLast = endOfMonth.getDayOfWeek(); 

现在我们可以创建一个日期,该日期将是该月的最后一个星期日的日期:

 DateTime lastSunday = new DateTime(2017, 6, lastOfMonth - whatDayIsLast, 0, 0, 0); 

对于我的例子(2017年6月),结果将是:

 DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd"); System.out.println(dtf.print(lastSunday); 

2017年6月25日