Time: How to get to next Friday?

How can I get next Friday using the Joda-Time API.

LocalDate today today . I think you should decide where you are before or after Friday of the current week. See this method:

 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); } } 

Is it possible to do this shorter or using oneliner?

PS: Please do not advise me to use the JDK date / time. Joda-Time is a much better API.

Java 8 introduces the java.time package ( Tutorial ), which is even better.

+55
java date datetime time jodatime
Oct 28 '09 at 9:16
source share
7 answers

java.time

With the java.time framework built into Java 8 and later ( Tutorial ), you can use TemporalAdjusters to get the next or previous day of the week .

 private LocalDate calcNextFriday(LocalDate d) { return d.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)); } 
+72
Mar 12 '15 at 12:53
source share

This can be made much easier to read:

 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)); } 

or in shorter form

 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 a possibility original object is returned } 

or even shorter

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

PS. I have not tested the real code! :)

+52
Oct 28 '09 at 9:56
source share

Your code in 1 line

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

Alternative approach

 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); } 

somewhat verified; -)

+5
Oct 28 '09 at 10:06
source share

I just wasted like 30 minutes trying to figure it out myself, but I generally had to jump forward.

Anyway, here is my solution:

 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; } 

Now you just need to do Partial (which is LocalDate) for the day of the week.

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

Now, whatever the most significant field for the partial, there will be +1 if the current date is after it (now).

That is, if you partial from March 2012, it will create a new date-time in March 2013 or <.

+4
Jul 18 '12 at 3:01
source share
 import java.util.Calendar; private Calendar getNextweekOfDay(int weekOfDay) { Calendar today = Calendar.getInstance(); int dayOfWeek = today.get(Calendar.DAY_OF_WEEK); int daysUntilNextWeekOfDay = weekOfDay - dayOfWeek; if (daysUntilNextWeekOfDay == 0) daysUntilNextWeekOfDay = 7; Calendar nextWeekOfDay = (Calendar)today.clone(); nextWeekOfDay.add(Calendar.DAY_OF_WEEK, daysUntilNextWeekOfDay); return nextWeekOfDay; } // set alarm for next Friday 9am public void setAlarm() { Calendar calAlarm = getNextweekOfDay(Calendar.FRIDAY); calAlarm.set(Calendar.HOUR_OF_DAY, 9);//9am calAlarm.set(Calendar.MINUTE, 0); calAlarm.set(Calendar.SECOND, 0); scheduleAlarm(calAlarm);// this is my own method to schedule a pendingIntent } 
+4
Jun 16 '15 at 7:19
source share

byte counting @fvu's answer can be shortened even to:

 private LocalDate calcNextFriday(LocalDate d) { return d.plusWeeks(d.getDayOfWeek() < DateTimeConstants.FRIDAY ? 0 : 1).withDayOfWeek(DateTimeConstants.FRIDAY); } 
+3
Oct 06 '14 at 10:18
source share

A simple module-based solution that should work with most previous versions of Java if you are not allowed to upgrade the java version to java8 or forward or use the standard java date library as jodatime

The number of days added to your date is determined by the following formula:

(7 + Calendar.FRIDAY - yourDateAsCalendar.get (Calendar.DAY_OF_WEEK))% 7

Please note that this can be generalized to any day of the week by changing the static Calendar.FRIDAY field to your work day. Some snippets of code below

 public static void main(String[] args) { for (int i = 0; i < 15; i++) { Calendar cur = Calendar.getInstance(); cur.add(Calendar.DAY_OF_MONTH, i); Calendar friday = Calendar.getInstance(); friday.setTime(cur.getTime()); friday.add(Calendar.DAY_OF_MONTH, (7 + Calendar.FRIDAY - cur.get(Calendar.DAY_OF_WEEK)) % 7); System.out.println(MessageFormat.format("Date {0} -> {1} ", cur.getTime(), friday.getTime())); } } 
0
Nov 22 '18 at 1:39
source share



All Articles