get ZoneOffset At Now - Java java.time

Java examples for java.time:Timezone

Description

get ZoneOffset At Now

Demo Code


/**//from www  . ja v a  2 s  .com
     * Copyright 2014 Kohsuke Namihira All Rights Reserved.
     */
//package com.java2s;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class Main {

  public static Map<ZoneId, ZoneOffset> getOffsetsAtNow() {
    return getOffsets(LocalDateTime.now());
  }

  public static Map<ZoneId, ZoneOffset> getOffsets(final LocalDateTime localDateTime) {
    if (localDateTime == null) {
      throw new IllegalArgumentException("localDateTime must not be null");
    }

    final Set<String> zoneIds = ZoneId.getAvailableZoneIds();
    final Map<ZoneId, ZoneOffset> result = zoneIds.stream().collect(
        Collectors.toMap(id -> ZoneId.of(id), id -> ZonedDateTime.of(localDateTime, ZoneId.of(id)).getOffset()));

    Map<ZoneId, ZoneOffset> sorted = new TreeMap<>((first, second) -> first.toString().compareTo(second.toString()));
    sorted.putAll(result);
    return sorted;
  }
}

Related Tutorials