user1790898
user1790898

Reputation: 63

Java 8 ZoneDateTime getting time

I am trying to update this code to use Java 8 time api

  Calendar c = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
    c.set(2020, 6 - 1, 7, 0, 0, 0);
    Date dts = c.getTime();
    // this time is already initialised to 01:00 BST
    int hours = 24;
    int minutes= 9;
    int seconds=0;

    Calendar calendar = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
    calendar.setTime(dts);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, minutes);
    calendar.set(Calendar.SECOND, seconds);
    calendar.add(Calendar.HOUR_OF_DAY, hours);

    System.out.println(calendar.getTime());
    // Mon Jun 08 01:09:00 BST 2020

to something like this

    ZonedDateTime zdt = ZonedDateTime.of(2020,6,7,0,0,0,0,ZoneId.of("UTC"));

    zdt.withHour(hours-1);
    zdt.withMinute(minutes);
    zdt.plusSeconds(seconds);
    System.out.println(zdt);

First thing is ZonedDate time unlike The Gregorian Calender doesnt initalise at 1 o clock. Which is where im having difficulties.

ZonedDateTime also doesnt take hours more than 23 so i have to reduce it by 1.

I guess the question im asking is what are the java alternatives to the methods in the gregorian calender that can exhibit similar behaviour.

Upvotes: 0

Views: 478

Answers (3)

user1790898
user1790898

Reputation: 63

It turns out what i needed was similar to this

 ZonedDateTime zdt = ZonedDateTime.ofInstant(date.toInstant(),ZoneId.of("UTC")).withHour(0).withMinute(minutes)
        .withSecond(seconds).plusHours(hours);

result = zdt.withZoneSameInstant(ZoneId.of("Europe/London"))

Thanks all for helping me get to this point.

Upvotes: 1

Oleg
Oleg

Reputation: 6314

In addition to Huatxu's answer use plusHours instead of withHour and use it with your local time zone and format string:

ZonedDateTime zdt = ZonedDateTime.of(2020, 6, 7, 0, 0, 0, 0, ZoneId.of("UTC"));
int hours = 24;
int minutes = 9;
int seconds = 0;

zdt = zdt.plusHours(hours);
zdt = zdt.withMinute(minutes);
zdt = zdt.plusSeconds(seconds);

System.out.println(zdt
        .withZoneSameInstant(ZoneId.systemDefault())
        .format(DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z uuuu")));

Or explicitly to get "BST" anywhere:

System.out.println(zdt
        .withZoneSameInstant(ZoneId.of("Europe/London"))
        .format(DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z uuuu")));

Upvotes: 1

Huatxu
Huatxu

Reputation: 111

The instances of LocalTime, LocalDate, LocalDateTime and ZonedDateTime are inmutable. Just like you do with your strings, you have to override the reference to the object with the new instance every time you make a change:

   ZonedDateTime zdt = ZonedDateTime.of(2020,6,7,0,0,0,0,ZoneId.of("UTC"));

   zdt = zdt.withHour(hours-1);
   zdt = zdt.withMinute(minutes);
   zdt = zdt.plusSeconds(seconds);
   System.out.println(zdt);

If you are new to this API, I would recommend you to not use the Zoned classes unless it is absolutely necessary, as they are very error prone.

Upvotes: 1

Related Questions