如何转换“ 2012年9月21日星期五15:23:59 CEST”到“ 2012-09-21T15:23:59”在Java中? [英] How to convert "Fri Sep 21 15:23:59 CEST 2012" to "2012-09-21T15:23:59" in Java?

查看:59
本文介绍了如何转换“ 2012年9月21日星期五15:23:59 CEST”到“ 2012-09-21T15:23:59”在Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Java中的日期字符串 Fri Sep 21 15:23:59 CEST 2012转换为 2012-09-21T15:23:59。
我用SimpleDateFormat和以下代码尝试了此操作:

I want to convert the date string "Fri Sep 21 15:23:59 CEST 2012" to "2012-09-21T15:23:59" in Java. I tried this with SimpleDateFormat and the following code:

try {
    String dateString = "Fri Sep 21 15:23:59 CEST 2012";
    SimpleDateFormat input = new SimpleDateFormat("EEE MM dd HH:mm:ss z YYYY");
    SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

    Date date = input.parse(dateString);
    System.out.println(output.format(date));            
} catch (ParseException e) {
    e.printStackTrace();
}

但是输入解析给我一个java.text.ParseException。我已经在 http://docs.oracle中阅读了文档.com / javase / 1.4.2 / docs / api / java / text / SimpleDateFormat.html ,但找不到该错误。

But the input parsing gives me a java.text.ParseException. I have read the documetation at http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html, but I am not able to find the error.

哪种格式的字符串可以解决此字符串的输入解析?

Which format string solves the input parsing of this string?

推荐答案

使用

"EEE MMM dd HH:mm:ss z YYYY"

注意 3 MMM

这篇关于如何转换“ 2012年9月21日星期五15:23:59 CEST”到“ 2012-09-21T15:23:59”在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