Java – Convert date and time between timezone
来源:互联网 发布:可视化数据分析 编辑:程序博客网 时间:2024/05/29 18:46
In this tutorial, we will show you few examples (ZonedDateTime (Java 8), Date, Calendar and Joda Time) to convert a date and time between different time zones.
All examples will be converting the date and time from
(UTC+8:00) Asia/Singapore - Singapore TimeDate : 22-1-2015 10:15:55 AM
to
(UTC-5:00) America/New_York - Eastern Standard TimeDate : 21-1-2015 09:15:55 PM
For time zone, avoid both
Date
and Calendar
- If you are using JDK >= 8, use the new
java.time.*
framework. - If you are using JDK < 8, use Joda Time. (The new Java 8
java.time.*
framework is inspired by this library)
1. ZonedDateTime
Always use this new Java 8 java.time.ZonedDateTime
to represent a date and time containing time zone.
package com.mkyong.date;import java.time.LocalDateTime;import java.time.ZoneId;import java.time.ZonedDateTime;import java.time.format.DateTimeFormatter;public class ZonedDateTimeExample { private static final String DATE_FORMAT = "dd-M-yyyy hh:mm:ss a"; public static void main(String[] args) { String dateInString = "22-1-2015 10:15:55 AM"; LocalDateTime ldt = LocalDateTime.parse(dateInString, DateTimeFormatter.ofPattern(DATE_FORMAT)); ZoneId singaporeZoneId = ZoneId.of("Asia/Singapore"); System.out.println("TimeZone : " + singaporeZoneId); //LocalDateTime + ZoneId = ZonedDateTime ZonedDateTime asiaZonedDateTime = ldt.atZone(singaporeZoneId); System.out.println("Date (Singapore) : " + asiaZonedDateTime); ZoneId newYokZoneId = ZoneId.of("America/New_York"); System.out.println("TimeZone : " + newYokZoneId); ZonedDateTime nyDateTime = asiaZonedDateTime.withZoneSameInstant(newYokZoneId); System.out.println("Date (New York) : " + nyDateTime); DateTimeFormatter format = DateTimeFormatter.ofPattern(DATE_FORMAT); System.out.println("\n---DateTimeFormatter---"); System.out.println("Date (Singapore) : " + format.format(asiaZonedDateTime)); System.out.println("Date (New York) : " + format.format(nyDateTime)); }}
Output
TimeZone : Asia/SingaporeDate (Singapore) : 2015-01-22T10:15:55+08:00[Asia/Singapore]TimeZone : America/New_YorkDate (New York) : 2015-01-21T21:15:55-05:00[America/New_York]---DateTimeFormatter---Date (Singapore) : 22-1-2015 10:15:55 AMDate (New York) : 21-1-2015 09:15:55 PM
Refer to this ZonedDateTime tutorial for more time zone, custom offset and daylight saving time (DST) examples.
2. Date
The
java.util.Date
has no concept of time zone, and only represents the number of seconds passed since the Unix epoch time – 1970-01-01T00:00:00Z. But, if you print the Date object directly, the Date object will be always printed with the default system time zone. Check the Date.toString()
source code.2.1 Set a time zone to DateFormat
and format the java.util.Date
SimpleDateFormat sdfAmerica = new SimpleDateFormat("dd-M-yyyy hh:mm:ss a");sdfAmerica.setTimeZone(TimeZone.getTimeZone("America/New_York"));String sDateInAmerica = sdfAmerica.format(date);
2.2 Full example
package com.mkyong.date;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.TimeZone;public class DateExample { private static final String DATE_FORMAT = "dd-M-yyyy hh:mm:ss a"; public static void main(String[] args) throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT); String dateInString = "22-01-2015 10:15:55 AM"; Date date = formatter.parse(dateInString); TimeZone tz = TimeZone.getDefault(); // From TimeZone Asia/Singapore System.out.println("TimeZone : " + tz.getID() + " - " + tz.getDisplayName()); System.out.println("TimeZone : " + tz); System.out.println("Date (Singapore) : " + formatter.format(date)); // To TimeZone America/New_York SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE_FORMAT); TimeZone tzInAmerica = TimeZone.getTimeZone("America/New_York"); sdfAmerica.setTimeZone(tzInAmerica); String sDateInAmerica = sdfAmerica.format(date); // Convert to String first Date dateInAmerica = formatter.parse(sDateInAmerica); // Create a new Date object System.out.println("\nTimeZone : " + tzInAmerica.getID() + " - " + tzInAmerica.getDisplayName()); System.out.println("TimeZone : " + tzInAmerica); System.out.println("Date (New York) (String) : " + sDateInAmerica); System.out.println("Date (New York) (Object) : " + formatter.format(dateInAmerica)); }}
Output
TimeZone : Asia/Kuala_Lumpur - Malaysia TimeTimeZone : sun.util.calendar.ZoneInfo[id="Asia/Kuala_Lumpur",...]Date (Singapore) : 22-1-2015 10:15:55 AMTimeZone : America/New_York - Eastern Standard TimeTimeZone : sun.util.calendar.ZoneInfo[id="America/New_York",...]Date (New York) (String) : 21-1-2015 09:15:55 PMDate (New York) (Object) : 21-1-2015 09:15:55 PM
3. Calendar
3.1 A Calendar example to set a time zone :
Calendar calendar = new GregorianCalendar();calendar.setTime(date);calendar.setTimeZone(tzInAmerica);
A super common mistake is to get the java.util.Date
directly like this :
//Wrong, it will display 22-1-2015 10:15:55 AM, time is still in the system default time zone!Date dateInAmerican = calendar.getTime());
In the above example, no matter what time zone you set in the Calendar, the Date object will be always printed with the default system time zone. (Check the Date.toString()
source code)
3.2 The correct way should be using the DateFormat
to format it :
SimpleDateFormat sdfAmerica = new SimpleDateFormat("dd-M-yyyy hh:mm:ss a");TimeZone tzInAmerica = TimeZone.getTimeZone("America/New_York");sdfAmerica.setTimeZone(tzInAmerica);sdfAmerica.format(calendar.getTime())
or get the Date via calendar.get()
:
int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH); // Jan = 0, dec = 11int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);int hour = calendar.get(Calendar.HOUR); // 12 hour clockint hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clockint minute = calendar.get(Calendar.MINUTE);int second = calendar.get(Calendar.SECOND);int ampm = calendar.get(Calendar.AM_PM); //0 = AM , 1 = PM
3.3 Full example
package com.mkyong.date;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.TimeZone;public class CalendarExample { private static final String DATE_FORMAT = "dd-M-yyyy hh:mm:ss a"; public static void main(String[] args) throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT); String dateInString = "22-01-2015 10:15:55 AM"; Date date = formatter.parse(dateInString); TimeZone tz = TimeZone.getDefault(); // From TimeZone Asia/Singapore System.out.println("TimeZone : " + tz.getID() + " - " + tz.getDisplayName()); System.out.println("TimeZone : " + tz); System.out.println("Date (Singapore) : " + formatter.format(date)); // To TimeZone America/New_York SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE_FORMAT); TimeZone tzInAmerica = TimeZone.getTimeZone("America/New_York"); sdfAmerica.setTimeZone(tzInAmerica); Calendar calendar = new GregorianCalendar(); calendar.setTime(date); calendar.setTimeZone(tzInAmerica); System.out.println("\nTimeZone : " + tzInAmerica.getID() + " - " + tzInAmerica.getDisplayName()); System.out.println("TimeZone : " + tzInAmerica); //Wrong! It will print the date with the system default time zone System.out.println("Date (New York) (Wrong!): " + calendar.getTime()); //Correct! need formatter System.out.println("Date (New York) (Correct!) : " + sdfAmerica.format(calendar.getTime())); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); // Jan = 0, dec = 11 int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); int hour = calendar.get(Calendar.HOUR); // 12 hour clock int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); int ampm = calendar.get(Calendar.AM_PM); //0 = AM , 1 = PM //Correct System.out.println("\nyear \t\t: " + year); System.out.println("month \t\t: " + month + 1); System.out.println("dayOfMonth \t: " + dayOfMonth); System.out.println("hour \t\t: " + hour); System.out.println("minute \t\t: " + minute); System.out.println("second \t\t: " + second); System.out.println("ampm \t\t: " + ampm); }}
Output
TimeZone : Asia/Kuala_Lumpur - Malaysia TimeTimeZone : sun.util.calendar.ZoneInfo[id="Asia/Kuala_Lumpur",...]Date (Singapore) : 22-1-2015 10:15:55 AMTimeZone : America/New_York - Eastern Standard TimeTimeZone : sun.util.calendar.ZoneInfo[id="America/New_York",...]]Date (New York) (Wrong!): Thu Jan 22 10:15:55 MYT 2015Date (New York) (Correct!) : 21-1-2015 09:15:55 PMyear : 2015month : 01dayOfMonth : 21hour : 9minute : 15second : 55ampm : 1
4. Joda Time
4.1 A Joda Time example to set a time zone :
DateTime dt = new DateTime(date);DateTimeZone dtZone = DateTimeZone.forID("America/New_York");DateTime dtus = dt.withZone(dtZone);
Again, a common mistake is getting the Date directly like this, time zone will be lost.
//Output : 22-1-2015 10:15:55 AMDate dateInAmerica = dtus.toDate();
The correct way is converted to Joda LocalDateTime
first.
//Output : 21-1-2015 09:15:55 PMDate dateInAmerica = dtus.toLocalDateTime().toDate();
4.2 Full example
package com.mkyong.date;import org.joda.time.DateTime;import org.joda.time.DateTimeZone;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.TimeZone;public class JodaTimeExample { private static final String DATE_FORMAT = "dd-M-yyyy hh:mm:ss a"; public static void main(String[] args) throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT); String dateInString = "22-01-2015 10:15:55 AM"; Date date = formatter.parse(dateInString); TimeZone tz = TimeZone.getDefault(); // From TimeZone Asia/Singapore System.out.println("TimeZone : " + tz.getID() + " - " + tz.getDisplayName()); System.out.println("TimeZone : " + tz); System.out.println("Date (Singapore) : " + formatter.format(date)); // To TimeZone America/New_York SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE_FORMAT); DateTime dt = new DateTime(date); DateTimeZone dtZone = DateTimeZone.forID("America/New_York"); DateTime dtus = dt.withZone(dtZone); TimeZone tzInAmerica = dtZone.toTimeZone(); Date dateInAmerica = dtus.toLocalDateTime().toDate(); //Convert to LocalDateTime first sdfAmerica.setTimeZone(tzInAmerica); System.out.println("\nTimeZone : " + tzInAmerica.getID() + " - " + tzInAmerica.getDisplayName()); System.out.println("TimeZone : " + tzInAmerica); System.out.println("DateTimeZone : " + dtZone); System.out.println("DateTime : " + dtus); System.out.println("dateInAmerica (Formatter) : " + formatter.format(dateInAmerica)); System.out.println("dateInAmerica (Object) : " + dateInAmerica); }}
Output
TimeZone : Asia/Kuala_Lumpur - Malaysia TimeTimeZone : sun.util.calendar.ZoneInfo[id="Asia/Kuala_Lumpur",...]Date (Singapore) : 22-1-2015 10:15:55 AMTimeZone : America/New_York - Eastern Standard TimeTimeZone : sun.util.calendar.ZoneInfo[id="America/New_York",...]DateTimeZone : America/New_YorkDateTime : 2015-01-21T21:15:55.000-05:00dateInAmerica (Formatter) : 21-1-2015 09:15:55 PMdateInAmerica (Object) : Wed Jan 21 21:15:55 MYT 2015
http://www.mkyong.com/java/java-convert-date-and-time-between-timezone/
https://www.mkyong.com/java/java-display-list-of-timezone-with-gmt/
http://www.mkyong.com/java8/java-display-all-zoneid-and-its-utc-offset/
- Java – Convert date and time between timezone
- Java – Convert date and time between timezone
- java convert timestamp to date and time
- Groovy Convert between string and date
- Java Date and Time系列(六)- java.util.TimeZone介绍
- Simple trick to convert Date format with timezone in Java!
- Java String and Date convert
- Conver to date time between Chinese and Calendar
- Java Date, Time and Format
- 【Learning】Java Date and Time
- Java Date and Time Tutorial
- JAVA Current date and time
- Java Date and Time系列
- Joda Time - Java date and time API
- [Java] Convert Between the Two, String[] and ArrayList<String>
- JAVA date timezone 的用法
- convert between characters and values
- convert between RGB565 and RGB888
- SunnyAmy 面向对象设计七大原则
- 蓝牙系统结构
- Kylin元数据备份日志
- TCP流模式 与 UDP报文模式
- CString成员函数用法
- Java – Convert date and time between timezone
- React-native 之WebView的使用
- 高通fastmmi(ffbm)的使用
- Android OTA应用访问Thinkphp5 服务器
- Acticle 13:javascript特效小分享:鼠标移入移出出现弹框效果
- Android Studio编译时Error: app:transformClassesAndResourcesWithProguardFor 的解决
- storm从入门到精通 第五节 Trident
- Android性能测试之fps获取
- Can't connect to local MySQL server through socket '/tmp/mysql.sock'