0

I am getting date from Oracle is in Timestamp but I need to convert it in to this format 2020-02-17 (yyyy-mm-dd) format, but currently in postman I am receiving date as "2020-02-17T09:40:37.850+0000" in this format.
Any help on this would be really appreciated

deHaar
  • 17,687
  • 10
  • 38
  • 51
Taher Ali
  • 37
  • 1
  • 2
  • 5
  • Just extract the first 10 characters of the inputted date, and pass it to another date object. You could also use the SimpleDateFormat, but that's using the longest possible method. – Robo Mop Feb 17 '20 at 10:05

3 Answers3

2

You can easily convert a java.sql.Timestamp to a java.time.LocalDate and get a date String by formatting the LocalDate like this:

public static void main(String[] args) {
    // just a timestamp stub that takes "now"
    java.sql.Timestamp ts = java.sql.Timestamp.from(Instant.now());
    // convert it to a modern date object
    LocalDate justDate = ts.toLocalDateTime().toLocalDate();
    // print it using a suitable formatter
    System.out.println(justDate.format(DateTimeFormatter.ISO_LOCAL_DATE));
}

The output (today) is

2020-02-17

You just need Java 8 or higher for this or import a backport library.

EDIT

If you don't need a String but a java.util.Date, do it with Instant only, like this:

public static void main(String[] args) {
    // just a timestamp stub that takes "now"
    Instant now = Instant.now();
    Timestamp ts = Timestamp.from(now);
    // create an Instant from the Timestamp
    Instant timestampInstant = ts.toInstant();
    // and then create a Date out from that Instant
    java.util.Date creationDate = java.util.Date.from(now);

    // do something with the Date here...
}

But please consider using java.time wherever possible, which might be in your domain class...

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • Thanks for quick revert.But my domain class is having Date field 'creationDate',,so how can i set that string back to date. Currently justDate.format(DateTimeFormatter.ISO_LOCAL_DATE) its a string – Taher Ali Feb 17 '20 at 10:44
1
 private String  getZonedDateTime(String startTime){
  // input -> startTime: 2020-02-17T09:40:37.850+0000
  // output -> 2020-02-17
  return ZonedDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX"))
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
 }

Just pass the Date String which you have and get it in what format you want.

Sriram Umapthy
  • 678
  • 8
  • 11
  • Thanks,,,but all this is string how can i get the same in Date variable. – Taher Ali Feb 17 '20 at 10:54
  • You cannot have a `Date` object with a format. See for example [want current date and time in “dd/MM/yyyy HH:mm:ss.SS” format](https://stackoverflow.com/questions/8745297/want-current-date-and-time-in-dd-mm-yyyy-hhmmss-ss-format). @TaherAli – Ole V.V. Feb 17 '20 at 20:54
0

That question is answered here

And what you want exactly, to display the date with that format or save with that format.

If you want display the date with (yyyy-mm-dd)

String dateFormated = new SimpleDateFormat("yyyy-MM-dd").format(myTimestamp);
System.out.println(dateFormated);

If you want save the date with that format you can try to do this:

try {
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd);
     String dateFormated = dateFormat.format(myTimestamp);
     Date parsedDate = dateFormat.parse(dateFormated);
     Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
} catch(Exception e) { 

}
Adrian Lagartera
  • 442
  • 1
  • 3
  • 17
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Feb 18 '20 at 21:40