Java Utililty Methods Calendar Calculate

List of utility methods to do Calendar Calculate

Description

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

Method

Calendardecode(String timestamp, Calendar calendar)
Converts a DSA encoded timestamp into a Java Calendar.
if (calendar == null) {
    calendar = Calendar.getInstance();
try {
    char[] chars = timestamp.toCharArray();
    int idx = 0;
    int year = convertDigits(chars[idx++], chars[idx++], chars[idx++], chars[idx++]);
    validateChar(chars[idx++], '-');
...
StringBuilderencode(Calendar calendar, boolean encodeTzOffset, StringBuilder buf)
Converts a Java Calendar into a DSA encoded timestamp.
if (buf == null) {
    buf = new StringBuilder();
long millis = calendar.getTimeInMillis();
int tmp = calendar.get(Calendar.YEAR);
buf.append(tmp).append('-');
tmp = calendar.get(Calendar.MONTH) + 1;
if (tmp < 10)
...
booleanequals(Calendar calendar1, Calendar calendar2)
Equals two calendar, compare day to day, month to month and year to year
int day1 = calendar1.get(Calendar.DAY_OF_MONTH);
int day2 = calendar2.get(Calendar.DAY_OF_MONTH);
int month1 = calendar1.get(Calendar.MONTH);
int month2 = calendar2.get(Calendar.MONTH);
int year1 = calendar1.get(Calendar.YEAR);
int year2 = calendar2.get(Calendar.YEAR);
return (day1 == day2 && month1 == month2 && year1 == year2);
StringFormatTime(Calendar p_time, boolean p_showMilliseconds)
Format Time
return FormatTime(p_time.get(Calendar.HOUR_OF_DAY), p_time.get(Calendar.MINUTE),
        p_time.get(Calendar.SECOND), (p_showMilliseconds ? p_time.get(Calendar.MILLISECOND) : -1), ":");
CalendargetCalendar(long millis)
get Calendar
Calendar c = Calendar.getInstance();
c.setTimeInMillis(millis);
return c;
GregorianCalendargetGregorianCalendar(String yyyymmdd)
get Gregorian Calendar
int yyyy = Integer.parseInt(yyyymmdd.substring(0, 4));
int mm = Integer.parseInt(yyyymmdd.substring(4, 6));
int dd = Integer.parseInt(yyyymmdd.substring(6, 8));
GregorianCalendar calendar = new GregorianCalendar(yyyy, mm - 1, dd, 0, 0, 0);
return calendar;
longgetLongTime(Calendar cal)
get Long Time
int date = getDateInt(cal);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
String hourStr = addPreZero(hour);
String minuteStr = addPreZero(minute);
String time = date + hourStr + minuteStr;
return Long.parseLong(time);
StringgetTimeStr3(Calendar timeInstance)
Returns a string of the time instance in string format for internal use.
Calendar time = timeInstance;
int field;
String year = "", month = "", day = "", hour = "", minute = "", second = "";
String timeStr = "";
field = time.get(Calendar.YEAR);
if (field < 10) {
    year = "000";
year = year + Integer.toString(field);
field = time.get(Calendar.MONTH) + 1;
if (field < 10) {
    month = "0";
month = month + Integer.toString(field);
field = time.get(Calendar.DAY_OF_MONTH);
if (field < 10) {
    day = "0";
day = day + Integer.toString(field);
field = time.get(Calendar.HOUR_OF_DAY);
if (field < 10) {
    hour = "0";
hour = hour + Integer.toString(field);
field = time.get(Calendar.MINUTE);
if (field < 10) {
    minute = "0";
minute = minute + Integer.toString(field);
field = time.get(Calendar.SECOND);
if (field < 10) {
    second = "0";
second = second + Integer.toString(field);
timeStr = year + month + day + hour + minute + second;
return timeStr;
StringgetTimeString(Calendar cal)
get Time String
int hour = cal.get(Calendar.HOUR);
int minute = cal.get(Calendar.MINUTE);
boolean isAm = (cal.get(Calendar.AM_PM) == Calendar.AM);
String hourStr;
String minuteStr;
if (!isAm) {
    hour += 12;
if (hour < 10) {
    hourStr = "0" + Integer.toString(hour);
} else {
    hourStr = Integer.toString(hour);
if (minute < 10) {
    minuteStr = "0" + Integer.toString(minute);
} else {
    minuteStr = Integer.toString(minute);
String timeStr = hourStr + ":" + minuteStr;
return timeStr;
StringgetTimeStrWithMillisecond(Calendar timeInstance)
get Time Str With Millisecond
Calendar time = timeInstance;
int field;
String year = "", month = "", day = "", hour = "", minute = "", second = "", millisecond = "";
String timeStr = "";
year = Integer.toString(time.get(Calendar.YEAR));
field = time.get(Calendar.MONTH) + 1;
if (field < 10) {
    month = "0";
...