-2

I am trying to convert String date of format "yyyy-MM-dd HH:mm:ss.SSS" to String "MM/dd/yyyy"

My code goes like this:

SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String dateInString = "2015-07-16 17:07:21";
try {
    Date date = formatter.parse(dateInString);
    System.out.println(formatter.format(date));
} catch (ParseException e) {
    e.printStackTrace();
}

I am getting error:

java.text.ParseException: Unparseable date: "2015-07-16 17:07:21" at java.base/java.text.DateFormat.parse(DateFormat.java:395) at MyClass.main(MyClass.java:10)

Please let me know how should I fix this. I know this might be a duplicate but i didn't find any luck. Thanks.

Aakash Goplani
  • 1,150
  • 1
  • 19
  • 36
  • 1
    Your date format doesn't match your String at all . – Arnaud Jun 13 '18 at 12:20
  • You cant use a format to change a date string you have. You have to manually change the date string to what you want. This means where ever you're getting your date string from needs to give it to you differently, or you need to do some clever string manupulation to get it to the format you want – Kwright02 Jun 13 '18 at 12:21
  • you need 2 SimpleDateFormat. One for input and one for output. – Fredo Jun 13 '18 at 12:21
  • 3
    You'd need two SimpleDateFormatters. One to parse the `yyyy-MM-dd HH:mm:ss.SSS` format, and one to format it to the `MM/dd/yyyy` format. Currently you try to both parse and format it with the same. – Kevin Cruijssen Jun 13 '18 at 12:21
  • If @KevinCruijssen is correct in what you wish to do, please check the link jhamon sent above – Kwright02 Jun 13 '18 at 12:23
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). `System.out.println(LocalDateTime.parse("2015-07-16 17:07:21", DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss")).format(DateTimeFormatter.ofPattern("MM/dd/uuuu")));` – Ole V.V. Jun 13 '18 at 12:36

2 Answers2

3

Try this

String dateInString = "2015-07-16 17:07:21"
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-ss HH:mm:ss");
SimpleDateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy");
try {
    Date date = inputFormat.parse(dateInString);
    System.out.println("Date ->" + outputFormat.format(date));
} catch (ParseException e) {
    e.printStackTrace();
}
Sree...
  • 333
  • 2
  • 7
1

First convert your String in a date as the follow:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateInString = "2015-07-16 17:07:21";
try {
    Date date = formatter.parse(dateInString);
    //And then apply the pattern
    formatter.applyPattern("MM/dd/yyyy");
    System.out.println(formatter.format(date));
} catch (ParseException e) {
    e.printStackTrace();
}
Allan Braga
  • 460
  • 5
  • 19
  • Your code currently doesn't work. Either the `.SSS` should be removed from the formatter, or the milliseconds should be added to the dateInString. – Kevin Cruijssen Jun 13 '18 at 12:28
  • You are right, thanks, i just copy the patter that guy was using. – Allan Braga Jun 13 '18 at 12:30
  • 1
    I know, I did the same in my now deleted answer (you were 1 min faster - upvoted for that). ;) Noticed it when I was creating [a TIO-link](https://tio.run/##fU/NToQwEL7zFBNOcKALmygJZG9q1gMnvBkPta1rWQqkHdYlZp8dS8ENUeMkzbTz/XUqeqJRxY/jKFXXaoTKDgiKM5LSTmpxR1E8tFpRzL01pUdZkwnMPVZTY6Cgsvn0ALr@tZYMDFK07dRKDspCQYlaNofnF6D6YEJLnMgAP1Ogo9oIDTtoxMcvNPAHW1FRRJzDfp8plRnjh/nfVm@u4T9uRbHhfDN5Xk3cN4Fb0mOzPHbgb@PkJorTKLmFJM3iNNsm/ixAPSyrTDWZO7EVzZsQ14K14RLl4gaDQpG2R9JZCOsmuP6azDcnDRfNBRhF9g7B/ZmJDmXbgAhX@WK2KZGy45OmTATfQm86l3H8Ag) to add to my answer and it gave a parse error. – Kevin Cruijssen Jun 13 '18 at 12:34
  • hahaha, thanks very much. Cheers. – Allan Braga Jun 13 '18 at 12:43