Java Code Examples for java.time.Instant#atOffset()

The following examples show how to use java.time.Instant#atOffset() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 2
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 3
Source Project: jdk8u60   File: TCKInstant.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 4
@Nonnull
@CheckReturnValue
public static OffsetDateTime creationTimeOf(final long id) {
    final long discordTimestamp = id >> 22;
    final Instant instant = Instant.ofEpochMilli(discordTimestamp + DISCORD_EPOCH);
    return instant.atOffset(ZoneOffset.UTC);
}
 
Example 5
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 6
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 7
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 8
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 9
Source Project: hottub   File: TCKInstant.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 10
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 11
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 12
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 13
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 14
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 15
Source Project: j2objc   File: TCKInstant.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 16
private OffsetDateTime timeIs(int seconds) {
    Instant instant = Instant.ofEpochSecond(seconds);
    when(clock.instant()).thenReturn(instant);
    return instant.atOffset(ZoneOffset.UTC);
}
 
Example 17
Source Project: java-timeseries   File: TimeSpec.java    License: MIT License 4 votes vote down vote up
@Test
public void whenToInstantThenCorrectInstantReturned() {
  Instant instant = Instant.ofEpochSecond(3000L);
  Time time = new Time(instant.atOffset(ZoneOffset.UTC));
  assertThat(time.toInstant(), is(instant));
}
 
Example 18
Source Project: ShedLock   File: Utils.java    License: Apache License 2.0 4 votes vote down vote up
public static String toIsoString(@NonNull Instant instant) {
    OffsetDateTime utc = instant.atOffset(ZoneOffset.UTC);
    return formatter.format(utc);
}
 
Example 19
/**
 * Schedule a re-invocation of the executing handler no less than 1 minute from
 * now
 *
 *
 * /** Creates a cron(..) expression for a single instance at Now+minutesFromNow
 * NOTE: CloudWatchEvents only support a 1minute granularity for re-invoke
 * Anything less should be handled inside the original handler request
 *
 * Expression is of form cron(minutes, hours, day-of-month, month, day-of-year,
 * year) where day-of-year is not necessary when the day-of-month and
 * month-of-year fields are supplied
 *
 * @param minutesFromNow The number of minutes from now for building the cron
 *            expression
 * @return A cron expression for use with CloudWatchEvents putRule(..) API
 */
public String generateOneTimeCronExpression(final int minutesFromNow) {
    // Add additional 1 minute, since rule can be created after scheduled time, like
    // "13:40:59" -> cron(41 13 30)
    Instant instant = Instant.now(this.clock).plusSeconds(60L * (minutesFromNow + 1));
    OffsetDateTime odt = instant.atOffset(ZoneOffset.UTC);

    return DateTimeFormatter.ofPattern("'cron('m H d M ? u')'").format(odt);
}