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
}
}
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;
}
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]);
}
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);
}
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);
}
Aggregations