Search in sources :

Example 56 with ZoneId

use of java.time.ZoneId in project SimpleFlatMapper by arnaudroger.

the class JavaTimeConverterServiceTest method testObjectToLocalDateTime.

@Test
public void testObjectToLocalDateTime() throws Exception {
    ZoneId zoneId = ZoneId.systemDefault();
    LocalDateTime localDateTime = LocalDateTime.now(zoneId);
    testObjectToLocalDateTime(null, null);
    testObjectToLocalDateTime(localDateTime, localDateTime);
    testObjectToLocalDateTime(localDateTime.atZone(zoneId), localDateTime);
    testObjectToLocalDateTime(localDateTime.atZone(zoneId).toInstant(), localDateTime);
    testObjectToLocalDateTime(localDateTime.atZone(zoneId).toOffsetDateTime(), localDateTime);
    testObjectToLocalDateTime(Date.from(localDateTime.atZone(zoneId).toInstant()), localDateTime.truncatedTo(ChronoUnit.MILLIS));
    try {
        testObjectToLocalDateTime("a string", localDateTime);
        fail();
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) ZoneId(java.time.ZoneId) Test(org.junit.Test)

Example 57 with ZoneId

use of java.time.ZoneId in project SimpleFlatMapper by arnaudroger.

the class JavaTimeHelper method getDateTimeFormatter.

public static DateTimeFormatter getDateTimeFormatter(Object... properties) {
    ZoneId zoneId = getZoneId(properties);
    DefaultDateFormatSupplier defaultDateFormatSupplier = null;
    for (Object prop : properties) {
        DateTimeFormatter dateTimeFormatter = toDateTimeFormatter(prop, zoneId);
        if (dateTimeFormatter != null) {
            return dateTimeFormatter;
        } else if (prop instanceof DefaultDateFormatSupplier) {
            defaultDateFormatSupplier = (DefaultDateFormatSupplier) prop;
        }
    }
    if (defaultDateFormatSupplier != null) {
        return withZone(defaultDateFormatSupplier.get(), zoneId);
    }
    return null;
}
Also used : ZoneId(java.time.ZoneId) DefaultDateFormatSupplier(org.simpleflatmapper.util.date.DefaultDateFormatSupplier) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 58 with ZoneId

use of java.time.ZoneId in project SimpleFlatMapper by arnaudroger.

the class JavaTimeHelper method getDateTimeFormatters.

public static DateTimeFormatter[] getDateTimeFormatters(Object... properties) {
    List<DateTimeFormatter> dtf = new ArrayList<DateTimeFormatter>();
    ZoneId zoneId = getZoneId(properties);
    DefaultDateFormatSupplier defaultDateFormatSupplier = null;
    for (Object prop : properties) {
        DateTimeFormatter dateTimeFormatter = toDateTimeFormatter(prop, zoneId);
        if (dateTimeFormatter != null) {
            dtf.add(dateTimeFormatter);
        } else if (prop instanceof DefaultDateFormatSupplier) {
            defaultDateFormatSupplier = (DefaultDateFormatSupplier) prop;
        }
    }
    if (dtf.isEmpty()) {
        if (defaultDateFormatSupplier == null) {
            throw new IllegalStateException("No date format specified");
        }
        dtf.add(withZone(defaultDateFormatSupplier.get(), zoneId));
    }
    return dtf.toArray(new DateTimeFormatter[0]);
}
Also used : ZoneId(java.time.ZoneId) DefaultDateFormatSupplier(org.simpleflatmapper.util.date.DefaultDateFormatSupplier) ArrayList(java.util.ArrayList) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 59 with ZoneId

use of java.time.ZoneId in project SimpleFlatMapper by arnaudroger.

the class PreparedStatementFieldMapperFactoryTest method testJavaYearMonth.

@Test
public void testJavaYearMonth() throws Exception {
    java.time.YearMonth value = java.time.YearMonth.now();
    java.time.ZoneId zoneId = ZoneId.of("America/Los_Angeles");
    newFieldMapperAndMapToPS(new ConstantGetter<Object, java.time.YearMonth>(value), java.time.YearMonth.class, new JavaZoneIdProperty(zoneId));
    newFieldMapperAndMapToPS(NullGetter.<Object, java.time.YearMonth>getter(), java.time.YearMonth.class);
    verify(ps).setDate(1, new java.sql.Date(value.atDay(1).atStartOfDay(zoneId).toInstant().toEpochMilli()));
    verify(ps).setNull(2, Types.DATE);
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) JavaZoneIdProperty(org.simpleflatmapper.map.property.time.JavaZoneIdProperty) ZoneId(java.time.ZoneId) java.sql(java.sql) Test(org.junit.Test) ResultSetGetterFactoryTest(org.simpleflatmapper.jdbc.test.impl.getter.ResultSetGetterFactoryTest)

Example 60 with ZoneId

use of java.time.ZoneId in project SimpleFlatMapper by arnaudroger.

the class PreparedStatementFieldMapperFactoryTest method testJavaLocalDateTime.

// IFJAVA8_START
@Test
public void testJavaLocalDateTime() throws Exception {
    java.time.LocalDateTime value = java.time.LocalDateTime.now();
    java.time.ZoneId zoneId = ZoneId.of("America/Los_Angeles");
    newFieldMapperAndMapToPS(new ConstantGetter<Object, java.time.LocalDateTime>(value), java.time.LocalDateTime.class, new JavaZoneIdProperty(zoneId));
    newFieldMapperAndMapToPS(NullGetter.<Object, java.time.LocalDateTime>getter(), java.time.LocalDateTime.class);
    verify(ps).setTimestamp(1, new Timestamp(value.atZone(zoneId).toInstant().toEpochMilli()));
    verify(ps).setNull(2, Types.TIMESTAMP);
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) JavaZoneIdProperty(org.simpleflatmapper.map.property.time.JavaZoneIdProperty) ZoneId(java.time.ZoneId) Test(org.junit.Test) ResultSetGetterFactoryTest(org.simpleflatmapper.jdbc.test.impl.getter.ResultSetGetterFactoryTest)

Aggregations

ZoneId (java.time.ZoneId)420 Test (org.junit.Test)137 ZonedDateTime (java.time.ZonedDateTime)108 LocalDateTime (java.time.LocalDateTime)69 LocalDate (java.time.LocalDate)55 Instant (java.time.Instant)49 Test (org.testng.annotations.Test)41 ZoneOffset (java.time.ZoneOffset)37 DateTimeFormatter (java.time.format.DateTimeFormatter)32 Date (java.util.Date)29 ArrayList (java.util.ArrayList)23 LocalTime (java.time.LocalTime)21 TemporalAccessor (java.time.temporal.TemporalAccessor)15 List (java.util.List)15 HashMap (java.util.HashMap)14 Clock (java.time.Clock)13 Collectors (java.util.stream.Collectors)13 DateTimeException (java.time.DateTimeException)12 Duration (java.time.Duration)12 OffsetDateTime (java.time.OffsetDateTime)11