Java Utililty Methods Calendar Time

List of utility methods to do Calendar Time

Description

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

Method

voidsetTimeToMinOrMax(Calendar cal, boolean max)
set Time To Min Or Max
if (max) {
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    cal.set(Calendar.MILLISECOND, 999);
} else {
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
...
voidstripTime(final Calendar cal)
strip Time
cal.clear(Calendar.AM_PM);
cal.clear(Calendar.HOUR_OF_DAY);
cal.clear(Calendar.HOUR);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
voidstripTimeComponent(Calendar cal)
Strips the time component from a Calendar object, leaving only the date component
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
voidtime2InicioDelDia(Calendar fecha)
time Inicio Del Dia
fecha.set(Calendar.HOUR_OF_DAY, 0);
fecha.set(Calendar.MINUTE, 0);
fecha.set(Calendar.SECOND, 0);
fecha.set(Calendar.MILLISECOND, 0);
Calendartimestamp17ToCalendar(String timestamp17String)
Convert 17-digit date format timestamps (as found in crawl.log, for example) into a GregorianCalendar object.
GregorianCalendar calendar = new GregorianCalendar();
int year = Integer.parseInt(timestamp17String.substring(0, 4));
int dayOfMonth = Integer.parseInt(timestamp17String.substring(6, 8));
int month = Integer.parseInt(timestamp17String.substring(4, 6)) - 1;
int hourOfDay = Integer.parseInt(timestamp17String.substring(8, 10));
int minute = Integer.parseInt(timestamp17String.substring(10, 12));
int second = Integer.parseInt(timestamp17String.substring(12, 14));
int milliseconds = Integer.parseInt(timestamp17String.substring(14, 17));
...
Calendartimestamp2Calendar(long timestamp)
timestamp Calendar
Date date = timestamp2Date(timestamp);
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("Europe/Brussels"));
cal.setTime(date);
return cal;
CalendartimestampToCalendar(String timestamp)
timestamp To Calendar
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
int year = Integer.parseInt(timestamp.substring(0, 4));
int month = Integer.parseInt(timestamp.substring(4, 6)) - 1; 
int day = Integer.parseInt(timestamp.substring(6, 8));
int hour = Integer.parseInt(timestamp.substring(8, 10));
int minute = Integer.parseInt(timestamp.substring(10, 12));
int second = Integer.parseInt(timestamp.substring(12, 14));
calendar.set(year, month, day, hour, minute, second);
...
CalendartoDefaultTimeZone(Calendar calendar)
to Default Time Zone
Calendar c = Calendar.getInstance();
c.setTimeInMillis(calendar.getTimeInMillis());
return c;
StringtoTimeStamp(Calendar cal)
Converts a Calendar object into a timestamp string in ISO 8601 format.
if (cal == null) {
    cal = Calendar.getInstance();
StringBuilder sb = new StringBuilder();
sb.append(String.format("%04d", cal.get(Calendar.YEAR)));
sb.append('-').append(String.format("%02d", cal.get(Calendar.MONTH) + 1));
sb.append('-').append(String.format("%02d", cal.get(Calendar.DAY_OF_MONTH)));
sb.append('T').append(String.format("%02d", cal.get(Calendar.HOUR_OF_DAY)));
...
StringtoTimeString(final Calendar calendar)
Show the Time value of a Calendar as a String.
String strHour;
String strMinute;
String strSecond;
strHour = "";
strMinute = "";
strSecond = "";
if (calendar != null) {
    if (calendar.get(Calendar.HOUR_OF_DAY) < 10) {
...