mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8133079: java.time LocalDate and LocalTime ofInstant() factory methods
Reviewed-by: rriggs, scolebourne
This commit is contained in:
parent
3c5dd5581c
commit
9322c398b5
4 changed files with 127 additions and 15 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -272,12 +272,8 @@ public final class LocalTime
|
|||
*/
|
||||
public static LocalTime now(Clock clock) {
|
||||
Objects.requireNonNull(clock, "clock");
|
||||
// inline OffsetTime factory to avoid creating object and InstantProvider checks
|
||||
final Instant now = clock.instant(); // called once
|
||||
ZoneOffset offset = clock.getZone().getRules().getOffset(now);
|
||||
long localSecond = now.getEpochSecond() + offset.getTotalSeconds(); // overflow caught later
|
||||
int secsOfDay = (int) Math.floorMod(localSecond, SECONDS_PER_DAY);
|
||||
return ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + now.getNano());
|
||||
return ofInstant(now, clock.getZone());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
@ -343,6 +339,27 @@ public final class LocalTime
|
|||
return create(hour, minute, second, nanoOfSecond);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains an instance of {@code LocalTime} from an {@code Instant} and zone ID.
|
||||
* <p>
|
||||
* This creates a local time based on the specified instant.
|
||||
* First, the offset from UTC/Greenwich is obtained using the zone ID and instant,
|
||||
* which is simple as there is only one valid offset for each instant.
|
||||
* Then, the instant and offset are used to calculate the local time.
|
||||
*
|
||||
* @param instant the instant to create the time from, not null
|
||||
* @param zone the time-zone, which may be an offset, not null
|
||||
* @return the local time, not null
|
||||
*/
|
||||
public static LocalTime ofInstant(Instant instant, ZoneId zone) {
|
||||
Objects.requireNonNull(instant, "instant");
|
||||
Objects.requireNonNull(zone, "zone");
|
||||
ZoneOffset offset = zone.getRules().getOffset(instant);
|
||||
long localSecond = instant.getEpochSecond() + offset.getTotalSeconds();
|
||||
int secsOfDay = (int) Math.floorMod(localSecond, SECONDS_PER_DAY);
|
||||
return ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + instant.getNano());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Obtains an instance of {@code LocalTime} from a second-of-day value.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue