Set year, month and day to a date
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void setDay(Date date, int day) {
if (date == null)
return;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, day);
}
public static void setMonth(Date date, int month) {
if (date == null)
return;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.MONTH, month - 1);
}
public static void setYear(Date date, int year) {
if (date == null)
return;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.YEAR, year);
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Date Set:
- Set Date to Noon
- Set Date to the first millisecond of the day, just after midnight.
- Set Date to the first millisecond of the month, just after midnight.
- Set Date to the last millisecond of the day, just before midnight.
- Set Date to the last millisecond of the minute.
- Set Date to the last millisecond of the month, just before midnight.
- Set date to clear the time values
- Set date to the end of the day
- Set Date to the start of the day
- Set date to the end of a day
- Set date to the start of an hour
- Set date to then end of an hour
- Set date to the start of a minute
- Set date to the end of a minute
- Set Calendar to Mid night
- Set year, month and day to a date
- Roll java.util.Date back and forth