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

Calendarconvert(Date d)
convert
Calendar cal = Calendar.getInstance();
cal.setTime(d);
return cal;
DateconvertDate(Object obj)
convert Date
if (obj != null) {
    if (obj instanceof Calendar) {
        Calendar cal = (Calendar) obj;
        return cal.getTime();
    if (obj instanceof Date) {
        return (Date) obj;
return null;
longconvertDateTime(String date, String time)
convert Date Time
Calendar calendar = GregorianCalendar.getInstance();
if (date != null) {
    String[] dates = date.split("/");
    int day = Integer.valueOf(dates[0]);
    int month = Integer.valueOf(dates[1]) - 1;
    int year = Integer.valueOf(dates[2]);
    calendar.set(Calendar.DATE, day);
    calendar.set(Calendar.MONTH, month);
...
longconvertDateTimeToSysTime(String sDateTime)
convert Date Time To Sys Time
int nYear = Integer.parseInt(sDateTime.substring(0, 4));
int nMonth = Integer.parseInt(sDateTime.substring(4, 6));
int nDay = Integer.parseInt(sDateTime.substring(6, 8));
int nHour = 0, nMinute = 0, nSecond = 0;
if (sDateTime.length() == 14) {
    nHour = Integer.parseInt(sDateTime.substring(8, 10));
    nMinute = Integer.parseInt(sDateTime.substring(10, 12));
    nSecond = Integer.parseInt(sDateTime.substring(12, 14));
...
StringConvertDateToAge(String date)
Converts the date in format yyyy/mm/dd to the age of the person
if (date == null)
    return "";
int year, month, day;
try {
    int pos = date.indexOf("/");
    int prev_pos = pos + 1;
    year = Integer.parseInt(date.substring(0, pos));
    pos = date.indexOf("/", pos + 1);
...
DoubleconvertDateToDoubleTime(Date time)
convert Date To Double Time
if (time == null) {
    return null;
Calendar c = Calendar.getInstance();
c.setTime(time);
double t = ((c.get(Calendar.HOUR_OF_DAY) * 60 + c.get(Calendar.MINUTE)) * 60 + c.get(Calendar.SECOND))
        * 1000 + c.get(Calendar.MILLISECOND);
Double d = new Double(t / DAY_MILLIS);
...
intconvertDateToInt(final Date date)
Converts a Date to an Integer formated as YYYYMMDD
calendar.setTimeInMillis(date.getTime());
return (calendar.get(Calendar.YEAR) * 10000) + ((calendar.get(Calendar.MONTH) + 1) * 100)
        + calendar.get(Calendar.DAY_OF_MONTH);
longconvertDateToLong(int year, int month, int day, int hour, int minute, int second)
convert a given date to long representation
Date d = getDate(year, month, day, hour, minute, second);
return d.getTime();
longconvertDateToServerTimeZoneDateInMilliSec(String timeZoneName, long milliSecond)
This will convert server date/time to specified Time zone date/time and return the date in milliSecond.
if (timeZoneName != null) {
    TimeZone loginTimeZone = TimeZone.getTimeZone(timeZoneName);
    Calendar calender = Calendar.getInstance(loginTimeZone);
    if (milliSecond != 0) {
        calender.setTimeInMillis(milliSecond);
    calender.add(Calendar.MILLISECOND, -(calender.getTimeZone().getRawOffset()));
    calender.add(Calendar.MILLISECOND, -(calender.getTimeZone().getDSTSavings()));
...
longconvertDateToTimeStamp(Date date)
This method is used for converting date to time stamp in long
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
return calendar.getTimeInMillis();