Java如何解析这个时间戳:”20200926T221447Z”

huangapple go评论80阅读模式
英文:

Java how to parse this timestamp "20200926T221447Z"

问题

我尝试过的方法

public class Event {

    private SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmssZ");

    private Date date;
    private String summary;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public void setDate(String stringDate) throws ParseException {
        this.date = formatter.parse(stringDate);
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }
}

// 主函数

String x = "20200926T221447Z";
Event event = new Event();
event.setDate(x);

**我得到的异常信息**

Exception in thread "main" java.lang.IllegalArgumentException: 非法的模式字符 'T'
    at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:826)
    at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:634)
    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:605)
    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:580)
    at stuff.calendar.Event.<init>(Event.java:9)
    at stuff.calendar.App.main(App.java:34)

**问题** 我的 SimpleDateFormat 格式不正确吗我尝试过进行更改但都不起作用我应该使用另一个 Java API 来解析这种特定的时间格式吗
英文:

Things I've tried

  public class Event {
private SimpleDateFormat formatter = new SimpleDateFormat(&quot;yyyyMMddTHHmmssZ&quot;);
private Date date;
private String summary;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public void setDate(String stringDate) throws ParseException {
this.date = formatter.parse(stringDate);
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
}

// main

String x = &quot;20200926T221447Z&quot;
Event event = new Event();
event.setDate(x);

Exception I get

Exception in thread &quot;main&quot; java.lang.IllegalArgumentException: Illegal pattern character &#39;T&#39;
at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:826)
at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:634)
at java.text.SimpleDateFormat.&lt;init&gt;(SimpleDateFormat.java:605)
at java.text.SimpleDateFormat.&lt;init&gt;(SimpleDateFormat.java:580)
at stuff.calendar.Event.&lt;init&gt;(Event.java:9)
at stuff.calendar.App.main(App.java:34)

Question : Is my SimpleDateFormat incorrect? I've tried changing this around but none work. Is there another java API I should use to parse this particular time format?

答案1

得分: 4

当尝试将一个 String 解析为日期时间对象时,我倾向于使用 DateTimeFormatter 而不是 SimpleDateFormat

我使用以下代码片段成功将您的 String 解析为 ZonedDateTime 对象:

String x = "20200926T221447Z";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuMMdd'T'HHmmssX");
System.out.println(ZonedDateTime.parse(x, dtf));

输出结果:

2020-09-26T22:14:47Z
英文:

When attempting to parse a String into a date-time object, I tend to prefer using a DateTimeFormatter over SimpleDateFormat.

I was able to parse your String into a ZonedDateTime object with the following snippet:

String x = &quot;20200926T221447Z&quot;;
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(&quot;uuuuMMdd&#39;T&#39;HHmmssX&quot;);
System.out.println(ZonedDateTime.parse(x, dtf));

Output:

2020-09-26T22:14:47Z

答案2

得分: 2

formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");

另外要考虑到 Z 具有特殊的解析含义(https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)。因此,如果您确实想要包含时区信息,您需要以 RFC-822 格式在输入中指定它。

SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmssZ");                                      
Date d = formatter.parse("20200926T221447-0800");
System.out.println(d);    

如果您的时区与 ISO8601(就像您的示例一样),请改用 X

示例(我认为这是您想要的):

SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmssX");
Date d = formatter.parse("20200926T221447Z");                                                                 
System.out.println(d);    
英文:

You have to put quotes around the string literals that should not be interpreted:

formatter = new SimpleDateFormat(&quot;yyyyMMdd&#39;T&#39;HHmmss&#39;Z&#39;&quot;);

Also consider that Z has a special parsing meaning (https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html). So if you actually want to include a timezone, you need to specify it in the input in RFC-822 format.

SimpleDateFormat formatter = new SimpleDateFormat(&quot;yyyyMMdd&#39;T&#39;HHmmssZ&quot;);                                      
Date d = formatter.parse(&quot;20200926T221447-0800&quot;);
System.out.println(d);    

Use X instead if your timezone is ISO8601 like your example:

Example (which I think is what you want):

SimpleDateFormat formatter = new SimpleDateFormat(&quot;yyyyMMdd&#39;T&#39;HHmmssX&quot;);
Date d = formatter.parse(&quot;20200926T221447Z&quot;);                                                                 
System.out.println(d);    

huangapple
  • 本文由 发表于 2020年9月27日 22:55:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64089814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定