Home > Enterprise >  How do I get start of the hour, day and week from a timestamp?
How do I get start of the hour, day and week from a timestamp?

Time:05-17

I have the following Timestamp:

Timestamp time = new Timestamp(1652039000000L);

This time is "2022-05-08 19:43:20.0" in local time. Are there any ways to turn this into the start of the current:

  1. Hour: "2022-05-08 19:00:00.0"
  2. Day: "2022-05-08 00:00:00.0"
  3. Week: "2022-05-02 00:00:00.0"

And then minus get the UTC time from it so that Paris ( 2 hours) would return:

  1. Hour: "2022-05-08 17:00:00.0"
  2. Day: "2022-05-07 22:00:00.0"
  3. Week: "2022-05-01 22:00:00.0"

CodePudding user response:

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 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().

CodePudding user response:

Use timeStamp.toLocalDateTime().toLocalDate(); for converting TimeStamp to LocalDate then get start of week by following code:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
    public class Main {
        public static void main(String[] args) {
            LocalDate date = LocalDate.of(2022, 5, 15);
            date = date.with(nextNthDayOfWeek(0, DayOfWeek.MONDAY));
            System.out.println("date = "   date); 
        }
    
        public static TemporalAdjuster nextNthDayOfWeek(int n, DayOfWeek dayOfWeek) {
            return temporal -> {
                Temporal next = temporal.with(TemporalAdjusters.next(dayOfWeek));
                return next.plus(n - 1, ChronoUnit.WEEKS);
            };
        }
    }

Java 8: Find nth DayOfWeek after specific day of month

  • Related