Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

java - converting date from one format to another does not work

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...