0

How can I convert a Java string to a date in this format - "31 Jan 2022". I've tried the code below.

Date sDate;
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
sDate = format.parse("20/12/2002");
return sDate.toString();  

It's giving an output in this format "Fri Jan 30 00:00:00" which is not what I want

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Felix
  • 1
  • 1
  • You should be getting the error [java.text.ParseException: Unparseable date: "31 Jan 2022"](https://ideone.com/dAGTnJ). `"31 Jan 2022"` is not in the format `"dd/MM/yyyy"`. Are you sure that your input is what you say it is? – OH GOD SPIDERS Aug 01 '22 at 11:10
  • 2
    Please **do not** use `Date` and `SimpleDateFormat`, as they are obsolete classes. They're also troublesome. You are better off using classes from the `java.time` package. In your case, where you only seem to need a date without time and timezone, you may want to use `LocalDate`. – MC Emperor Aug 01 '22 at 11:20
  • @OHGODSPIDERS please review the code again, I just made an edit. – Felix Aug 01 '22 at 15:17
  • A `Date` never could have a format. So if I understand your question as intended, you are asking the impossible. Also as @MCEmperor already said, you should not use that class at all. – Ole V.V. Aug 01 '22 at 15:18
  • @MCEmperor so in my case how do i implement it with LocalDate. Note that i'm parsing in a string as date – Felix Aug 01 '22 at 15:18
  • Doesn’t this answer your question? [Calendar date to yyyy-MM-dd format in java](https://stackoverflow.com/questions/12575990/calendar-date-to-yyyy-mm-dd-format-in-java)? [This](https://stackoverflow.com/questions/62871262/convert-java-util-date-to-java-util-date-with-different-formating-in-java)? [This](https://stackoverflow.com/questions/72236026/how-to-format-string-to-date-with-format-dd-mm-yyyy-in-java)? – Ole V.V. Aug 01 '22 at 15:22
  • 2
    ❶ Define your format as a `String` ❷ Pass this string as an argument to `DateTimeFormatter::ofPattern` ❸ Use your string and the `DateTimeFormatter` instance as arguments to `LocalDate::parse`. – MC Emperor Aug 01 '22 at 15:22
  • @Felix before we are telling you how to do it with a LocalDate maybe you can specify what exactly your problem is? Because with your new changes in the edit [I am getting an output 'Fri Dec 20 00:00:00 GMT 2002' that shows it parsed the date correctly](https://ideone.com/8Z88F8). If your problem however is that you want to output your date Object in a specific format, then LocalDate won't help you with that either. As Ole V.V. said date objects themself do not have a format. – OH GOD SPIDERS Aug 01 '22 at 15:22
  • 1
    If you want to output a Date, or LocalDate in a specific format, you will always have to create a String representation yourself and output that String. There simply is no way to "save" a format in a Date object and have it use that in its default toString() method that is used when you print it. See: [display Java.util.Date in a specific format](https://stackoverflow.com/questions/6262310/display-java-util-date-in-a-specific-format) on how to format your Date object into a String. – OH GOD SPIDERS Aug 01 '22 at 15:24

0 Answers0