-2

Here is my code,

SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date dft  = (Date) format.parse("16-MAY-2018 09:30:22:000");

I am getting below exception

java.text.ParseException: Unparseable date: "16-MAY-2018 09:30:22"

What's to be used to parse milliseconds?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    Your pattern says: `MM`, your String has `MAY`. If you have a very close look, you can see that this does not exactly match. `16-05-2018 09:30:22` is the String you need for your pattern. – Ben May 03 '18 at 07:57
  • 1
    Reading [the documentation](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) reveals that "Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number." – Malte Hartwig May 03 '18 at 07:58
  • @FedericoklezCulloca `MM` *does* imply that the month should be a number. "If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number." So for `MM`, the number of pattern letters is less than 3, so the month is interpreted as a number. – khelwood May 03 '18 at 08:04
  • 1
    Also what no answer mentions to this point is that the parse will always fail if your `Locale` is not `Locale.English`or `Locale.English` is specified as second parameter for the `SimpleDateFormat`'s constructor. – Ben May 03 '18 at 08:07
  • Any particular reason why you are using the long outdated `Date` and `SimpleDateFormat` classes? I recommend you forget about them and instead use [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). It is so much nicer to work with. – Ole V.V. May 03 '18 at 20:20

5 Answers5

3

The pattern should be MMM because there are three characters in the month.

You should also prefer java.time classes to the ones you're currently using if you're on Java 8 or above:

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
    .appendPattern("dd-MMM-yyyy")
    .appendLiteral(' ')
    .append(ISO_LOCAL_TIME)
    .toFormatter();

LocalDateTime timestamp = LocalDateTime.parse("16-May-2018 09:30:22", formatter);
Michael
  • 41,989
  • 11
  • 82
  • 128
  • What about milliseconds ? I am not able to prase milliseconds – zyan Billings May 03 '18 at 12:27
  • 1
    @zyanBillings `ISO_LOCAL_TIME` already includes an optional decimal point, with up to 9 digits for fractions of a second. – Michael May 03 '18 at 12:30
  • `SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS"); java.util.Date d1 =(java.util.Date)format.parse("03-MAY-2018 08:00:00:000 AM"); timeFormat = "dd-MMM-yyyy HH:mm:ss.SSS"; ` I am trying like below and getting same exception – zyan Billings May 03 '18 at 12:46
  • Have you ignored my advice about `java.time` or are you using <= Java 7? – Michael May 03 '18 at 12:48
  • 1
    @zyanBillings No big problem. `java.time` has been backported. All you need for Java 6 and 7 is [the ThreeTen Backport](http://www.threeten.org/threetenbp/). Enjoy. – Ole V.V. May 03 '18 at 20:22
  • 2
    To parse uppercase `MAY` add a call to `parseCaseInsensitive` before the call to `appendPattern`. – Ole V.V. May 03 '18 at 20:25
  • 1
    Given that your month abbreviations are in English you should also specify an English-speaking locale. For example `.toFormatter(Locale.ENGLISH)`. – Ole V.V. May 04 '18 at 04:09
0

Use this pattern: dd-MMM-yyyy HH:mm:ss

Nitin Singhal
  • 591
  • 5
  • 6
0

"16-MAY-2018 09:30:22" is not parsable with that time format. If you want to parse that you have to change date format to "dd-MMM-yyyy HH:mm:ss". The double M is only for numbered months (so should be 05 for May).

Check the SimpleDateFormat javadoc for more details: here

Lorelorelore
  • 3,335
  • 8
  • 29
  • 40
0
Date dft  = (Date) format.parse("16-05-2018 09:30:22");

OR change it to

SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
piy26
  • 1,574
  • 11
  • 21
  • After this I am getting below exception >java.util.Date cannot be cast to java.sql.Date – zyan Billings May 03 '18 at 08:06
  • Please change your import statement to `import java.util.Date;` – piy26 May 03 '18 at 08:07
  • Date class is provided by both `java.util.Date` and by `java.sql.Date`. You need to use the one from util package. That's the right one to use here. – piy26 May 03 '18 at 08:08
0

you are using dd-MM-yyyy HH:mm:ss, so parsable is

16-05-2018 09:30:22

and if you want 16-MAY-2018 09:30:22 then use

dd-MMM-yyyy HH:mm:ss