问题
In plain Java, I have this code to get the last Sunday of the month.
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)
What is a clean and elegant way to achieve the same result using Joda-Time?
回答1:
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;
}
}
回答2:
You can try something like that:
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));
}
}
Output:
2012-09-02
2012-09-09
2012-09-16
2012-09-23
2012-09-30
回答3:
This is quite an old post but possibly this answer will help someone. Use the java.time classes that replace Joda-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));
}
回答4:
With Joda
Let's say we have a date:
DateTime date = new DateTime(2017, 6, 1, 0, 0, 0);
`First we need to know if the month has 31, 30, 28 or 29 days :
int lastofMonth = date.dayOfMonth().getMaximumValue();
From this we create a new date:
DateTime endOfMonth = new DateTime(2017, 6, lastOfMonth, 0, 0, 0);
The we find out what day of week the last day is:
int whatDayIsLast = endOfMonth.getDayOfWeek();
Now we can create a date, which will be the date of the last Sunday of the month:
DateTime lastSunday = new DateTime(2017, 6, lastOfMonth - whatDayIsLast, 0, 0, 0);
For my example (June 2017), the result will be:
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd");
System.out.println(dtf.print(lastSunday);
2017-06-25
回答5:
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));
}
}
回答6:
// Following method will give last Sunday of the given month and year
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);
来源:https://stackoverflow.com/questions/12369844/joda-time-get-first-second-last-sunday-of-month