1

I'm receiving dates in epoch time from an API. They are in string format and what I want is to simply transform it to date without hours, minutes, and seconds, with the format dd / mm / yyyy. Example:

  String epochDate = "1590476643";

I was trying just to convert it with the timestamp and I'm getting this result with this logic. I'm getting when a 1970 date when the epoch time is from yesterday may 26.

Date d = new Date(Long.parseLong(epochDate));
Timestamp ts= new Timestamp(d.getTime());

RESULT = 1970-01-19 10:47:56 SHOULD BE 26-05-2020

How could I transform the string to a date and the format?

Stapler23
  • 39
  • 6
  • `date without hours, minutes, and seconds, with the format dd / mm / yyyy` as string ? – Eklavya May 27 '20 at 06:08
  • No, as date. It's in date format in the DB where I have to save it. – Stapler23 May 27 '20 at 06:10
  • What database? Does it store `java.util.Date` only or can it store `java.time.LocalDate`? – deHaar May 27 '20 at 06:12
  • You can use `LocalDate` if you only store `date` , have a look for converstion https://stackoverflow.com/questions/35183146/how-can-i-create-a-java-8-localdate-from-a-long-epoch-time-in-milliseconds – Eklavya May 27 '20 at 06:13
  • An oracle database. I think it can store both, for sure. It's type DATE in the bbdd. – Stapler23 May 27 '20 at 06:13
  • And refer here why you need to use `LocalDate` https://stackoverflow.com/a/5050259/4207306 – Eklavya May 27 '20 at 06:15
  • ``` LocalDate date = Instant.ofEpochMilli(Long.parseLong(tm_login)).atZone(ZoneId.systemDefault()).toLocalDate();``` why I'm getting with this date? ```1970-01-19 00:00:00``` when the epoch time in the string format is from yesteday. – Stapler23 May 27 '20 at 06:18
  • @Stapler23 See my answer. – Eklavya May 27 '20 at 06:29
  • Your `String` doesn't represent epoch millis, that's why your conversion fails... – deHaar May 27 '20 at 06:30

3 Answers3

1

Use LocalDate for only store date and Instant.ofEpochSecond for conversion

LocalDate localDate = Instant.ofEpochSecond(Long.parseLong("1590476643")).atZone(ZoneId.systemDefault()).toLocalDate();

And format as string

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formattedString = localDate.format(formatter);
Eklavya
  • 15,459
  • 4
  • 14
  • 41
  • 1
    This is basically what my answer says, don't know who was earlier... but nice to see it in one line ;-) – deHaar May 27 '20 at 06:28
  • Thanks guys, that's the solution. Any idea to format it to dd/mm/yyyy? – Stapler23 May 27 '20 at 06:30
  • @Stapler23 Formating need when serializing as string, you don't need to format for save in database, I added how to format as string – Eklavya May 27 '20 at 06:32
  • In your spare time try to read here https://stackoverflow.com/help/someone-answers – Eklavya May 27 '20 at 08:22
0

You can use DateFormat and SimpleDateFormat from java.text package,

String epochDate = "1590476643";
Date d = new Date(Long.parseLong(epochDate));
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String epochDateString = formatter.format(d);
  • Thanks for the comment. I'm getting all the time this date ```1970-01-19 00:00:00``` when the epoch time is from yesterday. Do you know what is going on?, besides is not the format that I want – Stapler23 May 27 '20 at 06:24
0

I strongly advice you to use java.time and avoid java.util for datetime operations.
Your String of epoch seconds can be turned into a java.time.LocalDate like this:

public static void main(String[] args) {
    // you seem to receive a datetime in epoch seconds
    String epochDate = "1590476643";
    long epochDateAsLong = Long.parseLong(epochDate);
    // so create an object representing a moment in time from the epoch seconds as long
    Instant instant = Instant.ofEpochSecond(epochDateAsLong);
    // create a LocalDateTime object from that instant and apply a desired time zone
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.of("UTC"));
    // create a LocalDate from this LocalDateTime (that means taking just the date part of it)
    LocalDate localDate = localDateTime.toLocalDate();
    // and print it
    System.out.println(localDate);

    // if you really need a Date, then use legacy compatibility, but try to avoid it
    Date date = Date.from(instant);

    // printing outdated class omitted...
}

and would then just output

2020-05-26

you can change the format of the output by using a DateTimeFormatter.

deHaar
  • 11,298
  • 10
  • 32
  • 38