Java — Get first next Monday after certain date | Code Factory

Donate : Link

WordPress Blog : Link

package com.example.java.programming.datetime;import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
/**
* @author code.factory
*
*/
public class FirstNextMonday {
public static void main(String... args) {
// Java 8+
LocalDate ld = LocalDate.of(2020, Month.JULY, 20);
System.out.println(ld);
ld = ld.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
/*it's possible that the date my actually be a Monday, you could also use.*/
/*ld = ld.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));*/
System.out.println(ld);

//Java <= 7
Calendar date1 = Calendar.getInstance();
date1.set(2020, 06, 20);
while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
date1.add(Calendar.DATE, 1);
}
System.out.println(date1.getTime());
}
}

Output :

2020-07-20
2020-07-27
Mon Jul 20 11:14:15 IST 2020