2

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);
    }
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
thisisdude
  • 543
  • 1
  • 7
  • 31
  • 2
    What is `java.sql.Date` doing there? – MC Emperor Mar 26 '19 at 14:02
  • 3
    You shouldn't store date/time values as string in the database, use the proper type (TIMESTAMP WITH TIME ZONE) and use `java.time.OffsetDateTime` to store it using a JDBC 4.2 (or higher) compliant driver. – Mark Rotteveel Mar 26 '19 at 14:02
  • You say you want to store date with a precision of nano seconds but what is the point if what your receive is in seconds? Any precision beyond whole seconds will just be made up? – Joakim Danielson Mar 26 '19 at 14:02
  • Related: [Converting ISO 8601-compliant String to java.util.Date](https://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date) (just ignore the many answers using the troublesome and outdated `SimpleDateFormat` class). – Ole V.V. Mar 26 '19 at 14:13

2 Answers2

3

Don’t define your own formatter. Use the built-in DateTimeFormatter.ISO_OFFSET_DATE_TIME.

    final String selectedStart = "2019-03-26T04:31:40-05:00";
    final LocalDate parsedDate = LocalDate.parse(selectedStart, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    System.out.println(parsedDate);

2019-03-26

What went wrong in your code?

Two of the characters in your format pattern string are wrong:

  1. The offset is -05:00, the minus is part of it (it could have been a + instead for a positive UTC offset). So delete it from your format pattern.
  2. Single quotes (apostrophes) may be set around literal parts as you are doing correctly in 'T'. You cannot have an unmatched single quote as in the end of your format pattern string. Just delete that too.

Further tips

  • LocalDate is the wrong class to use when you want to store date and time of day into your database. A LocalDate holds a date without time of day. You probably want OffsetDateTime.
  • Don’t care about format when storing into you database. Just hand the datetime object (for example your OffsetDateTime) to your JDBC driver or JPA implementation and let it take care of the rest.
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • -5.00 is a timestamp, stating the timezone. I need a format like this 25-MAR-19 01.23.42.121000000 AM, and not only date. – thisisdude Mar 26 '19 at 14:06
3

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"));
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • This also gives only date.My input is this - 2019-03-26T04:31:40-05:00 and output I expect is this 26-MAR-19 04.31.40.121000000 AM – thisisdude Mar 26 '19 at 14:11
  • @mashkurm I edit my answer you can try that solution, I dont test it sorry (using phone to answer ) – Youcef LAIDANI Mar 26 '19 at 14:24
  • 1
    It works and yields `26-Mar-2019 04.31.40.00000 AM`. For more decimals on the seconds just add more capital `S` in the format pattern. – Ole V.V. Mar 26 '19 at 14:54
  • Thank you @Ole V.V. for testing it for me, I wonder if that is a good idea to add 9 time `S` in the format, you don't think it's a little ugly and maybe the OP doesn't want all that details? – Youcef LAIDANI Mar 26 '19 at 15:03
  • 1
    We can just let the OP decide. He knows best what is desired. – Ole V.V. Mar 26 '19 at 15:05