简体   繁体   English

如何在Java中将Formatted String解析为Date对象

[英]How to parse a Formatted String into Date object in Java

In Java, How to parse "Wed, 05 Jun 2013 00:48:12 GMT" into a Date object, then print the date object out in the same format. 在Java中,如何将“Wed,05 Jun 2013 00:48:12 GMT”解析为Date对象,然后以相同的格式打印日期对象。

I have tried this: 我试过这个:

String dStr= "Wed, 05 Jun 2013 00:48:12 GMT"; 
SimpleDateFormat ft =  new SimpleDateFormat ("E, dd MMM yyyy hh:mm:ss ZZZ"); 
Date t = ft.parse(dStr); 
System.out.println(t); 
System.out.println(ft.format(t));

The result is 结果是

Wed Jun 05 10:48:12 EST 2013
Wed, 05 Jun 2013 10:48:12 +1000 

Thanks in advance: 提前致谢:

You don't have an error, both: Wed, 05 Jun 2013 00:48:12 GMT and Wed, 05 Jun 2013 00:48:12 GMT represent the same time, the first one is GMT (Grenweech) and the second one is the same time in Australia (EST), you just need to configure your time zone properly. 您没有错误,两者都有: Wed, 05 Jun 2013 00:48:12 GMTWed, 05 Jun 2013 00:48:12 GMT代表同一时间,第一个是GMT(Grenweech),第二个是GMT与澳大利亚(EST)同时,您只需要正确配置您的时区。

If you want to print the same date in GMT, add this line to your code: 如果您想在GMT中打印相同的日期,请将此行添加到您的代码中:

ft.setTimeZone(TimeZone.getTimeZone("GMT"));

Now, if you also want your date to be printed with the time-zone as "GMT" instead of "+0000", follow @HewWolff answer (use zzz instead of ZZZ) 现在,如果您还希望使用时区打印日期为“GMT”而不是“+0000”,请按@HewWolff回答 (使用zzz而不是ZZZ)

It looks like you want zzz rather than ZZZ . 看起来你想要zzz而不是ZZZ That should help you read/write your time zone as a code rather than a number. 这应该可以帮助您将时区作为代码而不是数字来读/写。

This solves your problem: 这解决了您的问题:

import java.text.*;
import java.util.*;

public class DateIt{
    public static void main(String [] args) throws Exception{
        String dStr= "Wed, 05 Jun 2013 00:48:12 GMT"; 
        SimpleDateFormat ft =  new SimpleDateFormat ("E, dd MMM yyyy HH:mm:ss z"); 
        Date t = ft.parse(dStr); 
        TimeZone gmt = TimeZone.getTimeZone("England/London");
        ft.setTimeZone(gmt);
        System.out.println(t); 
        System.out.println(ft.format(t));
        }
}

Ugh - I've run into this problem before. 呃 - 我之前遇到过这个问题。

The SimpleDateFormat.parse(String) method (I suspect) uses an instance of Calendar . SimpleDateFormat.parse(String)方法(我怀疑)使用Calendar的实例。 This matters for two reasons: 这有两个原因:

  1. You don't know which flavor of Calendar you're dealing with (Gregorian? Mayan? Vegan?). 你不知道你正在处理哪种日历(Gregorian?Mayan?Vegan?)。
  2. Calendar initializes its fields with default values that you don't necessarily want set. Calendar使用您不一定要设置的默认值初始化其字段。 When the SimpleDateFormat calls getTime() on the Calendar instance it created to return a Date, that Date will have default field values that aren't indicated by the string you submitted to begin with. 当SimpleDateFormat在它创建的Calendar实例上调用getTime()以返回Date时,该Date将具有默认字段值,这些值不是由您提交的字符串开头指示的。

These default values are having an impact when the ft.format(t) call is made. 在进行ft.format(t)调用时,这些默认值会产生影响。 The only way to solve this problem that I've found is to work with the Calendar class directly (which is entirely non-trivial, BTW.) I'll try to provide a code sample later when I've got more time. 我发现解决这个问题的唯一方法就是直接使用Calendar类(这完全不重要,BTW。)我会在以后有更多时间时尝试提供代码示例。 In the mean time, look at javafx.util.converter.DateStringConverter . 与此同时,请查看javafx.util.converter.DateStringConverter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM