在普通的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
获得相同结果的干净优雅方式是什么?
答案 0 :(得分:5)
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;
}
}
答案 1 :(得分:5)
你可以尝试这样的事情:
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
答案 2 :(得分:3)
这是一个相当古老的帖子,但这个答案可能对某人有帮助。使用替换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));
}
答案 3 :(得分:2)
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));
}
}
答案 4 :(得分:2)
//以下方法将给出给定月份和年份的最后一个星期日
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);
答案 5 :(得分:2)
java.time
API 的解决方案获取指定年月最后一个星期日的本地日期:
为了使用 Java 的 modern date-time API 实现这一点,您可以将 TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY)
作为参数传递给指定年份和月份的任何 LocalDate
。
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(getLastSundayOfMonth(2020, 12));
}
public static LocalDate getLastSundayOfMonth(int year, int month) {
return YearMonth.of(year, month)
.atEndOfMonth() //Returns a LocalDate at the end of the month
.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));
}
}
输出:
2020-12-27
我已使用 YearMonth#atEndOfMonth
获得 LocalDate
。除此之外,还可以使用 YearMonth#atDay
。
获取指定年份和月份的第 n 个星期几(例如 2nd Thu)的本地日期:
您可以将 TemporalAdjusters#dayOfWeekInMonth
作为参数传递给指定年份和月份的任何 LocalDate
。
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(localDateOnNthDayOfWeekOfMonth(DayOfWeek.THURSDAY, 2, 2020, 12));
}
public static LocalDate localDateOnNthDayOfWeekOfMonth(DayOfWeek dayOfWeek, int ordinal, int year, int month) {
return YearMonth.of(year, month)
.atEndOfMonth() // Returns a LocalDate at the end of the month
.with(TemporalAdjusters.dayOfWeekInMonth(ordinal, dayOfWeek));
}
}
输出:
2020-12-10
从 Trail: Date Time 了解 Java-8 日期时间 API。
Java 6 或 7 用户注意事项:
出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport,它将大部分 java.time 功能向后移植到 Java 6 和 7。如果您正在为 Android 项目工作并且您的 Android API 级别仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaring 和 How to use ThreeTenABP in Android Project。
答案 6 :(得分:1)
使用Joda
我们说我们有个约会日期:
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日