解析日期java

Adi*_*dil 2 java format date

如何可以解析此日期格式Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest)此日期格式05-14-2010我的意思mm-dd-yyyy

它告诉我这个错误:

java.text.ParseException: Unparseable date: "Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest)"

编辑

SimpleDateFormat formatter = new SimpleDateFormat("M-d-yyyy");
newFirstDate = formatter.parse(""+vo.getFirstDate());  //here the error
Run Code Online (Sandbox Code Playgroud)

提前致谢!

Mar*_*nik 5

此代码首先调整字符串,然后继续解析它.它尊重时区,只是删除"GMT",因为这是SimpleDateFormat喜欢它.

final String date = "Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest)"
  .replaceFirst("GMT", "");
System.out.println(
    new SimpleDateFormat("MM-dd-yyyy").format(
        new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z").parse(date)));
Run Code Online (Sandbox Code Playgroud)

打印:

05-14-2010
Run Code Online (Sandbox Code Playgroud)

请记住,输出也是时区敏感的.输入字符串定义的瞬间在我的时区被解释为属于程序打印的日期.如果你只需要将"2010年5月14日"改造成"05-14-2010",那就是另一个故事而且SimpleDateFormat不太适合.该JodaTime库将更加清晰地处理这种情况.