When you set a date-time value in java, you may encounter a timezone 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 the date-time in your desired timezone.
1. java.util.TimeZone Properties And Methods.
- TimeZone.getDefault() : This method will return a
java.util.TimeZone
object which represent current system used timezone. - 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. - getDisplayName() : Return the time zone display name.
- getID() : Return the timezone id. Such as “Pacific/Asia”.
- getOffset( long currentTimeMillis ) : Return offset milliseconds between UTC and the used timezone.
2. Show Specified TimeZone Info Example.
- The below java method will show the specified TimeZone info in the console.
/* 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.
- The below java methods will convert date-time between different TimeZones.
// 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 the 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"); }
- Below is the example execution output.
Date milliseconds : 1510222127526 Source date time : Date time " 2021-11-9 2:8:47 Zone Id : America/Los_Angeles, Zone Offset : -8 hour Destination date time : Date time " 2021-11-9 18:8:47 Zone Id : Asia/Singapore, Zone Offset : 8 hour Date milliseconds : 946693586527 Source date time : Date time " 2020-12-31 18:26:26 Zone Id : America/Los_Angeles, Zone Offset : -8 hour Destination date time : Date time " 2021-1-1 10:26:26 Zone Id : Asia/Singapore, Zone Offset : 8 hour