public static int getYearsBetween(final String date1, final String date2, String format){ try { final DateTimeFormatter fmt = DateTimeFormat .forPattern(format) .withChronology( LenientChronology.getInstance( GregorianChronology.getInstance())); return Years.yearsBetween( fmt.parseDateTime(date1), fmt.parseDateTime(date2) ).getYears(); } catch (Exception ex) { ex.printStackTrace(); return 0; } }
public static int getDaysBetween(final String date1, final String date2, String format){ try { final DateTimeFormatter fmt = DateTimeFormat .forPattern(format) .withChronology( LenientChronology.getInstance( GregorianChronology.getInstance())); return Days.daysBetween( fmt.parseDateTime(date1), fmt.parseDateTime(date2) ).getDays(); } catch (Exception ex) { ex.printStackTrace(); return 0; } }
public static int getHoursBetween(final String date1, final String date2, String format){ try { final DateTimeFormatter fmt = DateTimeFormat .forPattern(format) .withChronology( LenientChronology.getInstance( GregorianChronology.getInstance())); return Hours.hoursBetween( fmt.parseDateTime(date1), fmt.parseDateTime(date2) ).getHours(); } catch (Exception ex) { ex.printStackTrace(); return 0; } }
public static int getMonthsBetween(final String date1, final String date2, String format){ try { final DateTimeFormatter fmt = DateTimeFormat .forPattern(format) .withChronology( LenientChronology.getInstance( GregorianChronology.getInstance())); return Months.monthsBetween( fmt.parseDateTime(date1), fmt.parseDateTime(date2) ).getMonths(); } catch (Exception ex) { ex.printStackTrace(); return 0; } }
private String reformatFerretToISO(String in) { Chronology chrono = GregorianChronology.getInstance(DateTimeZone.UTC); DateTimeFormatter iso = ISODateTimeFormat.dateTime().withChronology(chrono).withZone(DateTimeZone.UTC);; DateTimeFormatter sFerretForm = DateTimeFormat.forPattern("dd-MMM-yyyy").withChronology(chrono).withZone(DateTimeZone.UTC); DateTimeFormatter lFerretForm = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm").withChronology(chrono).withZone(DateTimeZone.UTC); DateTime td; try { td = lFerretForm.parseDateTime(in).withZone(DateTimeZone.UTC).withChronology(chrono); } catch (Exception e) { try { td = sFerretForm.parseDateTime(in).withZone(DateTimeZone.UTC).withChronology(chrono); } catch (Exception e2) { return null; } } if ( td != null ) { return iso.print(td.getMillis()); } else { return null; } }
/** * Gets the chronology, which is the GJChronology if a GregorianCalendar is used, * BuddhistChronology if a BuddhistCalendar is used or ISOChronology otherwise. * The time zone specified is used in preference to that on the calendar. * * @param object the Calendar to convert, must not be null * @param zone the specified zone to use, null means default zone * @return the chronology, never null * @throws NullPointerException if the object is null * @throws ClassCastException if the object is an invalid type */ public Chronology getChronology(Object object, DateTimeZone zone) { if (object.getClass().getName().endsWith(".BuddhistCalendar")) { return BuddhistChronology.getInstance(zone); } else if (object instanceof GregorianCalendar) { GregorianCalendar gc = (GregorianCalendar) object; long cutover = gc.getGregorianChange().getTime(); if (cutover == Long.MIN_VALUE) { return GregorianChronology.getInstance(zone); } else if (cutover == Long.MAX_VALUE) { return JulianChronology.getInstance(zone); } else { return GJChronology.getInstance(zone, cutover, 4); } } else { return ISOChronology.getInstance(zone); } }
public void testBug2182444_ausNSW() { Chronology chronAusNSW = GregorianChronology.getInstance(DateTimeZone.forID("Australia/NSW")); Chronology chronUTC = GregorianChronology.getInstance(DateTimeZone.UTC); DateTime australiaNSWStandardInUTC = new DateTime(2008, 4, 5, 16, 0, 0, 0, chronUTC); DateTime australiaNSWDaylightInUTC = new DateTime(2008, 4, 5, 15, 0, 0, 0, chronUTC); assertTrue("Should be standard time", chronAusNSW.getZone().isStandardOffset(australiaNSWStandardInUTC.getMillis())); assertFalse("Should be daylight time", chronAusNSW.getZone().isStandardOffset(australiaNSWDaylightInUTC.getMillis())); DateTime australiaNSWStandardInAustraliaNSW = australiaNSWStandardInUTC.toDateTime(chronAusNSW); DateTime australiaNSWDaylightInAusraliaNSW = australiaNSWDaylightInUTC.toDateTime(chronAusNSW); assertEquals(2, australiaNSWStandardInAustraliaNSW.getHourOfDay()); assertEquals(australiaNSWStandardInAustraliaNSW.getHourOfDay(), australiaNSWDaylightInAusraliaNSW.getHourOfDay()); assertTrue(australiaNSWStandardInAustraliaNSW.getMillis() != australiaNSWDaylightInAusraliaNSW.getMillis()); assertEquals(australiaNSWStandardInAustraliaNSW, australiaNSWStandardInAustraliaNSW.withHourOfDay(2)); assertEquals(australiaNSWStandardInAustraliaNSW.getMillis() + 3, australiaNSWStandardInAustraliaNSW.withMillisOfSecond(3).getMillis()); assertEquals(australiaNSWDaylightInAusraliaNSW, australiaNSWDaylightInAusraliaNSW.withHourOfDay(2)); assertEquals(australiaNSWDaylightInAusraliaNSW.getMillis() + 3, australiaNSWDaylightInAusraliaNSW.withMillisOfSecond(3).getMillis()); }
public void testIsEqual_MD() { MonthDay test1 = new MonthDay(6, 6); MonthDay test1a = new MonthDay(6, 6); assertEquals(true, test1.isEqual(test1a)); assertEquals(true, test1a.isEqual(test1)); assertEquals(true, test1.isEqual(test1)); assertEquals(true, test1a.isEqual(test1a)); MonthDay test2 = new MonthDay(6, 7); assertEquals(false, test1.isEqual(test2)); assertEquals(false, test2.isEqual(test1)); MonthDay test3 = new MonthDay(6, 7, GregorianChronology.getInstanceUTC()); assertEquals(false, test1.isEqual(test3)); assertEquals(false, test3.isEqual(test1)); assertEquals(true, test3.isEqual(test2)); try { new MonthDay(6, 7).isEqual(null); fail(); } catch (IllegalArgumentException ex) {} }
public void testIsBefore_MD() { MonthDay test1 = new MonthDay(6, 6); MonthDay test1a = new MonthDay(6, 6); assertEquals(false, test1.isBefore(test1a)); assertEquals(false, test1a.isBefore(test1)); assertEquals(false, test1.isBefore(test1)); assertEquals(false, test1a.isBefore(test1a)); MonthDay test2 = new MonthDay(6, 7); assertEquals(true, test1.isBefore(test2)); assertEquals(false, test2.isBefore(test1)); MonthDay test3 = new MonthDay(6, 7, GregorianChronology.getInstanceUTC()); assertEquals(true, test1.isBefore(test3)); assertEquals(false, test3.isBefore(test1)); assertEquals(false, test3.isBefore(test2)); try { new MonthDay(6, 7).isBefore(null); fail(); } catch (IllegalArgumentException ex) {} }
public void testIsEqual_YM() { YearMonth test1 = new YearMonth(2005, 6); YearMonth test1a = new YearMonth(2005, 6); assertEquals(true, test1.isEqual(test1a)); assertEquals(true, test1a.isEqual(test1)); assertEquals(true, test1.isEqual(test1)); assertEquals(true, test1a.isEqual(test1a)); YearMonth test2 = new YearMonth(2005, 7); assertEquals(false, test1.isEqual(test2)); assertEquals(false, test2.isEqual(test1)); YearMonth test3 = new YearMonth(2005, 7, GregorianChronology.getInstanceUTC()); assertEquals(false, test1.isEqual(test3)); assertEquals(false, test3.isEqual(test1)); assertEquals(true, test3.isEqual(test2)); try { new YearMonth(2005, 7).isEqual(null); fail(); } catch (IllegalArgumentException ex) {} }
public void testIsEqual_RI() { Instant test1 = new Instant(TEST_TIME1); Instant test1a = new Instant(TEST_TIME1); assertEquals(true, test1.isEqual(test1a)); assertEquals(true, test1a.isEqual(test1)); assertEquals(true, test1.isEqual(test1)); assertEquals(true, test1a.isEqual(test1a)); Instant test2 = new Instant(TEST_TIME2); assertEquals(false, test1.isEqual(test2)); assertEquals(false, test2.isEqual(test1)); DateTime test3 = new DateTime(TEST_TIME2, GregorianChronology.getInstance(PARIS)); assertEquals(false, test1.isEqual(test3)); assertEquals(false, test3.isEqual(test1)); assertEquals(true, test3.isEqual(test2)); assertEquals(false, test2.isEqual(new MockInstant())); assertEquals(true, test1.isEqual(new MockInstant())); assertEquals(false, new Instant(TEST_TIME_NOW + 1).isEqual(null)); assertEquals(true, new Instant(TEST_TIME_NOW).isEqual(null)); assertEquals(false, new Instant(TEST_TIME_NOW - 1).isEqual(null)); }
public void testIsBefore_RI() { Instant test1 = new Instant(TEST_TIME1); Instant test1a = new Instant(TEST_TIME1); assertEquals(false, test1.isBefore(test1a)); assertEquals(false, test1a.isBefore(test1)); assertEquals(false, test1.isBefore(test1)); assertEquals(false, test1a.isBefore(test1a)); Instant test2 = new Instant(TEST_TIME2); assertEquals(true, test1.isBefore(test2)); assertEquals(false, test2.isBefore(test1)); DateTime test3 = new DateTime(TEST_TIME2, GregorianChronology.getInstance(PARIS)); assertEquals(true, test1.isBefore(test3)); assertEquals(false, test3.isBefore(test1)); assertEquals(false, test3.isBefore(test2)); assertEquals(false, test2.isBefore(new MockInstant())); assertEquals(false, test1.isBefore(new MockInstant())); assertEquals(false, new Instant(TEST_TIME_NOW + 1).isBefore(null)); assertEquals(false, new Instant(TEST_TIME_NOW).isBefore(null)); assertEquals(true, new Instant(TEST_TIME_NOW - 1).isBefore(null)); }
public void testIsAfter_TOD() { TimeOfDay test1 = new TimeOfDay(10, 20, 30, 40); TimeOfDay test1a = new TimeOfDay(10, 20, 30, 40); assertEquals(false, test1.isAfter(test1a)); assertEquals(false, test1a.isAfter(test1)); assertEquals(false, test1.isAfter(test1)); assertEquals(false, test1a.isAfter(test1a)); TimeOfDay test2 = new TimeOfDay(10, 20, 35, 40); assertEquals(false, test1.isAfter(test2)); assertEquals(true, test2.isAfter(test1)); TimeOfDay test3 = new TimeOfDay(10, 20, 35, 40, GregorianChronology.getInstanceUTC()); assertEquals(false, test1.isAfter(test3)); assertEquals(true, test3.isAfter(test1)); assertEquals(false, test3.isAfter(test2)); try { new TimeOfDay(10, 20, 35, 40).isAfter(null); fail(); } catch (IllegalArgumentException ex) {} }
@Test public void difference_between_two_dates_joda_duration () { DateTime sinceGraduation = new DateTime(1984, 6, 4, 0, 0, GregorianChronology.getInstance()); DateTime currentDate = new DateTime(); //current date Duration duration = new Duration(sinceGraduation, currentDate); logger.info(duration.getStandardDays()); logger.info(duration.getStandardHours()); logger.info(duration.getStandardMinutes()); logger.info(duration.getStandardSeconds()); assertTrue(duration.getStandardDays() >= 10697); assertTrue(duration.getStandardHours() >= 256747); assertTrue(duration.getStandardMinutes() >= 15404876); assertTrue(duration.getStandardSeconds() >= 924292577); }
@Test public void difference_between_two_dates_joda () { DateTime sinceGraduation = new DateTime(1984, 6, 4, 0, 0, GregorianChronology.getInstance()); DateTime currentDate = new DateTime(); //current date Days diffInDays = Days.daysBetween(sinceGraduation, currentDate); Hours diffInHours = Hours.hoursBetween(sinceGraduation, currentDate); Minutes diffInMinutes = Minutes.minutesBetween(sinceGraduation, currentDate); Seconds seconds = Seconds.secondsBetween(sinceGraduation, currentDate); logger.info(diffInDays.getDays()); logger.info(diffInHours.getHours()); logger.info(diffInMinutes.getMinutes()); logger.info(seconds.getSeconds()); assertTrue(diffInDays.getDays() >= 10697); assertTrue(diffInHours.getHours() >= 256747); assertTrue(diffInMinutes.getMinutes() >= 15404876); assertTrue(seconds.getSeconds() >= 924292577); }
public void testIsAfter_YMD() { YearMonthDay test1 = new YearMonthDay(2005, 6, 2); YearMonthDay test1a = new YearMonthDay(2005, 6, 2); assertEquals(false, test1.isAfter(test1a)); assertEquals(false, test1a.isAfter(test1)); assertEquals(false, test1.isAfter(test1)); assertEquals(false, test1a.isAfter(test1a)); YearMonthDay test2 = new YearMonthDay(2005, 7, 2); assertEquals(false, test1.isAfter(test2)); assertEquals(true, test2.isAfter(test1)); YearMonthDay test3 = new YearMonthDay(2005, 7, 2, GregorianChronology.getInstanceUTC()); assertEquals(false, test1.isAfter(test3)); assertEquals(true, test3.isAfter(test1)); assertEquals(false, test3.isAfter(test2)); try { new YearMonthDay(2005, 7, 2).isAfter(null); fail(); } catch (IllegalArgumentException ex) {} }
public void testIsBefore_LocalDate() { LocalDate test1 = new LocalDate(2005, 6, 2); LocalDate test1a = new LocalDate(2005, 6, 2); assertEquals(false, test1.isBefore(test1a)); assertEquals(false, test1a.isBefore(test1)); assertEquals(false, test1.isBefore(test1)); assertEquals(false, test1a.isBefore(test1a)); LocalDate test2 = new LocalDate(2005, 7, 2); assertEquals(true, test1.isBefore(test2)); assertEquals(false, test2.isBefore(test1)); LocalDate test3 = new LocalDate(2005, 7, 2, GregorianChronology.getInstanceUTC()); assertEquals(true, test1.isBefore(test3)); assertEquals(false, test3.isBefore(test1)); assertEquals(false, test3.isBefore(test2)); try { new LocalDate(2005, 7, 2).isBefore(null); fail(); } catch (IllegalArgumentException ex) {} }
public void testIsBefore_LocalTime() { LocalTime test1 = new LocalTime(10, 20, 30, 40); LocalTime test1a = new LocalTime(10, 20, 30, 40); assertEquals(false, test1.isBefore(test1a)); assertEquals(false, test1a.isBefore(test1)); assertEquals(false, test1.isBefore(test1)); assertEquals(false, test1a.isBefore(test1a)); LocalTime test2 = new LocalTime(10, 20, 35, 40); assertEquals(true, test1.isBefore(test2)); assertEquals(false, test2.isBefore(test1)); LocalTime test3 = new LocalTime(10, 20, 35, 40, GregorianChronology.getInstanceUTC()); assertEquals(true, test1.isBefore(test3)); assertEquals(false, test3.isBefore(test1)); assertEquals(false, test3.isBefore(test2)); try { new LocalTime(10, 20, 35, 40).isBefore(null); fail(); } catch (IllegalArgumentException ex) {} }
public void testIsBefore_YMD() { YearMonthDay test1 = new YearMonthDay(2005, 6, 2); YearMonthDay test1a = new YearMonthDay(2005, 6, 2); assertEquals(false, test1.isBefore(test1a)); assertEquals(false, test1a.isBefore(test1)); assertEquals(false, test1.isBefore(test1)); assertEquals(false, test1a.isBefore(test1a)); YearMonthDay test2 = new YearMonthDay(2005, 7, 2); assertEquals(true, test1.isBefore(test2)); assertEquals(false, test2.isBefore(test1)); YearMonthDay test3 = new YearMonthDay(2005, 7, 2, GregorianChronology.getInstanceUTC()); assertEquals(true, test1.isBefore(test3)); assertEquals(false, test3.isBefore(test1)); assertEquals(false, test3.isBefore(test2)); try { new YearMonthDay(2005, 7, 2).isBefore(null); fail(); } catch (IllegalArgumentException ex) {} }
public void testIsEqual_TOD() { TimeOfDay test1 = new TimeOfDay(10, 20, 30, 40); TimeOfDay test1a = new TimeOfDay(10, 20, 30, 40); assertEquals(true, test1.isEqual(test1a)); assertEquals(true, test1a.isEqual(test1)); assertEquals(true, test1.isEqual(test1)); assertEquals(true, test1a.isEqual(test1a)); TimeOfDay test2 = new TimeOfDay(10, 20, 35, 40); assertEquals(false, test1.isEqual(test2)); assertEquals(false, test2.isEqual(test1)); TimeOfDay test3 = new TimeOfDay(10, 20, 35, 40, GregorianChronology.getInstanceUTC()); assertEquals(false, test1.isEqual(test3)); assertEquals(false, test3.isEqual(test1)); assertEquals(true, test3.isEqual(test2)); try { new TimeOfDay(10, 20, 35, 40).isEqual(null); fail(); } catch (IllegalArgumentException ex) {} }
public void testIsBefore_TOD() { TimeOfDay test1 = new TimeOfDay(10, 20, 30, 40); TimeOfDay test1a = new TimeOfDay(10, 20, 30, 40); assertEquals(false, test1.isBefore(test1a)); assertEquals(false, test1a.isBefore(test1)); assertEquals(false, test1.isBefore(test1)); assertEquals(false, test1a.isBefore(test1a)); TimeOfDay test2 = new TimeOfDay(10, 20, 35, 40); assertEquals(true, test1.isBefore(test2)); assertEquals(false, test2.isBefore(test1)); TimeOfDay test3 = new TimeOfDay(10, 20, 35, 40, GregorianChronology.getInstanceUTC()); assertEquals(true, test1.isBefore(test3)); assertEquals(false, test3.isBefore(test1)); assertEquals(false, test3.isBefore(test2)); try { new TimeOfDay(10, 20, 35, 40).isBefore(null); fail(); } catch (IllegalArgumentException ex) {} }
public void testIsEqual_LocalTime() { LocalTime test1 = new LocalTime(10, 20, 30, 40); LocalTime test1a = new LocalTime(10, 20, 30, 40); assertEquals(true, test1.isEqual(test1a)); assertEquals(true, test1a.isEqual(test1)); assertEquals(true, test1.isEqual(test1)); assertEquals(true, test1a.isEqual(test1a)); LocalTime test2 = new LocalTime(10, 20, 35, 40); assertEquals(false, test1.isEqual(test2)); assertEquals(false, test2.isEqual(test1)); LocalTime test3 = new LocalTime(10, 20, 35, 40, GregorianChronology.getInstanceUTC()); assertEquals(false, test1.isEqual(test3)); assertEquals(false, test3.isEqual(test1)); assertEquals(true, test3.isEqual(test2)); try { new LocalTime(10, 20, 35, 40).isEqual(null); fail(); } catch (IllegalArgumentException ex) {} }
public void testIsEqual_LocalDate() { LocalDate test1 = new LocalDate(2005, 6, 2); LocalDate test1a = new LocalDate(2005, 6, 2); assertEquals(true, test1.isEqual(test1a)); assertEquals(true, test1a.isEqual(test1)); assertEquals(true, test1.isEqual(test1)); assertEquals(true, test1a.isEqual(test1a)); LocalDate test2 = new LocalDate(2005, 7, 2); assertEquals(false, test1.isEqual(test2)); assertEquals(false, test2.isEqual(test1)); LocalDate test3 = new LocalDate(2005, 7, 2, GregorianChronology.getInstanceUTC()); assertEquals(false, test1.isEqual(test3)); assertEquals(false, test3.isEqual(test1)); assertEquals(true, test3.isEqual(test2)); try { new LocalDate(2005, 7, 2).isEqual(null); fail(); } catch (IllegalArgumentException ex) {} }
public void testIsAfter_LocalTime() { LocalTime test1 = new LocalTime(10, 20, 30, 40); LocalTime test1a = new LocalTime(10, 20, 30, 40); assertEquals(false, test1.isAfter(test1a)); assertEquals(false, test1a.isAfter(test1)); assertEquals(false, test1.isAfter(test1)); assertEquals(false, test1a.isAfter(test1a)); LocalTime test2 = new LocalTime(10, 20, 35, 40); assertEquals(false, test1.isAfter(test2)); assertEquals(true, test2.isAfter(test1)); LocalTime test3 = new LocalTime(10, 20, 35, 40, GregorianChronology.getInstanceUTC()); assertEquals(false, test1.isAfter(test3)); assertEquals(true, test3.isAfter(test1)); assertEquals(false, test3.isAfter(test2)); try { new LocalTime(10, 20, 35, 40).isAfter(null); fail(); } catch (IllegalArgumentException ex) {} }
public void testToDateTime_Chronology() { MutableDateTime test = new MutableDateTime(TEST_TIME1); MutableDateTime result = test.toMutableDateTime(ISOChronology.getInstance()); assertEquals(test.getMillis(), result.getMillis()); assertEquals(ISOChronology.getInstance(), result.getChronology()); test = new MutableDateTime(TEST_TIME1); result = test.toMutableDateTime(GregorianChronology.getInstance(PARIS)); assertEquals(test.getMillis(), result.getMillis()); assertEquals(GregorianChronology.getInstance(PARIS), result.getChronology()); test = new MutableDateTime(TEST_TIME1, GregorianChronology.getInstance(PARIS)); result = test.toMutableDateTime((Chronology) null); assertEquals(test.getMillis(), result.getMillis()); assertEquals(ISOChronology.getInstance(), result.getChronology()); test = new MutableDateTime(TEST_TIME1); result = test.toMutableDateTime((Chronology) null); assertEquals(test.getMillis(), result.getMillis()); assertEquals(ISOChronology.getInstance(), result.getChronology()); }