0

My requirement is to convert the string "2019-04-25 07:06:42.790" to Date Object with same format as "2019-04-25 07:06:42.790".

I tried to do this, but it is always giving in String format.

    String createDate = "2019-04-25 07:06:42.790";

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
    LocalDateTime localDateTime = LocalDateTime.parse(createDate, formatter);

    System.out.println(formatter.format(localDateTime));

    SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
    formatter1.setTimeZone(TimeZone.getTimeZone("IST"));

    Date date = formatter1.parse(createDate);
    System.out.println(date);
    
    String formattedDateString = formatter1.format(date);
    System.out.println(formattedDateString);

Output from the above code:

2019-04-25 07:06:42.790
Thu Apr 25 07:06:42 IST 2019
2019-04-25 07:06:42.790
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Vikas
  • 21
  • 3
  • This should help https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – hfontanez Apr 08 '22 at 05:20
  • 1
    See also: https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/ – Jon Skeet Apr 08 '22 at 05:46
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 08 '22 at 06:19
  • You are asking the impossible. A `Date` cannot have a format (neither can a `LocalDateTime`). You can have any format you like, but only in a `String`. – Ole V.V. Apr 08 '22 at 06:20

1 Answers1

5

tl;dr

Date-time objects do not have a “format”.

Use only java.time classes.

LocalDateTime
.parse ( 
    "2019-04-25 07:06:42.790"
    .replace( " " , "T" ) 
)
.toString()
.replace( "T" , " " )

Details

You need to understand that date-time objects are not text. They don’t have a “format”. The do parse and generate text in various formats, but that text is always external.

Use only the java.time classes. Avoid legacy classes such as Date and Calendar.

Make your input comply with ISO 8601 standard.

String input = "2019-04-25 07:06:42.790".replace( " " , "T" ) ;

Parse as a LocalDateTime.

LocalDateTime ldt = LocalDateTime.parse ( input ) ;

To generate the same text as your input, call toString and replace the T with your desired SPACE character.

You could use a DateTimeFormatter rather than the string manipulations shown above. But in your specific case I recommend the string manipulations.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • So if want this 2019-04-25 07:06:42.790 String in Date datatype its not possible is that so? – Vikas Apr 08 '22 at 05:39
  • @Vikas: You can represent *the data itself* but not the text format. Just format the appropriate type (e.g. `LocalDateTime`) in whatever format you want later on. – Jon Skeet Apr 08 '22 at 05:47
  • @Basil So this i tried String input = "2019-04-25 07:06:42.790".replace( " " , "T" ) ; LocalDateTime ldt = LocalDateTime.parse ( input ) ; Date in = new Date(); ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault()); Date out = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant()); System.out.println(ldt+ " OUT "+ out) ; But the ouput is - 2022-04-08T11:28:22.810 OUT Fri Apr 08 11:28:22 IST 2022 This is not i want to achieve. – Vikas Apr 08 '22 at 05:57
  • @Vikas I already advised you to stop using the terrible legacy classes. The *java.time* classes completely replace them. – Basil Bourque Apr 08 '22 at 06:10