-3

I want to convert string to date. I tried different scenarios but unable to convert it to my required format.

String Format : "dd/MM/yyyy hh:mm aa".
Required Date Format : "yyyy-MM-dd HH:mm:ss.SSS".

SimpleDateFormat originalFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm aa");
Date date = originalFormat.parse("03/02/2007 05:36 PM");
      
SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String originalDate = targetFormat.format(date);
        
Date dateValue = targetFormat.parse(originalDate);
        
String finalDate = targetFormat.format(dateValue);
Date requiredDate = targetFormat.parse(finalDate);

I am getting result as string in my required format. But I need it as date. After conversion I am getting in "EEE MMM dd HH:mm:ss zzzz yyyy" this format. What to do?

Thanks in Advance.

phani
  • 105
  • 2
  • 15
  • 3
    Also: "I am getting result as string in my required format. But I need it as date." what do you mean? A `Date` object? Because those are not formatted. They are formatted only when you convert them to a string. – Federico klez Culloca Feb 04 '21 at 07:52
  • 2
    If you get that particular format when printing the date object, then you're using a `java.util.Date`. Don't use that old antiquated, flawed object. **Use `LocalDateTime`** object instead. Just give it a `DateTimeFormatter` when parsing your input string, and give it another one when formatting to string for display. – Andreas Feb 04 '21 at 08:00
  • 1
    Now that we see code, keep the first 4 lines, and delete the rest, since that extra stuff is just redoing for you already have. If you want a date object, the `date` value is usable, but it doesn't has a format you can control. The desired output format is what `targetFormat` controls, and you have the value in that format in the `originalDate` string, which means you already have everything. As Federico already said, the date object cannot have the desired format by itself, the format is always given separate from the value. Nothing you can do about that. – Andreas Feb 04 '21 at 08:06
  • Thank for your reply. Andreas and Federico – phani Feb 04 '21 at 08:09
  • It's already mentioned, but still — **do not use `Date` and `SimpleDateFormat`.** They're [troublesome](https://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api). Use classes from the [`java.time` package](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) instead. – MC Emperor Feb 04 '21 at 08:46

1 Answers1

0

I do not understand - "I am getting result as string in my required format. But I need it as date". If you want date in certan format it may be only date converted to string using format. I use java.time.LocalDateTime and java.time.format.DateTimeFormatter (avalible from Java 8):

   String strDate = "03/02/2007 05:36 PM";
   DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm a");
   LocalDateTime dt = LocalDateTime.parse(strDate, format);
   
   DateTimeFormatter newFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
   System.out.println(dt.format(newFormat));
Sergey Afinogenov
  • 2,137
  • 4
  • 13