下面列出了java.time.LocalTime#ofSecondOfDay ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Reads the state from the stream.
*
* @param in the input stream, not null
* @return the created object, not null
* @throws IOException if an error occurs
*/
static ZoneOffsetTransitionRule readExternal(DataInput in) throws IOException {
int data = in.readInt();
Month month = Month.of(data >>> 28);
int dom = ((data & (63 << 22)) >>> 22) - 32;
int dowByte = (data & (7 << 19)) >>> 19;
DayOfWeek dow = dowByte == 0 ? null : DayOfWeek.of(dowByte);
int timeByte = (data & (31 << 14)) >>> 14;
TimeDefinition defn = TimeDefinition.values()[(data & (3 << 12)) >>> 12];
int stdByte = (data & (255 << 4)) >>> 4;
int beforeByte = (data & (3 << 2)) >>> 2;
int afterByte = (data & 3);
LocalTime time = (timeByte == 31 ? LocalTime.ofSecondOfDay(in.readInt()) : LocalTime.of(timeByte % 24, 0));
ZoneOffset std = (stdByte == 255 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds((stdByte - 128) * 900));
ZoneOffset before = (beforeByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + beforeByte * 1800));
ZoneOffset after = (afterByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + afterByte * 1800));
return ZoneOffsetTransitionRule.of(month, dom, dow, time, timeByte == 24, defn, std, before, after);
}
@Override
public String getText()
{
if (startTime == null)
{
return "";
}
if (!stopped)
{
Duration elapsed = Duration.between(startTime, Instant.now());
time = LocalTime.ofSecondOfDay(elapsed.getSeconds());
}
if (time.getHour() > 0)
{
return time.format(DateTimeFormatter.ofPattern("HH:mm"));
}
return time.format(DateTimeFormatter.ofPattern("mm:ss"));
}
/**
* Reads the state from the stream.
*
* @param in the input stream, not null
* @return the created object, not null
* @throws IOException if an error occurs
*/
static ZoneOffsetTransitionRule readExternal(DataInput in) throws IOException {
int data = in.readInt();
Month month = Month.of(data >>> 28);
int dom = ((data & (63 << 22)) >>> 22) - 32;
int dowByte = (data & (7 << 19)) >>> 19;
DayOfWeek dow = dowByte == 0 ? null : DayOfWeek.of(dowByte);
int timeByte = (data & (31 << 14)) >>> 14;
TimeDefinition defn = TimeDefinition.values()[(data & (3 << 12)) >>> 12];
int stdByte = (data & (255 << 4)) >>> 4;
int beforeByte = (data & (3 << 2)) >>> 2;
int afterByte = (data & 3);
LocalTime time = (timeByte == 31 ? LocalTime.ofSecondOfDay(in.readInt()) : LocalTime.of(timeByte % 24, 0));
ZoneOffset std = (stdByte == 255 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds((stdByte - 128) * 900));
ZoneOffset before = (beforeByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + beforeByte * 1800));
ZoneOffset after = (afterByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + afterByte * 1800));
return ZoneOffsetTransitionRule.of(month, dom, dow, time, timeByte == 24, defn, std, before, after);
}
/**
* Reads the state from the stream.
*
* @param in the input stream, not null
* @return the created object, not null
* @throws IOException if an error occurs
*/
static ZoneOffsetTransitionRule readExternal(DataInput in) throws IOException {
int data = in.readInt();
Month month = Month.of(data >>> 28);
int dom = ((data & (63 << 22)) >>> 22) - 32;
int dowByte = (data & (7 << 19)) >>> 19;
DayOfWeek dow = dowByte == 0 ? null : DayOfWeek.of(dowByte);
int timeByte = (data & (31 << 14)) >>> 14;
TimeDefinition defn = TimeDefinition.values()[(data & (3 << 12)) >>> 12];
int stdByte = (data & (255 << 4)) >>> 4;
int beforeByte = (data & (3 << 2)) >>> 2;
int afterByte = (data & 3);
LocalTime time = (timeByte == 31 ? LocalTime.ofSecondOfDay(in.readInt()) : LocalTime.of(timeByte % 24, 0));
ZoneOffset std = (stdByte == 255 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds((stdByte - 128) * 900));
ZoneOffset before = (beforeByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + beforeByte * 1800));
ZoneOffset after = (afterByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + afterByte * 1800));
return ZoneOffsetTransitionRule.of(month, dom, dow, time, timeByte == 24, defn, std, before, after);
}
/**
* Reads the state from the stream.
*
* @param in the input stream, not null
* @return the created object, not null
* @throws IOException if an error occurs
*/
static ZoneOffsetTransitionRule readExternal(DataInput in) throws IOException {
int data = in.readInt();
Month month = Month.of(data >>> 28);
int dom = ((data & (63 << 22)) >>> 22) - 32;
int dowByte = (data & (7 << 19)) >>> 19;
DayOfWeek dow = dowByte == 0 ? null : DayOfWeek.of(dowByte);
int timeByte = (data & (31 << 14)) >>> 14;
TimeDefinition defn = TimeDefinition.values()[(data & (3 << 12)) >>> 12];
int stdByte = (data & (255 << 4)) >>> 4;
int beforeByte = (data & (3 << 2)) >>> 2;
int afterByte = (data & 3);
LocalTime time = (timeByte == 31 ? LocalTime.ofSecondOfDay(in.readInt()) : LocalTime.of(timeByte % 24, 0));
ZoneOffset std = (stdByte == 255 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds((stdByte - 128) * 900));
ZoneOffset before = (beforeByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + beforeByte * 1800));
ZoneOffset after = (afterByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + afterByte * 1800));
return ZoneOffsetTransitionRule.of(month, dom, dow, time, timeByte == 24, defn, std, before, after);
}
@Test
void generatedLocalTimeShouldBeAlwaysTheSameForTheSameSeed() {
// Given
randomizer = aNewLocalTimeRangeRandomizer(minTime, maxTime, SEED);
LocalTime expected = LocalTime.ofSecondOfDay(62481);
// When
LocalTime randomValue = randomizer.getRandomValue();
// Then
assertThat(randomValue).isEqualTo(expected);
}
public void setMagnitude(Double magnitude) {
if(magnitude == null) {
value = null;
} else {
value = LocalTime.ofSecondOfDay(Math.round(magnitude));
}
}
@Test
public void factory_ofSecondOfDay_singletons() {
for (int i = 0; i < 24; i++) {
LocalTime test1 = LocalTime.ofSecondOfDay(i * 60L * 60L);
LocalTime test2 = LocalTime.of(i, 0);
assertSame(test1, test2);
}
}
public static void main(String[] args) {
//Current Time
LocalTime time = LocalTime.now();
System.out.println("Current Time="+time);
//Creating LocalTime by providing input arguments
LocalTime specificTime = LocalTime.of(12,20,25,40);
System.out.println("Specific Time of Day="+specificTime);
//Try creating time by providing invalid inputs
//LocalTime invalidTime = LocalTime.of(25,20);
//Exception in thread "main" java.time.DateTimeException:
//Invalid value for HourOfDay (valid values 0 - 23): 25
//Current date in "Asia/Kolkata", you can get it from ZoneId javadoc
LocalTime timeKolkata = LocalTime.now(ZoneId.of("Asia/Kolkata"));
System.out.println("Current Time in IST="+timeKolkata);
//java.time.zone.ZoneRulesException: Unknown time-zone ID: IST
//LocalTime todayIST = LocalTime.now(ZoneId.of("IST"));
//Getting date from the base date i.e 01/01/1970
LocalTime specificSecondTime = LocalTime.ofSecondOfDay(10000);
System.out.println("10000th second time= "+specificSecondTime);
}
private String getLoginTime()
{
if (loginTime == null)
{
return "Report";
}
Duration duration = Duration.between(loginTime, Instant.now());
LocalTime time = LocalTime.ofSecondOfDay(duration.getSeconds());
return time.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofSecondOfDay_tooHigh() {
LocalTime.ofSecondOfDay(24 * 60 * 60);
}
@Test
public void factory_ofSecondOfDay() {
LocalTime localTime = LocalTime.ofSecondOfDay(2 * 60 * 60 + 17 * 60 + 23);
check(localTime, 2, 17, 23, 0);
}
/**
* Transform an {@link AnnualTimeZoneRule} into an equivalent {@link ZoneOffsetTransitionRule}.
* This is only used for the "final rules".
*
* @param rule
* The rule to transform.
* @param dstSavingMillisBefore
* The DST offset before the first transition in milliseconds.
*/
private static ZoneOffsetTransitionRule toZoneOffsetTransitionRule(
AnnualTimeZoneRule rule, int dstSavingMillisBefore) {
DateTimeRule dateTimeRule = rule.getRule();
// Calendar.JANUARY is 0, transform it into a proper Month.
Month month = Month.JANUARY.plus(dateTimeRule.getRuleMonth());
int dayOfMonthIndicator;
// Calendar.SUNDAY is 1, transform it into a proper DayOfWeek.
DayOfWeek dayOfWeek = DayOfWeek.SATURDAY.plus(dateTimeRule.getRuleDayOfWeek());
switch (dateTimeRule.getDateRuleType()) {
case DateTimeRule.DOM:
// Transition always on a specific day of the month.
dayOfMonthIndicator = dateTimeRule.getRuleDayOfMonth();
dayOfWeek = null;
break;
case DateTimeRule.DOW_GEQ_DOM:
// ICU representation matches java.time representation.
dayOfMonthIndicator = dateTimeRule.getRuleDayOfMonth();
break;
case DateTimeRule.DOW_LEQ_DOM:
// java.time uses a negative dayOfMonthIndicator to represent "Sun<=X" or "lastSun"
// rules. ICU uses this constant and the normal day. So "lastSun" in January would
// ruleDayOfMonth = 31 in ICU and dayOfMonthIndicator = -1 in java.time.
dayOfMonthIndicator = -month.maxLength() + dateTimeRule.getRuleDayOfMonth() - 1;
break;
case DateTimeRule.DOW:
// DOW is unspecified in the documentation and seems to never be used.
throw new ZoneRulesException("Date rule type DOW is unsupported");
default:
throw new ZoneRulesException(
"Unexpected date rule type: " + dateTimeRule.getDateRuleType());
}
// Cast to int is save, as input is int.
int secondOfDay = (int) TimeUnit.MILLISECONDS.toSeconds(dateTimeRule.getRuleMillisInDay());
LocalTime time;
boolean timeEndOfDay;
if (secondOfDay == SECONDS_IN_DAY) {
time = LocalTime.MIDNIGHT;
timeEndOfDay = true;
} else {
time = LocalTime.ofSecondOfDay(secondOfDay);
timeEndOfDay = false;
}
ZoneOffsetTransitionRule.TimeDefinition timeDefinition;
switch (dateTimeRule.getTimeRuleType()) {
case DateTimeRule.WALL_TIME:
timeDefinition = ZoneOffsetTransitionRule.TimeDefinition.WALL;
break;
case DateTimeRule.STANDARD_TIME:
timeDefinition = ZoneOffsetTransitionRule.TimeDefinition.STANDARD;
break;
case DateTimeRule.UTC_TIME:
timeDefinition = ZoneOffsetTransitionRule.TimeDefinition.UTC;
break;
default:
throw new ZoneRulesException(
"Unexpected time rule type " + dateTimeRule.getTimeRuleType());
}
ZoneOffset standardOffset = millisToOffset(rule.getRawOffset());
ZoneOffset offsetBefore = millisToOffset(rule.getRawOffset() + dstSavingMillisBefore);
ZoneOffset offsetAfter = millisToOffset(
rule.getRawOffset() + rule.getDSTSavings());
return ZoneOffsetTransitionRule.of(
month, dayOfMonthIndicator, dayOfWeek, time, timeEndOfDay, timeDefinition,
standardOffset, offsetBefore, offsetAfter);
}
@Test
public void factory_ofSecondOfDay() {
LocalTime localTime = LocalTime.ofSecondOfDay(2 * 60 * 60 + 17 * 60 + 23);
check(localTime, 2, 17, 23, 0);
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofSecondOfDay_tooHigh() {
LocalTime.ofSecondOfDay(24 * 60 * 60);
}
@Test
public void factory_ofSecondOfDay() {
LocalTime localTime = LocalTime.ofSecondOfDay(2 * 60 * 60 + 17 * 60 + 23);
check(localTime, 2, 17, 23, 0);
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofSecondOfDay_tooLow() {
LocalTime.ofSecondOfDay(-1);
}
@Test
public void factory_ofSecondOfDay() {
LocalTime localTime = LocalTime.ofSecondOfDay(2 * 60 * 60 + 17 * 60 + 23);
check(localTime, 2, 17, 23, 0);
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofSecondOfDay_tooLow() {
LocalTime.ofSecondOfDay(-1);
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofSecondOfDay_tooLow() {
LocalTime.ofSecondOfDay(-1);
}