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

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());
}
}
2020-07-20
2020-07-27
Mon Jul 20 11:14:15 IST 2020

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

ASP.Net Core and Docker for Beginner’s

Welcome to a new start.

Research on Case studies of fixed capacity Queue and Changing the Queue capacity using Artificial…

I gave up holidays to get shit done.

WINLAMBO Jackpots Explainer

Introducing Nx Cloud 2.0

I Hacked Rarible.com — Here’s how I did it.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Hackerrank ACM ICPC Team Solution

10 Things I’ve Learnt In My First Year As A Java Developer

Global’s Technology & Digital Early Careers Programme

Java for Azure Developers and Architects ( 1 of 15) — Overview

Java for Azure Architects and Developers

Java — Rewriting Traditional Codes With Streams #3 (collect)