Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a date with this format: Sun Oct 19 01:00:00 ADT 2014

and I need to convert it to the following format but I cannot,

Here is my code:

    // The test string
    String str = "Sun Oct 19 01:00:00 ADT 2014";

    // Formatter for the input date
    final DateTimeFormatter inputFormat = 
    DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");

   // The parsed date
   final LocalDateTime parsed = LocalDateTime.parse(str, inputFormat);

       // The output format(s). Specify the one you need
    final DateTimeFormatter outputFormat1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
     final DateTimeFormatter outputFormat2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");

  // Print
    System.out.println(outputFormat1.format(parsed)); // -> 2014/10/21
      System.out.println(outputFormat2.format(parsed)); // -> 2014-10-21

But it is not working at all and I am stuck with this from morning and really do not know how to solve it, Does anyone have any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
147 views
Welcome To Ask or Share your Answers For Others

1 Answer

What's wrong using this code. It works fine with 19-th

    // The test string
    String str = "Sun Oct 19 01:00:00 ADT 2014";

    // Formatter for the input date
    final DateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
    // The parsed date
    final Date parsed = inputFormat.parse(str);

    // The output format(s). Specify the one you need
    final DateFormat outputFormat1 = new SimpleDateFormat("yyyy/MM/dd");
    final DateFormat outputFormat2 = new SimpleDateFormat("yyyy-MM-dd");

    // Print
    System.out.println(outputFormat1.format(parsed)); // -> 2014/10/21
    System.out.println(outputFormat2.format(parsed)); // -> 2014-10-21

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...