You could use java.time
for this, you'd basically have to
- create an
Instant
from the milliseconds (not a Timestamp
)
- create a
ZonedDateTime
using a desired time zone and the Instant
- use methods provided by
java.time
classes in order to truncate or adjust the time and
- lastly, convert the
Instant
from one ZoneId
to another one (here from "Europe/Paris"
to "UTC"
)
Here's an example:
public static void main(String[] args) {
// first of all, use an Instant, not a Timestamp for conversion
Instant time = Instant.ofEpochMilli(1652039000000L);
// define the zone for your time
ZoneId paris = ZoneId.of("Europe/Paris");
// then create a ZonedDateTime of it at the desired zone
ZonedDateTime parisTime = ZonedDateTime.ofInstant(time, paris);
// (1) truncate the time to hours
ZonedDateTime parisTimeTilHour = parisTime.truncatedTo(ChronoUnit.HOURS);
// (2) truncate the time to days
ZonedDateTime parisTimeDateOnly = parisTime.truncatedTo(ChronoUnit.DAYS);
// (3) get the first day of week and truncate that to days
ZonedDateTime parisTimeStartOfWeek = parisTime.with(WeekFields.ISO.getFirstDayOfWeek())
.truncatedTo(ChronoUnit.DAYS);
// define a formatter to be used for output
DateTimeFormatter isoLDT = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.S");
// print the results:
System.out.println("Paris: " + parisTime.format(isoLDT));
System.out.println(" --> " + parisTimeTilHour.format(isoLDT));
System.out.println(" --> " + parisTimeDateOnly.format(isoLDT));
System.out.println(" --> " + parisTimeStartOfWeek.format(isoLDT));
// Shift the zone to UTC, create UTC as ZoneId first…
ZoneId utc = ZoneId.of("UTC");
ZonedDateTime utcTime = parisTime.withZoneSameInstant(utc);
ZonedDateTime utcTimeTilHour = parisTime.withZoneSameInstant(utc);
ZonedDateTime utcDateOnly = parisTimeDateOnly.withZoneSameInstant(utc);
ZonedDateTime utcWeekStart = parisTimeStartOfWeek.withZoneSameInstant(utc);
// print…
System.out.println("UTC : " + utcTime.format(isoLDT));
System.out.println(" --> " + utcTimeTilHour.format(isoLDT));
System.out.println(" --> " + utcDateOnly.format(isoLDT));
System.out.println(" --> " + utcWeekStart.format(isoLDT));
}
This example will output
Paris: 2022-05-08 21:43:20.0
--> 2022-05-08 21:00:00.0
--> 2022-05-08 00:00:00.0
--> 2022-05-02 00:00:00.0
UTC : 2022-05-08 19:43:20.0
--> 2022-05-08 19:43:20.0
--> 2022-05-07 22:00:00.0
--> 2022-05-01 22:00:00.0
And if you really have a Timestamp
only and you would have to extract the millis in order to create an Instant
… That's not necessary anymore, there is a method now for legacy compatibility, that is Timestamp.toInstant()
.