英文:
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("yyyyMMddTHHmmssZ");
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 = "20200926T221447Z"
Event event = new Event();
event.setDate(x);
Exception I get
Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character '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)
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 = "20200926T221447Z";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuMMdd'T'HHmmssX");
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("yyyyMMdd'T'HHmmss'Z'");
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("yyyyMMdd'T'HHmmssZ");
Date d = formatter.parse("20200926T221447-0800");
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("yyyyMMdd'T'HHmmssX");
Date d = formatter.parse("20200926T221447Z");
System.out.println(d);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论