@Override public Instant convert(DateTime source) { return source.toInstant(); } }
@Override public Instant convert(DateTime source) { return source.toInstant(); } }
/** * Parses an {@code Instant} from the specified string using a formatter. * * @param str the string to parse, not null * @param formatter the formatter to use, not null * @since 2.0 */ public static Instant parse(String str, DateTimeFormatter formatter) { return formatter.parseDateTime(str).toInstant(); }
/** * Parses an {@code Instant} from the specified string using a formatter. * * @param str the string to parse, not null * @param formatter the formatter to use, not null * @since 2.0 */ public static Instant parse(String str, DateTimeFormatter formatter) { return formatter.parseDateTime(str).toInstant(); }
out.writeLong(dateTime.toDateTime(DateTimeZone.forTimeZone(JSON.defaultTimeZone)).toInstant().getMillis());
out.writeLong(dateTime.toDateTime(DateTimeZone.forTimeZone(JSON.defaultTimeZone)).toInstant().getMillis());
/** * Parses a {@code Instant} from the specified string using a formatter. * * @param str the string to parse, not null * @param formatter the formatter to use, not null * @since 2.0 */ public static Instant parse(String str, DateTimeFormatter formatter) { return formatter.parseDateTime(str).toInstant(); }
public static Partial resolveDowToDay(Partial p) { if (p.isSupported(DateTimeFieldType.dayOfWeek())) { if (!p.isSupported(DateTimeFieldType.dayOfMonth())) { if (p.isSupported(DateTimeFieldType.weekOfWeekyear()) && (p.isSupported(DateTimeFieldType.year()))) { // Convert from year to weekyear (to avoid weirdness when the weekyear and year don't match at the beginning of the year) Partial pwy = withWeekYear(p); Instant t2 = getInstant(pwy); DateTime t1 = pwy.toDateTime(t2); Partial res = getPartial(t1.toInstant(), EMPTY_ISO_PARTIAL); DateTimeFieldType mostSpecific = getMostSpecific(p); res = discardMoreSpecificFields(res, mostSpecific.getDurationType()); return res; } } } return p; }
public static Partial resolveWeek(Partial p1, Partial p2) { if (isCompatible(p1,p2)) { if (!p1.isSupported(DateTimeFieldType.dayOfMonth())) { if (p2.isSupported(DateTimeFieldType.dayOfMonth()) && p2.isSupported(DateTimeFieldType.monthOfYear()) && p2.isSupported(DateTimeFieldType.year())) { Instant t2 = getInstant(p2); DateTime t1 = p1.toDateTime(t2); return getPartial(t1.toInstant(), p1.without(DateTimeFieldType.dayOfMonth()).without(DateTimeFieldType.monthOfYear()).with(DateTimeFieldType.weekOfWeekyear(), 1)); } } } return p1; } public static Partial resolveWeek(Partial p)
public static Partial resolveDowToDay(Partial p1, Partial p2) { // Discard anything that's more specific than dayOfMonth for p2 p2 = JodaTimeUtils.discardMoreSpecificFields(p2, DateTimeFieldType.dayOfMonth()); if (isCompatible(p1,p2)) { if (p1.isSupported(DateTimeFieldType.dayOfWeek())) { if (!p1.isSupported(DateTimeFieldType.dayOfMonth())) { if (p2.isSupported(DateTimeFieldType.dayOfMonth()) && p2.isSupported(DateTimeFieldType.monthOfYear()) && p2.isSupported(DateTimeFieldType.year())) { Instant t2 = getInstant(p2); DateTime t1 = p1.toDateTime(t2); return getPartial(t1.toInstant(), p1.with(DateTimeFieldType.dayOfMonth(), 1)/*.with(DateTimeFieldType.weekOfWeekyear(), 1) */); } } } } return p1; }
public static Instant getInstant(Partial p, ZoneId timezone) { if (p == null) return null; int year = p.isSupported(DateTimeFieldType.year())? p.get(DateTimeFieldType.year()):0; if (!p.isSupported(DateTimeFieldType.year())) { if (p.isSupported(DateTimeFieldType.centuryOfEra())) { year += 100*p.get(DateTimeFieldType.centuryOfEra()); } if (p.isSupported(DateTimeFieldType.yearOfCentury())) { year += p.get(DateTimeFieldType.yearOfCentury()); } else if (p.isSupported(DecadeOfCentury)) { year += 10*p.get(DecadeOfCentury); } } int moy = p.isSupported(DateTimeFieldType.monthOfYear())? p.get(DateTimeFieldType.monthOfYear()):1; if (!p.isSupported(DateTimeFieldType.monthOfYear())) { if (p.isSupported(QuarterOfYear)) { moy += 3*(p.get(QuarterOfYear)-1); } } int dom = p.isSupported(DateTimeFieldType.dayOfMonth())? p.get(DateTimeFieldType.dayOfMonth()):1; int hod = p.isSupported(DateTimeFieldType.hourOfDay())? p.get(DateTimeFieldType.hourOfDay()):0; int moh = p.isSupported(DateTimeFieldType.minuteOfHour())? p.get(DateTimeFieldType.minuteOfHour()):0; int som = p.isSupported(DateTimeFieldType.secondOfMinute())? p.get(DateTimeFieldType.secondOfMinute()):0; int msos = p.isSupported(DateTimeFieldType.millisOfSecond())? p.get(DateTimeFieldType.millisOfSecond()):0; return new DateTime(year, moy, dom, hod, moh, som, msos, fromTimezone(timezone)).toInstant(); }
/** * Parses a {@code Instant} from the specified string using a formatter. * * @param str the string to parse, not null * @param formatter the formatter to use, not null * @since 2.0 */ public static Instant parse(String str, DateTimeFormatter formatter) { return formatter.parseDateTime(str).toInstant(); }
/** * Parses a {@code Instant} from the specified string using a formatter. * * @param str the string to parse, not null * @param formatter the formatter to use, not null * @since 2.0 */ public static Instant parse(String str, DateTimeFormatter formatter) { return formatter.parseDateTime(str).toInstant(); }
DateTime local = DateTime.now() Date localJDK = local.toDate() assert localJDK.getTime() == local.toInstant().getMillis() DateTime differentTimeZone = DateTime.now(DateTimeZone.forID('America/Chicago')) Date localJDK2 = differentTimeZone.toDate() assert differentTimeZone.toInstant().getMillis() == localJDK2.getTime() assert localJDK.getTime() == localJDK2.getTime()
@Override public IntervalWindow assignWindow(Instant timestamp) { DateTime datetime = new DateTime(timestamp, timeZone); int dayOffset = Days.daysBetween(startDate, datetime).getDays() / number * number; DateTime begin = startDate.plusDays(dayOffset); DateTime end = begin.plusDays(number); return new IntervalWindow(begin.toInstant(), end.toInstant()); }
@Override public void populateDisplayData(DisplayData.Builder builder) { super.populateDisplayData(builder); builder .add(DisplayData.item("numYears", number).withLabel("Window Years")) .addIfNotDefault( DisplayData.item("startDate", new DateTime(startDate, timeZone).toInstant()) .withLabel("Window Start Date"), new DateTime(DEFAULT_START_DATE, DateTimeZone.UTC).toInstant()); }
@Override public IntervalWindow assignWindow(Instant timestamp) { DateTime datetime = new DateTime(timestamp, timeZone); DateTime offsetStart = startDate.withMonthOfYear(monthOfYear).withDayOfMonth(dayOfMonth); int yearOffset = Years.yearsBetween(offsetStart, datetime).getYears() / number * number; DateTime begin = offsetStart.plusYears(yearOffset); DateTime end = begin.plusYears(number); return new IntervalWindow(begin.toInstant(), end.toInstant()); }
@Test public void testToDateTime_fromInstantToApiDateTime() { assertEquals(apiDateTime1, dateTimesHelper.toDateTime(jodaDateTime1.toInstant(), TIME_ZONE_ID1)); assertEquals(apiDateTime2, dateTimesHelper.toDateTime(jodaDateTime2.toInstant(), TIME_ZONE_ID2)); assertEquals(apiDateTime3, dateTimesHelper.toDateTime(jodaDateTime3.toInstant(), TIME_ZONE_ID3)); }
@Test public void testTransitive_instantApiJoda() { assertEquals(jodaDateTime1, dateTimesHelper.toDateTime( dateTimesHelper.toDateTime(jodaDateTime1.toInstant(), TIME_ZONE_ID1))); assertEquals(jodaDateTime2, dateTimesHelper.toDateTime( dateTimesHelper.toDateTime(jodaDateTime2.toInstant(), TIME_ZONE_ID2))); assertEquals(jodaDateTime3, dateTimesHelper.toDateTime( dateTimesHelper.toDateTime(jodaDateTime3.toInstant(), TIME_ZONE_ID3))); }