Program
import java.util.Calendar; public class DateAllDays { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); if (args.length == 2) { int year = Integer.parseInt(args[0]); int month = Integer.parseInt(args[1]); calendar.set(year, month, 1); } int month = calendar.get(Calendar.MONTH); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); while (calendar.get(Calendar.MONTH) == month) { System.out.println(calendar.getTime()); calendar.add(Calendar.DATE, 1); } } }
Output
Thu Apr 01 00:00:00 IST 2021 Fri Apr 02 00:00:00 IST 2021 Sat Apr 03 00:00:00 IST 2021 Sun Apr 04 00:00:00 IST 2021 Mon Apr 05 00:00:00 IST 2021 Tue Apr 06 00:00:00 IST 2021 Wed Apr 07 00:00:00 IST 2021 Thu Apr 08 00:00:00 IST 2021 Fri Apr 09 00:00:00 IST 2021 Sat Apr 10 00:00:00 IST 2021 Sun Apr 11 00:00:00 IST 2021 Mon Apr 12 00:00:00 IST 2021 Tue Apr 13 00:00:00 IST 2021 Wed Apr 14 00:00:00 IST 2021 Thu Apr 15 00:00:00 IST 2021 Fri Apr 16 00:00:00 IST 2021 Sat Apr 17 00:00:00 IST 2021 Sun Apr 18 00:00:00 IST 2021 Mon Apr 19 00:00:00 IST 2021 Tue Apr 20 00:00:00 IST 2021 Wed Apr 21 00:00:00 IST 2021 Thu Apr 22 00:00:00 IST 2021 Fri Apr 23 00:00:00 IST 2021 Sat Apr 24 00:00:00 IST 2021 Sun Apr 25 00:00:00 IST 2021 Mon Apr 26 00:00:00 IST 2021 Tue Apr 27 00:00:00 IST 2021 Wed Apr 28 00:00:00 IST 2021 Thu Apr 29 00:00:00 IST 2021 Fri Apr 30 00:00:00 IST 2021
Description
public static Calendar getInstance()
Gets a calendar using the default time zone and locale. The Calendar returned is based on the current time in the default time zone with the default locale.
Returns:
a Calendar.
public int get(int field)
Returns the value of the given calendar field. In lenient mode, all calendar fields are normalized. In non-lenient mode, all calendar fields are validated and this method throws an exception if any calendar fields have out-of-range values. The normalization and validation are handled by the complete() method, which process is calendar system dependent.
Parameters:
field – the given calendar field.
Returns:
the value for the given calendar field.
Throws:
ArrayIndexOutOfBoundsException – if the specified field is out of range (field < 0 || field >= FIELD_COUNT).
public final Date getTime()
Returns a Date object representing this Calendar’s time value (millisecond offset from the Epoch”).
Returns:
a Date representing the time value.
public void set(int field,
int value)
Sets the given calendar field to the given value. The value is not interpreted by this method regardless of the leniency mode.
Parameters:
field – the given calendar field.
value – the value to be set for the given calendar field.
Throws:
ArrayIndexOutOfBoundsException – if the specified field is out of range (field < 0 || field >= FIELD_COUNT). in non-lenient mode.
public abstract void add(int field,
int amount)
Adds or subtracts the specified amount of time to the given calendar field, based on the calendar’s rules. For example, to subtract 5 days from the current time of the calendar, you can achieve it by calling:
add(Calendar.DAY_OF_MONTH, -5).
Parameters:
field – the calendar field.
amount – the amount of date or time to be added to the field.