Java TimeZone Example

When you set a date time value in java, you may encounter time zone issue. You will find the date time you get use java.util.Calendar class will always use the system default timezone. This example will tell you how to change it to show date time in your desired timezone.

1. java.util.TimeZone Properties And Methods.

  1. TimeZone.getDefault() : This method will return a java.util.TimeZone object which represent current system used timezone.
  2. TimeZone.getTimeZone(“Asia/Hong_Kong”) : This method will return a java.util.TimeZone object for a specified timezone id. You can click here to get all timezone id list.
  3. getDisplayName() : Return the time zone display name.
  4. getID() :  Return the timezone id. Such as “Pacific/Asia”.
  5. getOffset( long currentTimeMillis ) : Return offset milliseconds between UTC and the used timezone.

2. Show Specified TimeZone Info Example.

/* Show default or specified timezone info data. */
public  void showSpecifiedTimeZoneInfo(String timeZoneId)
{
    // Get current calendar.
    Calendar currCalendar = Calendar.getInstance();

    TimeZone targetTimeZone = null;

    if(timeZoneId==null || timeZoneId.trim().length()==0)
    {
        // Get default time zone.
        targetTimeZone = currCalendar.getTimeZone().getDefault();
    }else
    {
        // Get target time zone by id.
        targetTimeZone = TimeZone.getTimeZone(timeZoneId);
    }

    String targetTimeZoneDisplayName = targetTimeZone.getDisplayName();
    String targetTimeZoneId = targetTimeZone.getID();
    long targetTimeZoneOffSet = targetTimeZone.getOffset(System.currentTimeMillis());
    // Translate the milliseconds to hour.
    int offSetHour = (int)(targetTimeZoneOffSet / 1000 / 60 / 60);

    StringBuffer msgBuf = new StringBuffer();
    msgBuf.append("Time zone info. dislay name : ");
    msgBuf.append(targetTimeZoneDisplayName);
    msgBuf.append(" , id : ");
    msgBuf.append(targetTimeZoneId);
    msgBuf.append(" , offset hour : ");
    msgBuf.append(offSetHour);

    System.out.println(msgBuf.toString());
}

3. Convert Date Time Between TimeZone Example.

// Show a given date time milliseconds value in different timezone.
private void convertBetweenTimeZone(long milliseconds, String srcTimeZoneId, String destTimeZoneId)
{
    if(srcTimeZoneId!=null && srcTimeZoneId.trim().length() > 0 && destTimeZoneId!=null && destTimeZoneId.trim().length() > 0) {

        // Get source and destination timezone object.
        TimeZone srcTimeZone = TimeZone.getTimeZone(srcTimeZoneId);

        TimeZone destTimeZone = TimeZone.getTimeZone(destTimeZoneId);

        // Get calendar object.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(milliseconds);

        calendar.setTimeZone(srcTimeZone);
        String srcDateTime = this.getDateTime(calendar);

        calendar.setTimeZone(destTimeZone);
        String destDateTime = this.getDateTime(calendar);

        System.out.println("Date milliseconds : " + milliseconds);
        System.out.println("Source date time : " + srcDateTime);
        System.out.println("Destination date time : " + destDateTime);
    }
}

// Show a given date time in different time zone.
private void convertBetweenTimeZone(int year, int month, int day, int hour, int minute, int second, String srcTimeZoneId, String destTimeZoneId)
{
    if(srcTimeZoneId!=null && srcTimeZoneId.trim().length() > 0 && destTimeZoneId!=null && destTimeZoneId.trim().length() > 0) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);

        this.convertBetweenTimeZone(calendar.getTimeInMillis(), srcTimeZoneId, destTimeZoneId);
    }
}

/* Get special calendar date time in string format. */
private String getDateTime(Calendar calendar)
{
    StringBuffer strBuffer = new StringBuffer();
    strBuffer.append("Date time : ");
    strBuffer.append(calendar.get(Calendar.YEAR));
    strBuffer.append("-");
    strBuffer.append(calendar.get(Calendar.MONTH) + 1);
    strBuffer.append("-");
    strBuffer.append(calendar.get(Calendar.DAY_OF_MONTH));
    strBuffer.append(" ");
    strBuffer.append(calendar.get(Calendar.HOUR_OF_DAY));
    strBuffer.append(":");
    strBuffer.append(calendar.get(Calendar.MINUTE));
    strBuffer.append(":");
    strBuffer.append(calendar.get(Calendar.SECOND));
    strBuffer.append(" Zone Id : ");
    strBuffer.append(calendar.getTimeZone().getID());

    strBuffer.append(", Zone Offset : ");

    long zoneOffSet = calendar.get(Calendar.ZONE_OFFSET);
    // Translate the milliseconds to hour.
    int offSetHour = (int)(zoneOffSet / 1000 / 60 / 60);
    strBuffer.append(offSetHour);
    strBuffer.append(" hour");
    return strBuffer.toString();
}

public static void main(String args[])
{
    TimeZoneExample timeZoneExample = new TimeZoneExample();
    timeZoneExample.showSpecifiedTimeZoneInfo("America/Jamaica");

    timeZoneExample.convertBetweenTimeZone(System.currentTimeMillis(), "America/Los_Angeles", "Asia/Singapore");

    timeZoneExample.convertBetweenTimeZone(2000, 1, 1, 10, 26, 26, "America/Los_Angeles", "Asia/Singapore");

}

Output

java timezone example console output

(Visited 421 times, 1 visits today)
READ :   Java Variables and Data Types Introduction

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.