/** * Obtains a {@code LocalDateTime} set to the current system millisecond time * using <code>ISOChronology</code> in the default time zone. * The resulting object does not use the zone. * * @return the current date, not null * @since 2.0 */ public static LocalDateTime now() { return new LocalDateTime(); }
/** * Obtains a {@code LocalDateTime} set to the current system millisecond time * using <code>ISOChronology</code> in the default time zone. * The resulting object does not use the zone. * * @return the current date, not null * @since 2.0 */ public static LocalDateTime now() { return new LocalDateTime(); }
/** * Obtains a {@code LocalDateTime} set to the current system millisecond time * using the specified chronology. * The resulting object does not use the zone. * * @param chronology the chronology, not null * @return the current date, not null * @since 2.0 */ public static LocalDateTime now(Chronology chronology) { if (chronology == null) { throw new NullPointerException("Chronology must not be null"); } return new LocalDateTime(chronology); }
/** * Obtains a {@code LocalDateTime} set to the current system millisecond time * using <code>ISOChronology</code> in the specified time zone. * The resulting object does not use the zone. * * @param zone the time zone, not null * @return the current date, not null * @since 2.0 */ public static LocalDateTime now(DateTimeZone zone) { if (zone == null) { throw new NullPointerException("Zone must not be null"); } return new LocalDateTime(zone); }
/** * Obtains a {@code LocalDateTime} set to the current system millisecond time * using <code>ISOChronology</code> in the specified time zone. * The resulting object does not use the zone. * * @param zone the time zone, not null * @return the current date, not null * @since 2.0 */ public static LocalDateTime now(DateTimeZone zone) { if (zone == null) { throw new NullPointerException("Zone must not be null"); } return new LocalDateTime(zone); }
/** * Obtains a {@code LocalDateTime} set to the current system millisecond time * using the specified chronology. * The resulting object does not use the zone. * * @param chronology the chronology, not null * @return the current date, not null * @since 2.0 */ public static LocalDateTime now(Chronology chronology) { if (chronology == null) { throw new NullPointerException("Chronology must not be null"); } return new LocalDateTime(chronology); }
/** * Converts this object to a <code>LocalDateTime</code> with * the same datetime and chronology. * * @return a LocalDateTime with the same datetime and chronology * @since 1.3 */ public LocalDateTime toLocalDateTime() { return new LocalDateTime(getMillis(), getChronology()); }
/** * Converts this object to a <code>LocalDateTime</code> with * the same datetime and chronology. * * @return a LocalDateTime with the same datetime and chronology * @since 1.3 */ public LocalDateTime toLocalDateTime() { return new LocalDateTime(getMillis(), getChronology()); }
@Override public java.sql.Timestamp getPrimitiveJavaObject(Object o) { final TimeStampHolder h = (TimeStampHolder) o; org.joda.time.LocalDateTime dateTime = new org.joda.time.LocalDateTime(h.value, org.joda.time.DateTimeZone.UTC); // use "toDate()" to get java.util.Date object with exactly the same fields as this Joda date-time. // See more in Javadoc for "LocalDateTime#toDate()" return new java.sql.Timestamp(dateTime.toDate().getTime()); }
@Override public java.sql.Timestamp getPrimitiveJavaObject(Object o) { if (o == null) { return null; } final NullableTimeStampHolder h = (NullableTimeStampHolder) o; org.joda.time.LocalDateTime dateTime = new org.joda.time.LocalDateTime(h.value, org.joda.time.DateTimeZone.UTC); // use "toDate()" to get java.util.Date object with exactly the same fields as this Joda date-time. // See more in Javadoc for "LocalDateTime#toDate()" return new java.sql.Timestamp(dateTime.toDate().getTime()); }
@Override public TimestampWritable getPrimitiveWritableObject(Object o) { if (o == null) { return null; } final NullableTimeStampHolder h = (NullableTimeStampHolder) o; org.joda.time.LocalDateTime dateTime = new org.joda.time.LocalDateTime(h.value, org.joda.time.DateTimeZone.UTC); // use "toDate()" to get java.util.Date object with exactly the same fields as this Joda date-time. // See more in Javadoc for "LocalDateTime#toDate()" return new TimestampWritable(new java.sql.Timestamp(dateTime.toDate().getTime())); }
/** * Returns a copy of this datetime with different local millis. * <p> * The returned object will be a new instance of the same type. * Only the millis will change, the chronology is kept. * The returned object will be either be a new instance or <code>this</code>. * * @param newMillis the new millis, from 1970-01-01T00:00:00 * @return a copy of this datetime with different millis */ LocalDateTime withLocalMillis(long newMillis) { return (newMillis == getLocalMillis() ? this : new LocalDateTime(newMillis, getChronology())); }
/** * Handle broken serialization from other tools. * @return the resolved object, not null */ private Object readResolve() { if (iChronology == null) { return new LocalDateTime(iLocalMillis, ISOChronology.getInstanceUTC()); } if (DateTimeZone.UTC.equals(iChronology.getZone()) == false) { return new LocalDateTime(iLocalMillis, iChronology.withUTC()); } return this; }
/** * Handle broken serialization from other tools. * @return the resolved object, not null */ private Object readResolve() { if (iChronology == null) { return new LocalDateTime(iLocalMillis, ISOChronology.getInstanceUTC()); } if (DateTimeZone.UTC.equals(iChronology.getZone()) == false) { return new LocalDateTime(iLocalMillis, iChronology.withUTC()); } return this; }
/** * Returns a copy of this datetime with different local millis. * <p> * The returned object will be a new instance of the same type. * Only the millis will change, the chronology is kept. * The returned object will be either be a new instance or <code>this</code>. * * @param newMillis the new millis, from 1970-01-01T00:00:00 * @return a copy of this datetime with different millis */ LocalDateTime withLocalMillis(long newMillis) { return (newMillis == getLocalMillis() ? this : new LocalDateTime(newMillis, getChronology())); }
private boolean isDatePatternHourly(String datePattern) { DateTimeFormatter formatter = DateTimeFormat.forPattern(datePattern); LocalDateTime refDateTime = new LocalDateTime(2017, 01, 01, 10, 0, 0); String refDateTimeString = refDateTime.toString(formatter); LocalDateTime refDateTimeAtStartOfDay = refDateTime.withHourOfDay(0); String refDateTimeStringAtStartOfDay = refDateTimeAtStartOfDay.toString(formatter); return !refDateTimeString.equals(refDateTimeStringAtStartOfDay); }
public static long resolveSegment(long offset, int scale) { LocalDateTime localDateTime = new LocalDateTime(offset); long year = year(localDateTime); long month = month(localDateTime); long day = day(localDateTime); long hour = hour(localDateTime); long minute = minute(localDateTime); minute = minute - (minute % scale); return year + month + day + hour + minute; }
@Test public void testBindLocalDateTime() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateTime", new LocalDateTime(2009, 10, 31, 12, 0)); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); String value = binder.getBindingResult().getFieldValue("localDateTime").toString(); assertTrue(value.startsWith("10/31/09")); assertTrue(value.endsWith("12:00 PM")); }
@Test public void testBindLocalDateTimeAnnotated() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateTimeAnnotated", new LocalDateTime(2009, 10, 31, 12, 0)); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); String value = binder.getBindingResult().getFieldValue("localDateTimeAnnotated").toString(); assertTrue(value.startsWith("Oct 31, 2009")); assertTrue(value.endsWith("12:00 PM")); }
@Test public void testBindDateTimeWithSpecificStyle() { JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar(); registrar.setDateTimeStyle("MM"); setup(registrar); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateTime", new LocalDateTime(2009, 10, 31, 12, 0)); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); String value = binder.getBindingResult().getFieldValue("localDateTime").toString(); assertTrue(value.startsWith("Oct 31, 2009")); assertTrue(value.endsWith("12:00:00 PM")); }