-3

How can I convert from millisecond to date like DD-mm-yyyy? I have a column in my database with public holidays dates and it is of type date. In my Entity and DTO class I have the column to be of type Date. When I try to retrive the table from the database the date comes in milliseconds like this :

 {
   "date" : 1577916000000,
   "id" :1,
   "description" : New Years Eve
}
Andrea
  • 11
  • 1
  • 5
  • 1
    Don't use `Date` and `SimpleDateFormat`, they're obsolete. Use classes from the `java.time` package. – MC Emperor May 21 '20 at 13:08
  • In your case, you could use [`Instant::ofEpochMilli`](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#ofEpochMilli-long-). Then you could use either `atOffset` or `atZone` to convert it to a `OffsetDateTime` or `ZonedDateTime` respectively. In order to format, use the `DateTimeFormatter` class to create the format. Check [the documentation of the `DateTimeFormatter` class](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) for more details. – MC Emperor May 21 '20 at 13:14

3 Answers3

2
Instant
            .ofEpochMilli(1577916000000L)
            .atZone(ZoneId.systemDefault())
            .format(DateTimeFormatter.ofPattern("dd-MM-yyyy"))

You can read more about patterns here https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

PS: Try to avoid using outdated Date and SimpleDateFormat from java.util package. They were replaced by java.time package

-1

It seems you are getting a date as a timestamp. you have to format it to String as following.

Date date=new Date(yourDateWithMs);
DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");  
String strDate = dateFormat.format(date); 

This Date is 'java.util.Date' and yourDateWithMs can be given as long.

DateFormat is used to format the resultant string.

Vikum Dheemantha
  • 764
  • 8
  • 24
  • 1
    Wrong. This just gave me `java.lang.IllegalArgumentException`. Also 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. May 24 '20 at 17:15
  • @OleV.V. Thanks for the update. Meantime, even knowing my answer is outdated, I am curious why you getting `java.lang.IllegalArgumentException`. – Vikum Dheemantha May 25 '20 at 13:19
  • 1
    Part of it was me not reading your answer properly. My Eclipse created the `yourDateWithMs` variable as a `String`, and I initialized it to `"1577916000000"`. There is a deprecated `Date(String)` constructor, but it didn’t accept this string. When instead I declare `long yourDateWithMs = 1577916000000L;`, I get a `strDate` of `02-30-2020`. Month 30? Not correct either. – Ole V.V. May 25 '20 at 14:35
-1

Try this:

double s=1577916000000.0/1000;
        DateFormat df = new SimpleDateFormat("dd MMM yyyy ");
        String a=df.format(s);
amrtaweel
  • 76
  • 5
  • I think it’s wrong. It gave me `19 Jan 1970 `, which probably isn’t correct. Also 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. May 24 '20 at 17:17