/** * Converts this object to a DateTime using the default zone. * <p> * When the time zone is applied, the local date-time may be affected by daylight saving. * In a daylight saving gap, when the local time does not exist, * this method will throw an exception. * In a daylight saving overlap, when the same local time occurs twice, * this method returns the first occurrence of the local time. * * @return <code>this</code> * @throws IllegalInstantException if the local time does not exist when the time zone is applied */ public DateTime toDateTime() { return toDateTime((DateTimeZone) null); }
/** * Converts this object to a DateTime using the default zone. * <p> * When the time zone is applied, the local date-time may be affected by daylight saving. * In a daylight saving gap, when the local time does not exist, * this method will throw an exception. * In a daylight saving overlap, when the same local time occurs twice, * this method returns the first occurrence of the local time. * * @return <code>this</code> * @throws IllegalInstantException if the local time does not exist when the time zone is applied */ public DateTime toDateTime() { return toDateTime((DateTimeZone) null); }
/** * Checks if the given {@link LocalDateTime} is within a gap. * <p> * When switching from standard time to Daylight Savings Time there is * typically a gap where a clock hour is missing. This method identifies * whether the local datetime refers to such a gap. * * @param localDateTime the time to check, not null * @return true if the given datetime refers to a gap * @since 1.6 */ public boolean isLocalDateTimeGap(LocalDateTime localDateTime) { if (isFixed()) { return false; } try { localDateTime.toDateTime(this); return false; } catch (IllegalInstantException ex) { return true; } }
/** * Checks if the given {@link LocalDateTime} is within a gap. * <p> * When switching from standard time to Daylight Savings Time there is * typically a gap where a clock hour is missing. This method identifies * whether the local datetime refers to such a gap. * * @param localDateTime the time to check, not null * @return true if the given datetime refers to a gap * @since 1.6 */ public boolean isLocalDateTimeGap(LocalDateTime localDateTime) { if (isFixed()) { return false; } try { localDateTime.toDateTime(this); return false; } catch (IllegalInstantException ex) { return true; } }
DateTime myDateBorken = ldt.toDateTime(dtz); } catch (IllegalArgumentException iae) { System.out.println("Sure enough, invalid instant due to time zone offset transition!"); DateTime myDate = ldt.toDateTime(dtz); System.out.println("No problem: "+myDate);
private DateTime parseDateTime(final String scheduleDate, final String scheduleTime) { // scheduleTime: 12,00,pm,PDT final String[] parts = scheduleTime.split(",", -1); int hour = Integer.parseInt(parts[0]); final int minutes = Integer.parseInt(parts[1]); final boolean isPm = parts[2].equalsIgnoreCase("pm"); final DateTimeZone timezone = parts[3].equals("UTC") ? DateTimeZone.UTC : DateTimeZone.getDefault(); // scheduleDate: 02/10/2013 DateTime day = null; if (scheduleDate == null || scheduleDate.trim().length() == 0) { day = new LocalDateTime().toDateTime(); } else { day = DateTimeFormat.forPattern("MM/dd/yyyy") .withZone(timezone).parseDateTime(scheduleDate); } hour %= 12; if (isPm) { hour += 12; } final DateTime firstSchedTime = day.withHourOfDay(hour).withMinuteOfHour(minutes).withSecondOfMinute(0); return firstSchedTime; }
private Optional<Timestamp> tryParseWithFormat(String strValue) { checkState(fmt != null); if (startingDateValue != null) { // reset value in case any date fields are missing from the date pattern MutableDateTime mdt = new MutableDateTime( startingDateValue, ISOChronology.getInstanceUTC()); // Using parseInto() avoids throwing exception when parsing, // allowing fallback to default timestamp parsing if custom patterns fail. int ret = fmt.parseInto(mdt, strValue, 0); // Only accept parse results if we parsed the entire string if (ret == strValue.length()) { return Optional.of(Timestamp.ofEpochMilli(mdt.getMillis())); } return Optional.empty(); } try { LocalDateTime dt = fmt.parseLocalDateTime(strValue); return Optional.of( Timestamp.ofEpochMilli( dt.toDateTime(ISOChronology.getInstanceUTC().getZone()).getMillis())); } catch (IllegalArgumentException e) { return Optional.empty(); } }
out.writeLong(dateTime.toDateTime(DateTimeZone.forTimeZone(JSON.defaultTimeZone)).toInstant().getMillis());
/** * Converts this object to a DateTime using the default zone. * <p> * This method will throw an exception if the datetime that would be * created does not exist when the time zone is taken into account. * * @return <code>this</code> */ public DateTime toDateTime() { return toDateTime((DateTimeZone) null); }
out.writeLong(dateTime.toDateTime(DateTimeZone.forTimeZone(JSON.defaultTimeZone)).toInstant().getMillis());
@Override public long convertToMillis(LocalDateTime value) { return value.toDateTime().getMillis(); }
@Override protected String toJsonNanos(LocalDateTime value) { return String.valueOf(value.toDateTime().getMillis()); }
@Override public Timestamp convertToTimestamp(LocalDateTime t) { return new Timestamp(t.toDateTime().getMillis()); }
@Override public Object toJdbcType(Object value) { if (value instanceof LocalDateTime) { return new Timestamp(((LocalDateTime) value).toDateTime().getMillis()); } return BasicTypeConverter.toTimestamp(value); }
LocalDateTime local = new LocalDateTime(2015, 10, 02, 11, 31, 40); DateTime utc = local.toDateTime(DateTimeZone.UTC); long secondsSinceEpoch = utc.getMillis() / 1000;
/** * Checks if the given {@link LocalDateTime} is within a gap. * <p> * When switching from standard time to Daylight Savings Time there is * typically a gap where a clock hour is missing. This method identifies * whether the local datetime refers to such a gap. * * @param localDateTime the time to check, not null * @return true if the given datetime refers to a gap * @since 1.6 */ public boolean isLocalDateTimeGap(LocalDateTime localDateTime) { if (isFixed()) { return false; } try { localDateTime.toDateTime(this); return false; } catch (IllegalArgumentException ex) { return true; } }
public DateTime convert(Object val) throws ConverterException { if (val == null){ return null; } try { // Joda has it's own pluggable converters infrastructure // it will throw IllegalArgumentException if can't convert // look @ org.joda.time.convert.ConverterManager return new LocalDateTime(val).toDateTime(timeZone); } catch (IllegalArgumentException ex) { throw new ConverterException("Error while converting type " + val.getClass().toString() + " to jodatime", ex); } }
@Override public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider provider) throws IOException { switch (_serializationShape(provider)) { case FORMAT_STRING: gen.writeString(_format.createFormatter(provider).print(value)); break; case FORMAT_TIMESTAMP: { // copied from `LocalDateTimeDeserializer`... DateTimeZone tz = _format.isTimezoneExplicit() ? _format.getTimeZone() : DateTimeZone.forTimeZone(provider.getTimeZone()); gen.writeNumber(value.toDateTime(tz).getMillis()); } break; case FORMAT_ARRAY: // Timestamp here actually means an array of values gen.writeStartArray(); gen.writeNumber(value.year().get()); gen.writeNumber(value.monthOfYear().get()); gen.writeNumber(value.dayOfMonth().get()); gen.writeNumber(value.hourOfDay().get()); gen.writeNumber(value.minuteOfHour().get()); gen.writeNumber(value.secondOfMinute().get()); gen.writeNumber(value.millisOfSecond().get()); gen.writeEndArray(); } } }
@Override protected DateTime fromConvertedColumns(Object[] convertedColumns) { LocalDateTime datePart = (LocalDateTime) convertedColumns[0]; DateTimeZone zone = (DateTimeZone) convertedColumns[1]; DateTime result; if (datePart == null) { result = null; } else { result = datePart.toDateTime(zone); } return result; }
public static Date convertJodaTimezone(LocalDateTime date, String srcTz, String destTz) { DateTime srcDateTime = date.toDateTime(DateTimeZone.forID(srcTz)); DateTime dstDateTime = srcDateTime.withZone(DateTimeZone.forID(destTz)); return dstDateTime.toLocalDateTime().toDateTime().toDate(); }