use of org.joda.time.LocalTime in project tests by datanucleus.
the class JodaTimeTest method testLocalTime.
/**
* Test for LocalTime persistence and retrieval.
*/
public void testLocalTime() {
try {
// Create some data we can use for access
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
LocalTime localTime1 = new LocalTime(15, 9, 26);
LocalTime localTime2 = new LocalTime(7, 9, 26);
try {
tx.begin();
JodaSample3 s = new JodaSample3(1, localTime1, localTime2);
JodaSample3 s2 = new JodaSample3(2, null, null);
em.persist(s);
em.persist(s2);
tx.commit();
} catch (Exception e) {
LOG.error("Error persisting LocalTime sample", e);
fail("Error persisting LocalTime sample");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Retrieve the data
em = emf.createEntityManager();
tx = em.getTransaction();
try {
tx.begin();
JodaSample3 s = em.find(JodaSample3.class, 1);
LocalTime lt1 = s.getLocalTime1();
assertNotNull("Retrieved LocalTime was null!", lt1);
assertEquals("Hour was wrong", 15, lt1.getHourOfDay());
assertEquals("Minute was wrong", 9, lt1.getMinuteOfHour());
assertEquals("Second was wrong", 26, lt1.getSecondOfMinute());
LocalTime lt2 = s.getLocalTime2();
assertNotNull("Retrieved LocalTime was null!", lt2);
assertEquals("Hour was wrong", 7, lt2.getHourOfDay());
assertEquals("Minute was wrong", 9, lt2.getMinuteOfHour());
assertEquals("Second was wrong", 26, lt2.getSecondOfMinute());
JodaSample3 s2 = em.find(JodaSample3.class, 2);
assertNull(s2.getLocalTime1());
assertNull(s2.getLocalTime2());
tx.commit();
} catch (Exception e) {
LOG.error("Error retrieving LocalTime data", e);
fail("Error retrieving LocalTime data : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(JodaSample3.class);
}
}
use of org.joda.time.LocalTime in project qi4j-sdk by Qi4j.
the class DateTextFieldWithPicker method earliestDate.
public DateTextFieldWithPicker earliestDate(LocalDate newEarliestDate) {
if (selectedDate != null && newEarliestDate.isAfter(selectedDate)) {
throw new IllegalArgumentException("Earliest date can't be before selected day.");
}
earliestDate = newEarliestDate;
// Input field validation - date should be _after_ minimumDate (not the same)
LocalDate minimumDate = newEarliestDate.minusDays(1);
Date convertedMinimumDate = new DateTime(minimumDate.toDateTime(new LocalTime())).toDate();
add(DateValidator.minimum(convertedMinimumDate));
return this;
}
use of org.joda.time.LocalTime in project drill by apache.
the class CompareFunctionsProcessor method visitSchemaPath.
@Override
public Boolean visitSchemaPath(SchemaPath path, LogicalExpression valueArg) throws RuntimeException {
// If valueArg is null, this might be a IS NULL/IS NOT NULL type of query
if (valueArg == null) {
this.path = path;
return true;
}
if (valueArg instanceof QuotedString) {
this.value = SqlHelper.decodeStringAsValue(((QuotedString) valueArg).value);
this.path = path;
return true;
}
if (valueArg instanceof IntExpression) {
this.value = KeyValueBuilder.initFrom(((IntExpression) valueArg).getInt());
this.path = path;
return true;
}
if (valueArg instanceof FloatExpression) {
this.value = KeyValueBuilder.initFrom(((FloatExpression) valueArg).getFloat());
this.path = path;
return true;
}
if (valueArg instanceof BooleanExpression) {
this.value = KeyValueBuilder.initFrom(((BooleanExpression) valueArg).getBoolean());
this.path = path;
return true;
}
if (valueArg instanceof Decimal28Expression) {
this.value = KeyValueBuilder.initFrom(((Decimal28Expression) valueArg).getBigDecimal());
this.path = path;
return true;
}
if (valueArg instanceof Decimal38Expression) {
this.value = KeyValueBuilder.initFrom(((Decimal38Expression) valueArg).getBigDecimal());
this.path = path;
return true;
}
if (valueArg instanceof DoubleExpression) {
this.value = KeyValueBuilder.initFrom(((DoubleExpression) valueArg).getDouble());
this.path = path;
return true;
}
if (valueArg instanceof LongExpression) {
this.value = KeyValueBuilder.initFrom(((LongExpression) valueArg).getLong());
this.path = path;
return true;
}
if (valueArg instanceof DateExpression) {
long d = ((DateExpression) valueArg).getDate();
final long MILLISECONDS_IN_A_DAY = (long) 1000 * 60 * 60 * 24;
int daysSinceEpoch = (int) (d / MILLISECONDS_IN_A_DAY);
this.value = KeyValueBuilder.initFrom(ODate.fromDaysSinceEpoch(daysSinceEpoch));
this.path = path;
return true;
}
if (valueArg instanceof TimeExpression) {
int t = ((TimeExpression) valueArg).getTime();
LocalTime lT = LocalTime.fromMillisOfDay(t);
this.value = KeyValueBuilder.initFrom(new OTime(lT.getHourOfDay(), lT.getMinuteOfHour(), lT.getSecondOfMinute(), lT.getMillisOfSecond()));
this.path = path;
return true;
}
// See com.mapr.db.impl.ConditionImpl.is(FieldPath path, QueryCondition.Op op, BigDecimal value) method
if (valueArg instanceof VarDecimalExpression) {
this.value = KeyValueBuilder.initFrom(((VarDecimalExpression) valueArg).getBigDecimal().doubleValue());
this.path = path;
return true;
}
if (valueArg instanceof TimeStampExpression) {
return visitTimestampExpr(path, (TimeStampExpression) valueArg);
}
return false;
}
use of org.joda.time.LocalTime in project SimpleFlatMapper by arnaudroger.
the class JodaTimeConverterFactoryProducer method produce.
@Override
public void produce(Consumer<? super ContextualConverterFactory<?, ?>> consumer) {
// Date to joda time
factoryConverter(consumer, new AbstractContextualConverterFactory<Date, DateTime>(Date.class, DateTime.class) {
@Override
public ContextualConverter<Date, DateTime> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
return new DateToJodaDateTimeConverter(JodaTimeHelper.getDateTimeZoneOrDefault(params));
}
});
constantConverter(consumer, Date.class, Instant.class, new DateToJodaInstantConverter());
constantConverter(consumer, Date.class, LocalDate.class, new DateToJodaLocalDateConverter());
constantConverter(consumer, Date.class, LocalDateTime.class, new DateToJodaLocalDateTimeConverter());
constantConverter(consumer, Date.class, LocalTime.class, new DateToJodaLocalTimeConverter());
// joda time to date
constantConverter(consumer, DateTime.class, Date.class, new JodaDateTimeTojuDateConverter());
constantConverter(consumer, Instant.class, Date.class, new JodaInstantTojuDateConverter());
constantConverter(consumer, LocalDate.class, Date.class, new JodaLocalDateTojuDateConverter());
factoryConverter(consumer, new AbstractContextualConverterFactory<LocalDateTime, Date>(LocalDateTime.class, Date.class) {
@Override
public ContextualConverter<LocalDateTime, Date> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
return new JodaLocalDateTimeTojuDateConverter(JodaTimeHelper.getDateTimeZoneOrDefault(params));
}
});
factoryConverter(consumer, new AbstractContextualConverterFactory<LocalTime, Date>(LocalTime.class, Date.class) {
@Override
public ContextualConverter<LocalTime, Date> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
return new JodaLocalTimeTojuDateConverter(JodaTimeHelper.getDateTimeZoneOrDefault(params));
}
});
// char sequence to joda time
factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, DateTime>(CharSequence.class, DateTime.class) {
@Override
protected ContextualConverter<CharSequence, DateTime> newConverter(DateTimeFormatter formatter) {
return new CharSequenceToJodaDateTimeConverter(formatter);
}
});
factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, Instant>(CharSequence.class, Instant.class) {
@Override
protected ContextualConverter<CharSequence, Instant> newConverter(DateTimeFormatter formatter) {
return new CharSequenceToJodaInstantConverter(formatter);
}
});
factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, LocalDate>(CharSequence.class, LocalDate.class) {
@Override
protected ContextualConverter<CharSequence, LocalDate> newConverter(DateTimeFormatter formatter) {
return new CharSequenceToJodaLocalDateConverter(formatter);
}
});
factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, LocalDateTime>(CharSequence.class, LocalDateTime.class) {
@Override
protected ContextualConverter<CharSequence, LocalDateTime> newConverter(DateTimeFormatter formatter) {
return new CharSequenceToJodaLocalDateTimeConverter(formatter);
}
});
factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, LocalTime>(CharSequence.class, LocalTime.class) {
@Override
protected ContextualConverter<CharSequence, LocalTime> newConverter(DateTimeFormatter formatter) {
return new CharSequenceToJodaLocalTimeConverter(formatter);
}
});
factoryConverter(consumer, new AbstractContextualConverterFactory<ReadableInstant, String>(ReadableInstant.class, String.class) {
@Override
public ContextualConverter<? super ReadableInstant, String> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
DateTimeFormatter dateTimeFormatter = JodaTimeHelper.getDateTimeFormatter(params);
if (dateTimeFormatter != null) {
return new JodaReadableInstantToStringConverter(dateTimeFormatter);
} else {
return ToStringConverter.INSTANCE;
}
}
});
factoryConverter(consumer, new AbstractContextualConverterFactory<ReadablePartial, String>(ReadablePartial.class, String.class) {
@Override
public ContextualConverter<? super ReadablePartial, String> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
DateTimeFormatter dateTimeFormatter = JodaTimeHelper.getDateTimeFormatter(params);
if (dateTimeFormatter != null) {
return new JodaReadablePartialToStringConverter(dateTimeFormatter);
} else {
return ToStringConverter.INSTANCE;
}
}
});
// IFJAVA8_START
constantConverter(consumer, LocalDateTime.class, java.time.LocalDateTime.class, new org.simpleflatmapper.converter.joda.impl.time.JodaLocalDateTimeTojuLocalDateTimeConverter());
constantConverter(consumer, LocalDate.class, java.time.LocalDate.class, new org.simpleflatmapper.converter.joda.impl.time.JodaLocalDateTojuLocalDateConverter());
constantConverter(consumer, LocalTime.class, java.time.LocalTime.class, new org.simpleflatmapper.converter.joda.impl.time.JodaLocalTimeTojuLocalTimeConverter());
// IFJAVA8_END
constantConverter(consumer, LocalTime.class, Long.class, new ContextualConverter<LocalTime, Long>() {
@Override
public Long convert(LocalTime in, Context context) throws Exception {
if (in == null)
return null;
return TimeUnit.MILLISECONDS.toNanos(in.getMillisOfDay());
}
});
}
use of org.joda.time.LocalTime in project mapstruct by mapstruct.
the class JodaTimeTest method shouldMapIncompleteLocalTimeToXmlGregorianCalendar.
@ProcessorTest
@WithClasses(LocalTimeToXmlGregorianCalendar.class)
public void shouldMapIncompleteLocalTimeToXmlGregorianCalendar() {
LocalTimeBean in = new LocalTimeBean();
LocalTime dt = new LocalTime(1, 1, 0, 100);
in.setLocalTime(dt);
XmlGregorianCalendarBean res = LocalTimeToXmlGregorianCalendar.INSTANCE.toXmlGregorianCalendarBean(in);
assertThat(res.getxMLGregorianCalendar().getYear()).isEqualTo(DatatypeConstants.FIELD_UNDEFINED);
assertThat(res.getxMLGregorianCalendar().getMonth()).isEqualTo(DatatypeConstants.FIELD_UNDEFINED);
assertThat(res.getxMLGregorianCalendar().getDay()).isEqualTo(DatatypeConstants.FIELD_UNDEFINED);
assertThat(res.getxMLGregorianCalendar().getHour()).isEqualTo(1);
assertThat(res.getxMLGregorianCalendar().getMinute()).isEqualTo(1);
assertThat(res.getxMLGregorianCalendar().getSecond()).isEqualTo(0);
assertThat(res.getxMLGregorianCalendar().getMillisecond()).isEqualTo(100);
assertThat(res.getxMLGregorianCalendar().getTimezone()).isEqualTo(DatatypeConstants.FIELD_UNDEFINED);
}
Aggregations