Get first next Monday after certain date?

Java 8+

LocalDate ld = LocalDate.of(2014, Month.JUNE, 12);
System.out.println(ld);
ld = ld.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println(ld);

Which prints…

2014-06-12
2014-06-16

Because it’s possible that the date my actually be a Monday, you could also use…

ld = ld.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));

Java <= 7

You should be using the ThreeTen Backport, which gives you the support of the Java 8 Date/Time API

Original Answer

Instead of System.out.println(date1); use System.out.println(date1.getTime());

getTime returns an instance of Date which represents the current state of the Calendar

Which will output Mon Jul 14 00:00:00 EST 2014

System.out.println(date1) is the equivlent of using System.out.println(date1.toString()), which, in this case, is dumping a bunch of useful info about the state of the Calendar object, but not really human readable data.

System.out.println(date1.getTime()) will use the Date‘s to toString method to display a date value, formatted based on the current local settings, which will provide more useful information.

Updated

Instead of using GregorianCalendar, you should use the system Calendar, for example…

Calendar date1 = Calendar.getInstance();
date1.set(2014, 06, 12);

Also, months are 0 indexed, meaning that Janurary is actually 0 not 1, so in your example, you’ve specified the month as July, not June.

So, instead, using…

Calendar date1 = Calendar.getInstance();
date1.set(2014, 05, 12);

while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
    date1.add(Calendar.DATE, 1);
}

System.out.println(date1.getTime());

Which outputted…

Mon Jun 16 16:22:26 EST 2014

Which is next Monday from today…more or less 😉

Leave a Comment