-1

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?

HMdeveloper
  • 2,772
  • 9
  • 45
  • 74
  • What exactly isn't working? What errors are you getting? What output are you getting, and what output are you expecting? – forgivenson Jan 06 '15 at 19:26
  • 3
    Your input string is a fully specified point in time including time zone, so why do you use `LocalDateTime` for parsing? – isnot2bad Jan 06 '15 at 19:28
  • You seem to be testing whether October 19, 2014 converts to 2014-10-21. The 19th of the month is not the 21st of the month. Also, as @forgivenson points out, you should probably be using a `ZonedDateTime` instead of a `LocalDateTime.` – David Conrad Jan 06 '15 at 19:54
  • Here is the error that I get Exception in thread "main" java.time.DateTimeException: Unable to extract value: class java.time.LocalDateTime at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:282) at java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser.format(DateTimeFormatterBuilder.java:3685) at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182) at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1744) at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1718) – HMdeveloper Jan 06 '15 at 19:57

2 Answers2

1

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
1

I hope you are using JDK8 and your imports are the following ones:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

Then the following code :

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {

    public static void main(String[] args) {
        // 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
    }
}

Prints as expected :

2014/10/19
2014-10-19

If you want to print 2014/10/21 just change the str to :

String str = "Tue Oct 21 01:00:00 ADT 2014";
panagdu
  • 2,133
  • 1
  • 21
  • 36