0

I am trying to work out how I would figure out which day a given Linux Epoch TS belongs to.

To help explain more, I am giving the following example:

I have had a go at this using JODATIME, but couldn't get a working solution.

// some code here that I need.... <---??

// The time set here below is (Tue 2019-09-10 14:05:00 UTC)
final long timestamp = 1568124300000L


if (timestamp = xxxxx) {
    System.out.println("The day it belongs to is: " + dayEpoch);
}

Ideally I want the output to be printed is:

The day it belongs to is: 2019-09-10

Would appreciate if you give an example of code to achieve this :)

Saffik
  • 911
  • 4
  • 19
  • 45
  • 1
    [Unix epoch time to Java Date object](//stackoverflow.com/q/535004) – 001 Sep 10 '19 at 14:20
  • 1
    @JohnnyMopp Today no one wants a `Date` object, or at least should not want one. The `Date` class is poorly designed and long outdated. A `LocalDate` will serve the questioner much better. `LocalDate` is from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 10 '19 at 14:42

1 Answers1

3

You can use java.time to meet your requirement(s):

First option (preferred): OffsetDateTime

final long timestamp = 1568124300000L;

// create an Instant from the timestamp
Instant instant = Instant.ofEpochMilli(timestamp);
// create a datetime object from the Instant
OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, ZoneId.of("UTC"));
// print it in your desired formatting
System.out.println("The day it belongs to is: "
                    + offsetDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

Second option (valid, but less appropriate): ZonedDateTime

final long timestamp = 1568124300000L;

// create an Instant from the timestamp
Instant instant = Instant.ofEpochMilli(timestamp);
// create a datetime object from the Instant
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
// print it in your desired formatting
System.out.println("The day it belongs to is: "
                    + zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

Please note that you can have different ZoneIds. You have clearly written that it is UTC in your case, so I took that one for the example. There is ZoneId.systemDefault(); to get a local timezone, the output for that is the same (date) on my system.
For using this, you will have to be using Java 8 or the ThreeTen-Backport

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 2
    Very good answer. The attention that the answer gives to time zone is crucial, thank you. Instead of `DateTimeFormatter.ofPattern("yyyy-MM-dd")` just use `DateTimeFormatter.ISO_LOCAL_DATE`. – Ole V.V. Sep 10 '19 at 14:44
  • One could also write `"The day it belongs to is: " + localDateTime.toLocalDate()` and skip the DateTimeFormatter entirely. – VGR Sep 10 '19 at 14:48
  • **`LocalDateTime` is the wrong class** to use here. As explained in its Javadoc, that class cannot represent a moment, is *not* a point on the timeline. Your use here strips away the vital information about time zone or offset-from-UTC with nothing gained to compensate for that loss. – Basil Bourque Sep 10 '19 at 23:24
  • @BasilBourque yes, you're right, I should have used a `ZonedDateTime` or an `OffsetDateTime`, but the answer seems to be sufficient for Saffik (OP). I will update this soon for future readers… Thanks for pointing that out! – deHaar Sep 11 '19 at 06:32
  • 1
    For UTC, `OffsetDateTime` would be more appropriate than `ZonedDateTime`. – Basil Bourque Sep 11 '19 at 06:46
  • 1
    @BasilBourque for the sake of completeness and use of appropriate classes, I provided both options. Thanks again. – deHaar Sep 11 '19 at 07:08