0

I am trying to convert java String date into java.sql.Timestamp. I am able to convert this by using SimpleDateFormat with String date value as "2021-01-07 02:02:16.172", but when trying with the value as "2021-08-04T00:00:00.000" with seperator 'T', it gives me error. Below is the java code:

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTest {

    public static void main(String[] args) throws ParseException {
        
        //String date = "2021-08-04T00:00:00.000Z";// How to convert this?
        
        String date = "2021-01-07 02:02:16.172";// conversion successful
        
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
        Date parsedDate = dateFormat.parse(date);
        
        Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
        
        System.out.println(timestamp);
    }
    
}
4
  • 1
    new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS"); would be the old style Commented Aug 27, 2021 at 12:22
  • 1
    I recommend you don’t use Timestamp and SimpleDateFormat. Those classes are poorly designed and long outdated, the latter in particular notoriously troublesome. Instead use Instant from java.time, the modern Java date and time API. Commented Aug 27, 2021 at 18:05
  • @g00se (1) Please don’t advise the use of SimpleDateFormat, you’re doing people a bad favour. (2) Beware of the difference between hh and HH. Your formatter is not correct. Commented Aug 27, 2021 at 18:07
  • 1
    I'm not "advising" it. As you can see, I said it was the "old style" Commented Aug 27, 2021 at 18:35

2 Answers 2

5

You could use the modern API for dates, times and related information (like offsets from UTC): java.time

Strings in different formats need to be handled differently:

  • your first example String is formatted in ISO standard, so it can be parsed without defining a custom format. The parsing implicitly uses a DateTimeFormatter.ISO_OFFSET_DATE_TIME, which will result in an OffsetDateTime

  • your seconds String lacks the 'T' between date and time as well as an offset, that means you can just directly parse it to a LocalDateTime

java.sql.Timestamp got methods for conversion to java.time classes, at least to/from an Instant and a LocalDateTime. Since an Instant is a well defined moment in time, you can derive it from an OffsetDateTime:

public static void main(String[] args) throws Exception {
    // your two example datetimes
    String isoDateTime = "2021-08-04T00:00:00.000Z";
    String customDateTime = "2021-01-07 02:02:16.172";
    // you will need a custom formatter for the second one
    DateTimeFormatter customDtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
    // parse the Strings to java.time objects
    // ISO standard, no extra formatter needed for the first one
    OffsetDateTime odt = OffsetDateTime.parse(isoDateTime);
    // the second one requires the formatter defined above
    LocalDateTime ldt = LocalDateTime.parse(customDateTime, customDtf);
    // convert them into Timestamps
    Timestamp tsOne = Timestamp.from(odt.toInstant());
    Timestamp tsTwo = Timestamp.valueOf(ldt);
    // and print them
    System.out.println("First Timestamp:  " + tsOne);
    System.out.println("Second Timestamp: " + tsTwo);
}

The output of this is

First Timestamp:  2021-08-04 02:00:00.0
Second Timestamp: 2021-01-07 02:02:16.172

This would be the new style...

Sign up to request clarification or add additional context in comments.

1 Comment

I strongly recommend using java.time as this answer demonstrates. The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API.
1
new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");

would be the old style

1 Comment

g00se - I did not like this answer. I have seen you writing good answers and I wish you continue doing so. There are two problems with this answer: (1) Use HH which is used for 24-hour format or use hh:mm:ss.SSS a where hh is used for 12-hour format and a is used for am/pm marker. (2) Do not use outdated and error-prone SimpleDateFormat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.