Java Code Examples for java.time.ZonedDateTime#withZoneSameInstant()

The following examples show how to use java.time.ZonedDateTime#withZoneSameInstant() . 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 check out the related API usage on the sidebar.
Example 1
Source File: TimeUtil.java    From alpaca-java with MIT License 6 votes vote down vote up
/**
 * From date time string.
 *
 * @param dateTimeString the date time string
 *
 * @return the local date time using the system time zone
 */
public static LocalDateTime fromDateTimeString(String dateTimeString) {
    LocalDateTime ldt = LocalDateTime.parse(dateTimeString, inputDateTimeFormatter);

    ZonedDateTime ldtZoned;

    if (dateTimeString.endsWith("Z")) {
        ldtZoned = ldt.atZone(ZoneId.of("UTC"));
    } else {
        ldtZoned = ldt.atZone(ZoneId.of("America/New_York"));
    }

    ZonedDateTime localTimeZoned = ldtZoned.withZoneSameInstant(ZoneId.systemDefault());

    return localTimeZoned.toLocalDateTime();
}
 
Example 2
Source File: TCKZonedDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZoneSameInstant() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withZoneSameInstant(ZONE_0200);
    ZonedDateTime expected = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.plusHours(1), ZONE_0200);
    assertEquals(test, expected);
}
 
Example 3
Source File: TCKZonedDateTime.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZoneSameInstant() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withZoneSameInstant(ZONE_0200);
    ZonedDateTime expected = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.plusHours(1), ZONE_0200);
    assertEquals(test, expected);
}
 
Example 4
Source File: TimeUtil.java    From alpaca-java with MIT License 5 votes vote down vote up
/**
 * To date time string.
 *
 * @param ldt the ldt
 *
 * @return the string
 */
public static String toDateTimeString(LocalDateTime ldt) {
    ZonedDateTime ldtZoned = ldt.atZone(ZoneId.systemDefault());

    ZonedDateTime localTimeZoned = ldtZoned.withZoneSameInstant(ZoneId.of("UTC"));

    return outputDateTimeFormatter.format(localTimeZoned);
}
 
Example 5
Source File: ZonedDateTimeAdapter.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ZonedDateTime unmarshal(String value) throws Exception {
  if (value == null) {
    return null;
  }
  if (value.matches(".*([Z]|[+-][0-9]{1,2}:[0-9]{1,2})$")) {
    return ZonedDateTime.parse(value).withZoneSameInstant(ZoneOffset.UTC);
  } else {
    LocalDateTime local = LocalDateTime.parse(value, localFormat);
    ZonedDateTime localZ = ZonedDateTime.of(local, getZoneId());
    return localZ.withZoneSameInstant(ZoneOffset.UTC);
  }
}
 
Example 6
Source File: DateUtilsUT.java    From fredbet with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Test
public void parseBuildTimestampToLocalDateTime() {
    String buildTimestamp = "2018-01-01 17:01 +00:00";

    ZonedDateTime parseToZonedDateTime = ZonedDateTime.parse(buildTimestamp, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm ZZZZZ"));
    LOG.debug("parseToZonedDateTime: {}", parseToZonedDateTime);

    ZonedDateTime converted = parseToZonedDateTime.withZoneSameInstant(ZoneOffset.systemDefault());
    LOG.debug("converted: {}", converted);
}
 
Example 7
Source File: InstantArgumentTest.java    From dropwizard-java8 with Apache License 2.0 5 votes vote down vote up
@Test
public void applyCalendar() throws Exception {
    final ZoneId systemDefault = ZoneId.systemDefault();

    // this test only asserts that a calendar was passed in. Not that the JDBC driver
    // will do the right thing and adjust the time.
    final ZonedDateTime zonedDateTime = ZonedDateTime.parse("2012-12-21T00:00:00.000Z");
    final ZonedDateTime expected = zonedDateTime.withZoneSameInstant(systemDefault);
    final Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone(systemDefault));

    new InstantArgument(zonedDateTime.toInstant(), Optional.of(calendar)).apply(1, statement, context);

    Mockito.verify(statement).setTimestamp(1, Timestamp.from(expected.toInstant()), calendar);
}
 
Example 8
Source File: FormatDateExpressionProcessor.java    From vividus with Apache License 2.0 5 votes vote down vote up
private ZonedDateTime updateTimeZone(Matcher expressionMatcher, ZonedDateTime zonedDate)
{
    String outputTimeZone = expressionMatcher.group(OUTPUT_TIMEZONE_GROUP);
    if (outputTimeZone != null)
    {
        return zonedDate.withZoneSameInstant(ZoneId.of(outputTimeZone));
    }
    return zonedDate;
}
 
Example 9
Source File: TCKZonedDateTime.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_withZoneSameInstant() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withZoneSameInstant(ZONE_0200);
    ZonedDateTime expected = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.plusHours(1), ZONE_0200);
    assertEquals(test, expected);
}
 
Example 10
Source File: TCKZonedDateTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withZoneSameInstant_noChange() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withZoneSameInstant(ZONE_0100);
    assertEquals(test, base);
}
 
Example 11
Source File: TCKZonedDateTime.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test(expected=NullPointerException.class)
public void test_withZoneSameInstant_null() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    base.withZoneSameInstant(null);
}
 
Example 12
Source File: TCKZonedDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_withZoneSameInstant_null() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    base.withZoneSameInstant(null);
}
 
Example 13
Source File: TCKZonedDateTime.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_withZoneSameInstant_null() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    base.withZoneSameInstant(null);
}
 
Example 14
Source File: TCKZonedDateTime.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_withZoneSameInstant_null() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    base.withZoneSameInstant(null);
}
 
Example 15
Source File: TCKZonedDateTime.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_withZoneSameInstant_null() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    base.withZoneSameInstant(null);
}
 
Example 16
Source File: TCKZonedDateTime.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_withZoneSameInstant_null() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    base.withZoneSameInstant(null);
}
 
Example 17
Source File: ExtensionUtils.java    From alf.io with GNU General Public License v3.0 4 votes vote down vote up
public static String formatDateTime(ZonedDateTime dateTime, String formatPattern, boolean utc) {
    var dateTimeToFormat = utc ? dateTime.withZoneSameInstant(ZoneId.of("UTC")) : dateTime;
    return dateTimeToFormat.format(DateTimeFormatter.ofPattern(formatPattern));
}
 
Example 18
Source File: TCKZonedDateTime.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withZoneSameInstant_noChange() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withZoneSameInstant(ZONE_0100);
    assertEquals(test, base);
}
 
Example 19
Source File: TCKZonedDateTime.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_withZoneSameInstant_null() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    base.withZoneSameInstant(null);
}
 
Example 20
Source File: ZonedDateTimeParameter.java    From mariadb-connector-j with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Constructor.
 *
 * @param tz zone date time
 * @param serverZoneId server session zoneId
 * @param fractionalSeconds must fractional Seconds be send to database.
 * @param options session options
 */
public ZonedDateTimeParameter(
    ZonedDateTime tz, ZoneId serverZoneId, boolean fractionalSeconds, Options options) {
  ZoneId zoneId = options.useLegacyDatetimeCode ? ZoneOffset.systemDefault() : serverZoneId;
  this.tz = tz.withZoneSameInstant(zoneId);
  this.fractionalSeconds = fractionalSeconds;
}