Java Utililty Methods Date Convert

List of utility methods to do Date Convert

Description

The list of methods to do Date Convert are organized into topic(s).

Method

voidconvertLocalToGMT(int[] dateTime)
Use this method on values returned from a DateTimeSelector, if you want the selector to show local time but serialize to GMT.
Calendar local = new GregorianCalendar();
local.set(dateTime[YEAR], dateTime[MONTH], dateTime[DAY], dateTime[HOUR], dateTime[MINUTE],
        dateTime[SECOND]);
Calendar GMTcal = new GregorianCalendar(TimeZone.getTimeZone("GMT+0")); 
GMTcal.setTime(local.getTime());
dateTime[YEAR] = GMTcal.get(Calendar.YEAR);
dateTime[MONTH] = GMTcal.get(Calendar.MONTH);
dateTime[DAY] = GMTcal.get(Calendar.DATE);
...
CalendarconvertLocalToTimezone(Date original, String targetTimezoneId)
convert Local To Timezone
Calendar local = new GregorianCalendar();
local.setTime(original);
Calendar targetCal = new GregorianCalendar(TimeZone.getTimeZone(targetTimezoneId));
targetCal.setTimeInMillis(local.getTimeInMillis());
return targetCal;
DateconvertsDateToGMTTimezone(Date date)
converts Date To GMT Timezone
Calendar calGMT = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calGMT.setTime(date);
return calGMT.getTime();
DateconvertSistemDependentDateToDesiredTimeZone(final Date sistemDependentDate, TimeZone desiredTimeZone)
Convert sistem dependent date to desired time zone.
Long msDate = sistemDependentDate.getTime();
int systemTimeZoneOffset = TimeZone.getDefault().getOffset(Calendar.getInstance().getTimeInMillis());
if (desiredTimeZone == null) {
    desiredTimeZone = TimeZone.getDefault();
int desiredTimeZoneOffset = desiredTimeZone.getOffset(Calendar.getInstance().getTimeInMillis());
int totalMsOffset = -systemTimeZoneOffset + desiredTimeZoneOffset;
msDate = msDate + totalMsOffset;
...
DateconvertStringToDate(String date)
Helper method to convert the date from the xml to date
String year = date.substring(0, 4);
String month = date.substring(5, 7);
String day = date.substring(8, 10);
String hour = date.substring(11, 13);
String min = date.substring(14, 16);
String sec = date.substring(17, 19);
Calendar caldate = Calendar.getInstance();
caldate.set(Calendar.YEAR, Integer.parseInt(year));
...
CalendarconvertStringToDate(String date)
Converts date in the string format 'dd.mm.yyyy' to an date object.
int day = Integer.parseInt(date.substring(0, 2));
int month = Integer.parseInt(date.substring(3, 5)) - 1;
int year = Integer.parseInt(date.substring(6));
return new GregorianCalendar(year, month, day);
DateconvertStringToDate(String string)
Convert data of the form "MM-DD-YY HH:mm" to Date
if (string == null) {
    return null;
String[] parts = splitStringIntoParts(string);
if (parts == null) {
    return null;
boolean result = validateDateParts(parts);
...
DateconvertToClientTime(Date serverDate, Integer clientTimezoneOffset)
convert To Client Time
if (clientTimezoneOffset == null || clientTimezoneOffset == getServerTimezoneOffset())
    return serverDate;
Long gmtTime = serverDate.getTime() + getServerTimezoneOffset() * (60 * 1000);
gmtTime -= clientTimezoneOffset * (60 * 1000);
return new Date(gmtTime);
DateconvertToDate(String source)
Converts a given string into a date.
if ((source == null) || source.trim().equals("")) {
    return null;
source = source.trim();
boolean bc = false;
if (source.startsWith("-")) {
    source = source.substring(1);
    bc = true;
...
StringconvertToDateStamp(long time)
Convert given date to string
OutputFormat: yyyymmdd_hhmm
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(time));
return convertToDateStamp(cal);