-1

I have a String date "30 Aug 2019". I want to format in "2019-08-30'. I am trying with following code. It is not working.

String input = "30 Aug 2019"; Date date = sdf.parse(input);

       // Date to String:
      String strDate = sdf.format(date);
      System.out.println(strDate);

I am getting an error as Exception in thread "main" java.text.ParseException: Unparseable date: "30 Aug 2019"

Please help me, how to go ahead ?

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
Shashank
  • 9
  • 3
  • 2
    FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 30 '19 at 21:14
  • 1
    Best way to go ahead would be [to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and post it in your question. With the information you have posted this far, we cannot tell what’s wrong and thus cannot guide you. The exception you are mentioning doesn’t come from the piece of code you have posted. – Ole V.V. Aug 31 '19 at 17:28

2 Answers2

2

tl;dr

You are using terrible date-time classes that were outmoded years ago by the adoption of JSR 310. Use java.time.LocalDate instead.

And you neglected to specify a formatting pattern to match your input string. We do so here using the DateTimeFormatter class.

LocalDate
.parse(
    "30 Aug 2019" ,
    DateTimeFormatter.ofPattern( "d MMM uuuu" ).withLocale( Locale.US )
)
.toString()

2019-08-30

java.time

The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.

Specify a Locale on your DateTimeFormatter to determine the human language and cultural norms needed for translating name of month, and such.

String input = "30 Aug 2019" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "d MMM uuuu" ).withLocale( Locale.US ) ;
LocalDate ld = LocalDate.parse( input , f ) ;

See this code run live at IdeOne.com.

ld.toString(): 2019-08-30


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 218,480
  • 72
  • 657
  • 915
0

Although you should use java.time classes, to answer your question, you need 2 SimpleDateFormats; one for parsing and one for formatting:

SimpleDateFormat sdfIn = new SimpleDateFormat("dd MMM yyyy");
SimpleDateFormat sdfOut = new SimpleDateFormat("yyyy-MM-dd");

Date date = sdfIn.parse("30 Aug 2019");
String strDate = sdfOut.format(date);

System.out.println(strDate); // 2019-08-30
Bohemian
  • 365,064
  • 84
  • 522
  • 658