将任何时区的日期和时间转换为UTC区域 [英] convert date and time in any timezone to UTC zone
问题描述
-
这是我的约会日期15-05-2014 00:00:00
this is my date " 15-05-2014 00:00:00 "
如何转换IST到UTC ie(到14-05-2014 18:30:00)
how to convert IST to UTC i.e( to 14-05-2014 18:30:00)
我的代码是
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("IST")); //here set timezone
System.out.println(formatter.format(date));
formatter.setTimeZone(TimeZone.getTimeZone("UTC")); //static UTC timezone
System.out.println(formatter.format(date));
String str = formatter.format(date);
Date date1 = formatter.parse(str);
System.out.println(date1.toString());
-
如果用户从任何区域输入相同的日期,那么将获得UTC时间(例如:从澳大利亚然后15-05-2014 00:00:00到14-05-2014 16:00:00)
if user enter same date from any zone then will get UTC time(ex: from Australia then 15-05-2014 00:00:00 to 14-05-2014 16:00:00)
请任何建议。
推荐答案
您无法将该日期值转换为其他时区或UTC。类型
java.util.Date
没有任何内部时区状态,只能通过规范引用UTC,其方式无法通过user(仅计算UTC时区中自UNIX时代以来的毫秒数,而不考虑leapseconds)。You cannot "convert that date values" to other timezones or UTC. The type
java.util.Date
does not have any internal timezone state and only refers to UTC by spec in a way which cannot be changed by user (just counting the milliseconds since UNIX epoch in UTC timezone leaving aside leapseconds).但您可以转换<$ c $的格式化字符串表示形式c> java.util.Date 到另一个时区。我更喜欢使用两种不同的格式化程序,每个时区(和模式)一个。我也更喜欢在你的情况下使用亚洲/加尔各答,因为它将普遍起作用(IST也可能是以色列标准时间,在以色列将有不同的解释):
But you can convert the formatted String-representation of a
java.util.Date
to another timezone. I prefer to use two different formatters, one per timezone (and pattern). I also prefer to use "Asia/Kolkata" in your case because then it will universally works (IST could also be "Israel Standard Time" which will be interpreted differently in Israel):DateFormat formatterIST = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); formatterIST.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); // better than using IST Date date = formatterIST.parse("15-05-2014 00:00:00"); System.out.println(formatterIST.format(date)); // output: 15-05-2014 00:00:00 DateFormat formatterUTC = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); formatterUTC.setTimeZone(TimeZone.getTimeZone("UTC")); // UTC timezone System.out.println(formatterUTC.format(date)); // output: 14-05-2014 18:30:00 // output in system timezone using pattern "EEE MMM dd HH:mm:ss zzz yyyy" System.out.println(date.toString()); // output in my timezone: Wed May 14 20:30:00 CEST 2014
这篇关于将任何时区的日期和时间转换为UTC区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!