Using ‘java.util.Calendar’ class, we can get all the current week dates in Java.
CurrentWeekDates.java
package com.sample.app;
import java.util.Calendar;
import java.util.Date;
public class CurrentWeekDates {
private static void printAllCurrentWeekDates() {
Calendar calendar = Calendar.getInstance();
for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
calendar.set(Calendar.DAY_OF_WEEK, i);
System.out.println("Date " + calendar.getTime());
}
}
public static void main(String[] args) {
System.out.println("today : " + new Date() +"\n\n");
printAllCurrentWeekDates();
}
}
Sample Output
today : Mon Jul 11 09:56:33 IST 2022 Date Sun Jul 10 09:56:33 IST 2022 Date Mon Jul 11 09:56:33 IST 2022 Date Tue Jul 12 09:56:33 IST 2022 Date Wed Jul 13 09:56:33 IST 2022 Date Thu Jul 14 09:56:33 IST 2022 Date Fri Jul 15 09:56:33 IST 2022 Date Sat Jul 16 09:56:33 IST 2022
Previous Next Home
No comments:
Post a Comment