Java Utililty Methods Calendar Year

List of utility methods to do Calendar Year

Description

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

Method

voidsetCalendarFields(Calendar cal, int year, int month, int day, int hour, int minute, int second, int millis)
set Calendar Fields
if (year <= 0) {
    cal.set(Calendar.ERA, GregorianCalendar.BC);
    cal.set(Calendar.YEAR, 1 - year);
} else {
    cal.set(Calendar.ERA, GregorianCalendar.AD);
    cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
...
voidsetDate(Calendar cal, int year, int month, int day)
set Date
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DATE, day);
GregorianCalendartoCalendar(int year, int month, int day, int hour, int minutes, int seconds)
to Calendar
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(year, month - 1, day, hour, minutes, seconds);
return calendar;
StringtoStringYearMonth(Calendar calendar)
to String Year Month
String yyyy = String.valueOf(calendar.get(Calendar.YEAR) + BUDDHIST_YEAR);
String mm = String.valueOf(calendar.get(Calendar.MONTH) + 1);
if (mm.length() < 2) {
    mm = "0" + mm;
return yyyy + mm;
voidtruncCalendarToHalfYear(Calendar c)
truncate the calendar to half year (i.e.
if (c == null) {
    return;
int month = c.get(Calendar.MONTH);
truncCalendarToYear(c);
if (month >= Calendar.JULY) {
    c.set(Calendar.MONTH, Calendar.JULY);
voidtruncCalendarToYear(Calendar c)
truncate the calendar to year
if (c == null) {
    return;
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
...
intyear(Calendar calendar)
year
return calendar.get(Calendar.YEAR);
intyear(Calendar date)
year
return date.get(Calendar.YEAR);