时间:如何获得下一个星期五?

我怎样才能得到Joda-Time API的下一个星期五。

今天的LocalDatetoday 。 在我看来,你必须决定在本周五星期五之前或之后。 看到这个方法:

 private LocalDate calcNextFriday(LocalDate d) { LocalDate friday = d.dayOfWeek().setCopy(5); if (d.isBefore(friday)) { return d.dayOfWeek().setCopy(5); } else { return d.plusWeeks(1).dayOfWeek().setCopy(5); } } 

是否有可能做得更短或与一个oneliner?

PS:请不要通知我使用JDK的date/时间的东西。 Joda-Time是一个更好的API。

Java 8引入了更好的java.time包( Tutorial )。

可以更容易阅读:

 if (d.getDayOfWeek() < DateTimeConstants.FRIDAY) { return d.withDayOfWeek(DateTimeConstants.FRIDAY)); } else if (d.getDayOfWeek() == DateTimeConstants.FRIDAY) { // almost useless branch, could be merged with the one above return d; } else { return d.plusWeeks(1).withDayOfWeek(DateTimeConstants.FRIDAY)); } 

或者稍微缩短一点

 private LocalDate calcNextFriday(LocalDate d) { if (d.getDayOfWeek() < DateTimeConstants.FRIDAY) { d = d.withDayOfWeek(DateTimeConstants.FRIDAY)); } else { d = d.plusWeeks(1).withDayOfWeek(DateTimeConstants.FRIDAY)); } return d; // note that there's a possibility original object is returned } 

甚至更短

 private LocalDate calcNextFriday(LocalDate d) { if (d.getDayOfWeek() >= DateTimeConstants.FRIDAY) { d = d.plusWeeks(1); } return d.withDayOfWeek(DateTimeConstants.FRIDAY); } 

PS。 我没有testing实际的代码! 🙂

java.time

使用Java 8及更高版本( 教程 )中内置的java.time框架,您可以使用TemporalAdjusters获取下一个或上一个 星期几 。

 private LocalDate calcNextFriday(LocalDate d) { return d.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)); } 

你的代码在1行

 private LocalDate calcNextFriday3(LocalDate d) { return d.isBefore(d.dayOfWeek().setCopy(5))?d.dayOfWeek().setCopy(5):d.plusWeeks(1).dayOfWeek().setCopy(5); } 

替代方法

 private LocalDate calcNextDay(LocalDate d, int weekday) { return (d.getDayOfWeek() < weekday)?d.withDayOfWeek(weekday):d.plusWeeks(1).withDayOfWeek(weekday); } private LocalDate calcNextFriday2(LocalDate d) { return calcNextDay(d,DateTimeConstants.FRIDAY); } 

有点testing;-)

我只是浪费了30分钟,试图自己弄清楚这一点,但我需要一般地前进。

无论如何,这是我的解决scheme:

 public static DateTime rollForwardWith(ReadableInstant now, AbstractPartial lp) { DateTime dt = lp.toDateTime(now); while (dt.isBefore(now)) { dt = dt.withFieldAdded(lp.getFieldTypes()[0].getRangeDurationType(), 1); } return dt; } 

现在,您只需在一周中的某一天制作“部分”(LocalDate)即可。

 Partial().with(DateTimeFieldType.dayOfWeek(), DateTimeConstants.FRIDAY); 

现在无论最重要的字段是什么,如果当前date在现在(现在)之后,则部分将是+1。

也就是说,如果您在2012年3月进行了部分更改,则会创build2013年3月的新date时间或<。

计数字节@fvu答案可以进一步缩短为:

 private LocalDate calcNextFriday(LocalDate d) { return d.plusWeeks(d.getDayOfWeek() < DateTimeConstants.FRIDAY ? 0 : 1).withDayOfWeek(DateTimeConstants.FRIDAY); } 

周五的参数为0,下周的周五

 public java.util.Calendar getNextweekOfDay(int weekOfDay) throws Exception{ java.util.Calendar today = java.util.Calendar.getInstance(); int dayOfWeek = today.get(java.util.Calendar.DAY_OF_WEEK); int daysUntilNextWeekOfDay = weekOfDay - dayOfWeek; java.util.Calendar nextWeekOfDay = (java.util.Calendar)today.clone(); nextWeekOfDay.add(java.util.Calendar.DAY_OF_WEEK, daysUntilNextWeekOfDay); nextWeekOfDay.add(java.util.Calendar.DAY_OF_WEEK, 7); nextWeekOfDay.set(java.util.Calendar.HOUR_OF_DAY, 0); return nextWeekOfDay; }