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

The following examples show how to use java.time.ZonedDateTime#getNano() . 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: ElasticsearchFieldResolver.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a date-time string to epoch-milliseconds. The ISO_ZONED_DATE_TIME format will be attempted first,
 * followed by the ISO_LOCAL_DATE_TIME format if the previous one fails. Examples of formats that will work:
 * 1) "2020-05-18T10:15:30.123456789"
 * 2) "2020-05-15T06:50:01.123Z"
 * 3) "2020-05-15T06:49:30.123-05:00".
 * Nanoseconds will be rounded to the nearest millisecond.
 * @param dateTimeValue is the date-time value to be converted to epoch-milliseconds.
 * @return a long value representing the epoch-milliseconds derived from dateTimeValue.
 * @throws DateTimeParseException
 */
private long toEpochMillis(String dateTimeValue)
        throws DateTimeParseException
{
    long epochSeconds;
    double nanoSeconds;

    try {
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTimeValue,
                DateTimeFormatter.ISO_ZONED_DATE_TIME.withResolverStyle(ResolverStyle.SMART));
        epochSeconds = zonedDateTime.toEpochSecond();
        nanoSeconds = zonedDateTime.getNano();
    }
    catch (DateTimeParseException error) {
        LocalDateTime localDateTime = LocalDateTime.parse(dateTimeValue,
                DateTimeFormatter.ISO_LOCAL_DATE_TIME
                        .withResolverStyle(ResolverStyle.SMART));
        epochSeconds = localDateTime.toEpochSecond(ZoneOffset.UTC);
        nanoSeconds = localDateTime.getNano();
    }

    return epochSeconds * 1000 + Math.round(nanoSeconds / 1000000);
}
 
Example 2
Source File: JavatimeTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 3
Source File: WallClockTimeImpl.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Obtain current wall clock time in UTC time zone
 * @return current wall clock time in UTC time zone
 */
public static WallClockTime utc() {
    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
    int years = now.getYear();
    int months = now.getMonthValue();
    int days = now.getDayOfMonth();
    int hours = now.getHour();
    int minutes = now.getMinute();
    double seconds = (double) now.getSecond() + ((double) now.getNano()) / 1e9;
    return new WallClockTimeImpl(years, months, days, hours, minutes, seconds);
}
 
Example 4
Source File: JavatimeTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 5
Source File: JavatimeTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 6
Source File: JavatimeTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 7
Source File: JavatimeTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 8
Source File: ZonedDateTimeMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  ZonedDateTime zonedDateTime = (ZonedDateTime) input;
  long seconds = zonedDateTime.toEpochSecond();
  int nanos = zonedDateTime.getNano();
  long microseconds = TimeUnit.SECONDS.toMicros(seconds) + TimeUnit.NANOSECONDS.toMicros(nanos);
  return TimestampValue.newBuilder(Timestamp.ofTimeMicroseconds(microseconds));
}
 
Example 9
Source File: JavatimeTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 10
Source File: JavatimeTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 11
Source File: JavatimeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 12
Source File: JavatimeTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 13
Source File: JavatimeTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 14
Source File: JavatimeTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 15
Source File: JavatimeTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 16
Source File: JavatimeTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
    return zdt.getYear() == ts.getYear() + 1900 &&
           zdt.getMonthValue() == ts.getMonth() + 1 &&
           zdt.getDayOfMonth() == ts.getDate() &&
           zdt.getHour() == ts.getHours() &&
           zdt.getMinute() == ts.getMinutes() &&
           zdt.getSecond() == ts.getSeconds() &&
           zdt.getNano() == ts.getNanos();
}
 
