DateFormat conversion problem in java?

前端 未结 1 966
有刺的猬
有刺的猬 2020-12-18 13:23

my input String is : 2010-03-24T17:28:50.000Z

output pattern is like:

DateFormat formatter1 = new SimpleDateFormat(\"EEE. MMM. d. yyyy\");

1条回答
  •  有刺的猬
    2020-12-18 13:37

    The problem is in this part:

    new Date("2010-03-24T17:28:50.000Z")
    

    Apparently it doesn't accept dates/times in that format.

    You shouldn't be using that constructor anyway - create an appropriate formatter to parse that particular format, and then parse it with that.

    Alternatively, use Joda Time to start with, and avoid using DateFormat completely. I don't know if you can use Joda Time from Android, mind you... and it's fairly large.

    EDIT: To spell it out explicitly:

    String inputText = "2010-03-24T17:28:50.000Z";
    // "Z" appears not to be supported for some reason.
    DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    DateFormat outputFormat = new SimpleDateFormat("EEE. MMM. d. yyyy");
    Date parsed = inputFormat.parse(inputText);
    String outputText = outputFormat.format(parsed);
    
    // Output is Wed. Mar. 24 2010 on my box
    

    0 讨论(0)
提交回复
热议问题