英文:
How to convert timezones from given string
问题
SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy'T'hh:mm:ss'Z'");
SimpleDateFormat format2 = new SimpleDateFormat("M/d/yy h:mm a");
format1.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = format1.parse("06/17/2008T13:53:23Z");
System.out.println(date);
format2.setTimeZone(TimeZone.getTimeZone("America/New_York")); // Use the correct time zone ID for EDT
System.out.println(format2.format(date));
英文:
I have a given string 06/17/2008T13:53:23Z
, I want to convert that to EDT
time zone. So my output will be 6/17/08 9:53 AM EDT
.
Below is my Java Code, it is giving wrong output:
SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy'T'hh:mm:ss'Z'");
SimpleDateFormat format2 = new SimpleDateFormat("M/d/yy hh:mm a");
format1.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = format1.parse("06/17/2008T13:53:23Z");
System.out.println(date);
format2.setTimeZone(TimeZone.getTimeZone("EDT"));
System.out.println(format2.format(date));
It gives output as :
Tue Jun 17 19:23:23 IST 2008
6/17/08 01:53 PM
Here the output time as 01:53 PM
instead of 9:53 AM EDT
How to fix this issue?
答案1
得分: 2
你的解析模式使用了 hh
(适用于12小时制),而你的日期时间字符串中的时间是24小时制,所以你需要使用 HH
。另外,你应避免使用三个字母的时区名称。正如你已经预期的那样,EDT 的时区偏移为 -4 小时,你可以在 SimpleDateFormat
中将其表示为 GMT-4
。
尽管时区偏移是以数字(小时、分钟或秒)表示的,但时区是以表示时区名称的字符串形式(例如 America/New_York
)表示的。时区和时区偏移之间的关系是多对一,即多个时区可以具有相同的时区偏移。
请注意,java.util
日期时间类已经过时且容易出错,同样的情况也适用于它们的格式化API,即 SimpleDateFormat
。我建议你完全停止使用它们,并切换到现代日期时间API。在**教程: 日期时间**中可以了解有关现代日期时间API的更多信息。
如果你在进行 Android 项目,并且你的 Android API 版本仍不符合 Java 8,可以查看通过 desugaring 使用的 Java 8+ API以及在 Android 项目中使用 ThreeTenABP 的方法。
使用现代日期时间API:
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String str = "06/17/2008T13:53:23Z";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/u'T'H:m:s'z'");
ZonedDateTime zdt = ZonedDateTime.parse(str, formatter);
// 转换为东部时间
ZonedDateTime zdtET = zdt.withZoneSameInstant(ZoneId.of("America/New_York"));
// ZonedDateTime zdtET = zdt.withZoneSameInstant(ZoneOffset.ofHours(-4));// 或者使用这种方式
// 以默认格式打印,即 ZonedDateTime#toString
System.out.println(zdtET);
// 以自定义格式打印
System.out.println(zdtET.format(DateTimeFormatter.ofPattern("MM/dd/uuuu'T'HH:mm:ss zzzz")));
System.out.println(zdtET.format(DateTimeFormatter.ofPattern("M/d/uu HH:mm:ss a")));
}
}
输出:
2008-06-17T09:53:23-04:00[America/New_York]
06/17/2008T09:53:23 Eastern Daylight Time
6/17/08 09:53:23 am
注意: 如果你使用 ZoneOffset.ofHours(-4)
(在上面的代码中已注释),你将无法在输出中得到时区名称(例如 America/New_York
),因为正如前面所述,许多时区可以具有相同的时区偏移,并且没有默认的时区用于某个时区偏移。
使用传统API:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
String str = "06/17/2008T13:53:23Z";
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy'T'HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
Date date = formatter.parse(str);
// 转换为东部时间
SimpleDateFormat sdfOutput = new SimpleDateFormat("M/d/yy hh:mm a");
sdfOutput.setTimeZone(TimeZone.getTimeZone("America/New_York"));
// sdfOutput.setTimeZone(TimeZone.getTimeZone("GMT-4"));// 或者使用这种方式
System.out.println(sdfOutput.format(date));
}
}
输出:
6/17/08 09:53 am
英文:
Your parsing pattern uses hh
(which is for 12-hour format) whereas the time in your date-time string is in 24-hour format for which you need to use HH
. Secondly, you should avoid using the three-letter time-zone name. As you have already expected, EDT has a Zone-Offset of -4
hours and you can use this as GMT-4
with SimpleDateFormat
.
While a Zone-Offset is expressed in terms of numbers (hours, minutes or seconds), a timezone is expressed as a string representing the name (e.g. America/New_York
) of the timezone. The relation between timezone and Zone-Offset is many-to-one i.e. many timezones can have the same Zone-Offset.
Note that java.util
date-time classes are outdated and error-prone and so is their formatting API, SimpleDateFormat
. I suggest you should stop using them completely and switch to the modern date-time API. Learn more about the modern date-time API at Trail: Date Time.
If you are doing it for your Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Using the modern date-time API:
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String str = "06/17/2008T13:53:23Z";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/u'T'H:m:sz");
ZonedDateTime zdt = ZonedDateTime.parse(str, formatter);
// Convert to Eastern Time
ZonedDateTime zdtET = zdt.withZoneSameInstant(ZoneId.of("America/New_York"));
// ZonedDateTime zdtET = zdt.withZoneSameInstant(ZoneOffset.ofHours(-4));// Or this
// Print in default format i.e. ZonedDateTime#toString
System.out.println(zdtET);
// Print in custom formats
System.out.println(zdtET.format(DateTimeFormatter.ofPattern("MM/dd/uuuu'T'HH:mm:ss zzzz")));
System.out.println(zdtET.format(DateTimeFormatter.ofPattern("M/d/uu hh:mm:ss a")));
}
}
Output:
2008-06-17T09:53:23-04:00[America/New_York]
06/17/2008T09:53:23 Eastern Daylight Time
6/17/08 09:53:23 am
Note: If you use ZoneOffset.ofHours(-4)
[commented in the code above], you can not get the name of timezone (e.g. America/New_York
) in the output because, as explained earlier, many timezones can have the same Zone-Offset and there is no default timezone for a Zone-Offset.
Using the legacy API:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
String str = "06/17/2008T13:53:23Z";
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy'T'HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
Date date = formatter.parse(str);
// Convert to Eastern Time
SimpleDateFormat sdfOutput = new SimpleDateFormat("M/d/yy hh:mm a");
sdfOutput.setTimeZone(TimeZone.getTimeZone("America/New_York"));
// sdfOutput.setTimeZone(TimeZone.getTimeZone("GMT-4"));// Or this
System.out.println(sdfOutput.format(date));
}
}
Output:
6/17/08 09:53 am
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论