Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





Java - Code Samples

 Sample 1. Code Sample / Example / Snippet of java.time.zone.ZoneOffsetTransition

    public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {

Objects.requireNonNull(localDateTime, "localDateTime");

Objects.requireNonNull(zone, "zone");

if (zone instanceof ZoneOffset) {

return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);

}

ZoneRules rules = zone.getRules();

List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);

ZoneOffset offset;

if (validOffsets.size() == 1) {

offset = validOffsets.get(0);

} else if (validOffsets.size() == 0) {

ZoneOffsetTransition trans = rules.getTransition(localDateTime);

localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());

offset = trans.getOffsetAfter();

} else {

if (preferredOffset != null && validOffsets.contains(preferredOffset)) {

offset = preferredOffset;

} else {

offset = Objects.requireNonNull(validOffsets.get(0), "offset"); // protect against bad ZoneRules

}

}

return new ZonedDateTime(localDateTime, offset, zone);

}


   Like      Feedback      java.time.zone.ZoneOffsetTransition



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus