Below snippet return the start of next week date.
public static long startStartOfNextWeek(long now) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(now));
calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.WEEK_OF_YEAR, 1);
return calendar.getTime().getTime();
}
Find the below working application.
GetStartOfNextWeek.java
package com.sample.app.time;
import java.util.Calendar;
import java.util.Date;
public class GetStartOfNextWeek {
public static long startStartOfNextWeek(long now) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(now));
calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.WEEK_OF_YEAR, 1);
return calendar.getTime().getTime();
}
public static void main(String args[]) {
Date today = new Date();
long startOfNextWeek = startStartOfNextWeek(today.getTime());
Date startOfNextWeekDate = new Date(startOfNextWeek);
System.out.println("today : " + today);
System.out.println("startOfNextWeekDate : " + startOfNextWeekDate);
}
}
Output
today : Sat Feb 26 09:57:07 IST 2022 startOfNextWeekDate : Sun Feb 27 00:00:00 IST 2022
No comments:
Post a Comment