Search code examples
javadatedate-formatjava-time

How to convert 2019-03-26T04:31:40-05:00 like string into timestamp in Java?


I want to change my time got from server and save into the database using the following format - "25-MAR-19 01.23.42.121000000 AM"

How can I do that using Java?

Tried following code which didn't help

import java.sql.Date;
import java.time;
public class MyClass {
    public static void main(String args[]) {
        final String selectedStart = "2019-03-26T04:31:40-05:00";
        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss-XXX'");
        final LocalDate parsedDate = LocalDate.parse(selectedStart, formatter);
    }
}

Solution

  • I don't totally understand your question but if you want just the date part you can use Ole.V.V answer orjust use :

    LocalDate d = LocalDate.parse(selectedStart.split("T")[0]);
    

    Edit

    OffsetDateTime odt = OffsetDateTime.parse(selectedStart);
    String str = odt.format(DateTimeFormatter.ofPattern("dd-MMM-uuuu hh.mm.ss.SSSSS a"));