Example 17
Source File: SimpleFormatterNanos.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        Locale.setDefault(Locale.ENGLISH);
        LogRecord record = new LogRecord(Level.INFO, "Java Version: {0}");
        record.setLoggerName("test");
        record.setParameters(new Object[] {System.getProperty("java.version")});
        int nanos = getNanoAdjustment(record);
        long millis = record.getMillis();
        // make sure we don't have leading zeros when printing below
        // the second precision
        if (millis % MILLIS_IN_SECOND < 100) millis = millis + 100;
        // make sure we have some nanos
        if (nanos % NANOS_IN_MICRO == 0) nanos = nanos + 999;
        record.setMillis(millis);
        setNanoAdjustment(record, nanos);
        final Instant instant = record.getInstant();
        if (nanos < 0) {
            throw new RuntimeException("Unexpected negative nano adjustment: "
                    + getNanoAdjustment(record));
        }
        if (nanos >= NANOS_IN_MILLI) {
            throw new RuntimeException("Nano adjustment exceeds 1ms: "
                    + getNanoAdjustment(record));
        }
        if (millis != record.getMillis()) {
            throw new RuntimeException("Unexpected millis: " + millis + " != "
                    + record.getMillis());
        }
        if (millis != record.getInstant().toEpochMilli()) {
            throw new RuntimeException("Unexpected millis: "
                    + record.getInstant().toEpochMilli());
        }
        ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
        int zdtNanos = zdt.getNano();
        long expectedNanos = (millis % MILLIS_IN_SECOND) * NANOS_IN_MILLI + nanos;
        assertEquals(expectedNanos, instant.getNano(), "Instant.getNano()");
        assertEquals(expectedNanos, zdtNanos, "ZonedDateTime.getNano()");
        String match = "."+expectedNanos+" ";

        System.out.println("Testing with default format...");

        SimpleFormatter formatter = new SimpleFormatter();
        String str = formatter.format(record);
        if (str.contains(match)) {
            throw new RuntimeException("SimpleFormatter.format()"
                    + " string contains unexpected nanos: "
                    + "\n\tdid not expect match for: '" + match + "'"
                    + "\n\tin: " + str);
        }

        System.out.println("Nanos omitted as expected: no '"+match+"' in "+str);


        System.out.println("Changing format to print nanos...");
        System.setProperty("java.util.logging.SimpleFormatter.format",
                "%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS.%1$tN %1$Tp %2$s%n%4$s: %5$s%6$s%n");

        SimpleFormatter formatter2 = new SimpleFormatter();
        str = formatter2.format(record);
        if (!str.contains(match)) {
            throw new RuntimeException("SimpleFormatter.format()"
                    + " string does not contain expected nanos: "
                    + "\n\texpected match for: '" + match + "'"
                    + "\n\tin: " + str);
        }
        System.out.println("Found expected match for '"+match+"' in "+str);


        System.out.println("Changing format to omit nanos...");
        System.setProperty("java.util.logging.SimpleFormatter.format",
                "%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n");

        SimpleFormatter formatter3 = new SimpleFormatter();
        str = formatter3.format(record);
        String notMatch = match;
        if (str.contains(notMatch)) {
            throw new RuntimeException("SimpleFormatter.format()"
                    + " string contains unexpected nanos: "
                    + "\n\tdid not expect match for: '" + notMatch + "'"
                    + "\n\tin: " + str);
        }
        System.out.println("Nanos omitted as expected: no '"+notMatch+"' in "+str);

    }
 
Example 18
Source File: XmlFormatterNanos.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void testMatching(XMLFormatter formatter,
        LogRecord record,  Instant instant, long expectedNanos,
        boolean useInstant) {

    ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
    int zdtNanos = zdt.getNano();
    assertEquals(expectedNanos, zdtNanos, "ZonedDateTime.getNano()");

    String str = formatter.format(record);

    String match = "."+expectedNanos;
    if (str.contains(match) != useInstant) {
        throw new RuntimeException(formatter.getClass().getSimpleName()
                + ".format()"
                + " string does not contain expected nanos: "
                + "\n\texpected match for: '" + match + "'"
                + "\n\tin: \n" + str);
    }
    System.out.println("Found expected match for '"+match+"' in \n"+str);

    match = "<millis>"+instant.toEpochMilli()+"</millis>";
    if (!str.contains(match)) {
        throw new RuntimeException(formatter.getClass().getSimpleName()
                + ".format()"
                + " string does not contain expected millis: "
                + "\n\texpected match for: '" + match + "'"
                + "\n\tin: \n" + str);
    }
    System.out.println("Found expected match for '"+match+"' in \n"+str);

    match = "<nanos>";
    if (str.contains(match) != useInstant) {
        throw new RuntimeException(formatter.getClass().getSimpleName()
                + ".format()"
                + " string "
                + (useInstant
                        ? "does not contain expected nanos: "
                        : "contains unexpected nanos: ")
                + "\n\t" + (useInstant ? "expected" : "unexpected")
                + " match for: '" + match + "'"
                + "\n\tin: \n" + str);
    }
    match = "<nanos>"+getNanoAdjustment(record)+"</nanos>";
    if (str.contains(match) != useInstant) {
        throw new RuntimeException(formatter.getClass().getSimpleName()
                + ".format()"
                + " string "
                + (useInstant
                        ? "does not contain expected nanos: "
                        : "contains unexpected nanos: ")
                + "\n\t" + (useInstant ? "expected" : "unexpected")
                + " match for: '" + match + "'"
                + "\n\tin: \n" + str);
    }
    if (useInstant) {
        System.out.println("Found expected match for '"+match+"' in \n"+str);
    } else {
        System.out.println("As expected '"+match+"' is not present in \n"+str);
    }

    match = useInstant ? DateTimeFormatter.ISO_INSTANT.format(instant)
            : zdt.truncatedTo(ChronoUnit.SECONDS)
                    .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    match = "<date>"+match+"</date>";
    if (!str.contains(match)) {
        throw new RuntimeException(formatter.getClass().getSimpleName()
                + ".format()"
                + " string does not contain expected date: "
                + "\n\texpected match for: '" + match + "'"
                + "\n\tin: \n" + str);
    }
    System.out.println("Found expected match for '"+match+"' in \n"+str);

}
 
Example 19
Source File: QueryUtils.java    From catatumbo with Apache License 2.0 3 votes vote down vote up
/**
 * Converts the given OffsetDateTime to a Timestamp.
 * 
 * @param zonedDateTime
 *          the {@link ZonedDateTime} to convert
 * @return Timestamp object that is equivalent to the given OffsetDateTime.
 */
private static Timestamp toTimestamp(ZonedDateTime zonedDateTime) {
  long seconds = zonedDateTime.toEpochSecond();
  int nanos = zonedDateTime.getNano();
  long microseconds = TimeUnit.SECONDS.toMicros(seconds) + TimeUnit.NANOSECONDS.toMicros(nanos);
  return Timestamp.ofTimeMicroseconds(microseconds);
}