0

I need to convert date (2011-Jan-01) to any of the simpledateformats.

The eclipse designer uses Java 7.

    String pDate = obj.getJSONArray("product").getJSONObject(i).getString("createdDate");
    //"2011-Jan-01" - date format.
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
    try {
        Date fDate = format.parse(pDate);
        System.out.println("jsonDate: " + fDate);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("pDate: " + pDate);//"2011-Jan-01"


            errors:
            error:java.text.ParseException:Unparseable date:"2011-Jan-01"
            error: at java.text.DateFormat.parse(DateFormat.java:348)

I'm getting errors above.

3 Answers3

2

Your parsing format should not include the time (because your input is only the date), and you need another format call to produce your desired output. Something like,

String pDate = "2011-Jan-01";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MMM-dd");
try {
    Date fDate = format.parse(pDate);
    System.out.println(new SimpleDateFormat("dd-MM-yyyy").format(fDate));
} catch (ParseException e) {
    e.printStackTrace();
}

which outputs

01-01-2011
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

java.time and ThreeTen Backport

This works on Java 7 (details below):

    DateTimeFormatter jsonDateFormatter = DateTimeFormatter.ofPattern("uuuu-MMM-dd", Locale.ENGLISH);
    DateTimeFormatter outputDateFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");

    String pDate = "2011-Jan-01";
    LocalDate fDate = LocalDate.parse(pDate, jsonDateFormatter);
    pDate = fDate.format(outputDateFormatter);

    System.out.println("Formatted date: " + pDate);

Output from the snippet is:

Formatted date: 01-01-2011

Unless you have strong reasons not to, I recommend that you use one of Java’s built-in formats for your user’s locale rather than hardcoding an output format. For example:

    DateTimeFormatter outputDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
            .withLocale(Locale.UK);

Now the output is:

Formatted date: 01-Jan-2011

Question: Why java.time?

The old datetime classes that you tried to use in the question, Date and SimpleDateFormat are poorly designed, the latter notoriously troublesome. Fortunately they are also long outdated. java.time, the modern Java date and time API, is so much nicer to work with.

Question: Can I use java.time on Java 7? How?

Yes, java.time works nicely on Java 7. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Change SimpleDateFormat format = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss"); to SimpleDateFormat format = new SimpleDateFormat("yyyy-MMM-dd");

phuchoangmai
  • 305
  • 1
  • 5
  • 15