Java Utililty Methods Calendar Adjust

List of utility methods to do Calendar Adjust

Description

The list of methods to do Calendar Adjust are organized into topic(s).

Method

voidadjustCalendar(Calendar calendar)
adjust Calendar
if (calendar.get(Calendar.HOUR_OF_DAY) != 0) {
    if (calendar.get(Calendar.HOUR_OF_DAY) == 1)
        calendar.setTimeInMillis(calendar.getTimeInMillis() - MILLISECONDS_PER_HOUR);
    if (calendar.get(Calendar.HOUR_OF_DAY) == 23)
        calendar.setTimeInMillis(calendar.getTimeInMillis() + MILLISECONDS_PER_HOUR);
voidadjustCalendarToLocalTime(Calendar cal)
Needed to rectify the interpretation of timestamp values in calendar objects.
Date date = cal.getTime();
TimeZone tz = TimeZone.getTimeZone("CET");
long msFromEpochGmt = date.getTime(); 
int offsetFromUTC = tz.getOffset(msFromEpochGmt);
cal.setTimeInMillis(msFromEpochGmt + offsetFromUTC);
CalendaradjustDate(Calendar calendar, int differenceInDay)
Adjusts the Calendar to several days before or after the current date.
calendar.setTimeInMillis(calendar.getTimeInMillis() + DAY_IN_MS * differenceInDay);
return calendar;
longadjustedMillis(Calendar cal)
Gets the time in milliseconds from a Calendar, adjusting for timezone so that day subtraction works properly.
return cal.getTimeInMillis() + cal.getTimeZone().getOffset(cal.getTimeInMillis());
voidadjustToDayEnd(Calendar cal)
Set the hours, minutes, seconds and milliseconds so the calendar represents midnight minus one milli-second.
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 999);
voidadjustToFieldStart(Calendar cal, int[] fields)
Set the calendar fields in the array to their actual (Considering the current Calendar time) minimum.
for (int i = 0; i < fields.length; i++) {
    int f = fields[i];
    if (f == Calendar.DAY_OF_WEEK_IN_MONTH) {
        cal.get(Calendar.DAY_OF_WEEK);
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
    } else {
        cal.set(f, cal.getActualMinimum(f));
voidadjustToMonthStart(Calendar cal)
Adjust a Date to the start of the month (and start of the day).
cal.set(Calendar.DAY_OF_MONTH, 1);
CalendargetMidnightCalendarByUnadjustedDate(Date date, TimeZone timezone)
get Midnight Calendar By Unadjusted Date
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Calendar mcal = getMidnightCalendarInstance(timezone);
mcal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
mcal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
mcal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
return mcal;
voidtruncCalendarToWeek(Calendar c, int adjustIncrement)
trunc Calendar To Week
truncCalendarToWeek(c, adjustIncrement, Calendar.getInstance().getFirstDayOfWeek());
voidtruncCalendarToWeek(Calendar c, int adjustIncrement)
truncate the calendar to week
if (adjustIncrement < -1) {
    adjustIncrement = -1;
if (adjustIncrement > 1) {
    adjustIncrement = 1;
if (adjustIncrement == 0) {
    adjustIncrement = -1;
...