/** * 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); } }
private void checkJodaSetGetYear() { int COUNT = COUNT_FAST; // Is it fair to use only MutableDateTime here? You decide. // MutableDateTime dt = new MutableDateTime(GJChronology.getInstance()); // for (int i = 0; i < AVERAGE; i++) { // start("Joda", "setGetYear"); // for (int j = 0; j < COUNT; j++) { // dt.setYear(1972); // int val = dt.getYear(); // if (val < 0) {System.out.println("Anti optimise");} // } // end(COUNT); // } DateTime dt = new DateTime(GJChronology.getInstance()); for (int i = 0; i < AVERAGE; i++) { start("Joda", "setGetYear"); for (int j = 0; j < COUNT; j++) { dt = dt.year().setCopy(1972); int val = dt.getYear(); if (val < 0) {System.out.println("Anti optimise");} } end(COUNT); } }
public void testToString() { DateTimeZone paris = DateTimeZone.forID("Europe/Paris"); ISOChronology isoParis = ISOChronology.getInstance(paris); assertEquals("ISOChronology[Europe/Paris]", isoParis.toString()); assertEquals("GJChronology[Europe/Paris]", GJChronology.getInstance(paris).toString()); assertEquals("GregorianChronology[Europe/Paris]", GregorianChronology.getInstance(paris).toString()); assertEquals("JulianChronology[Europe/Paris]", JulianChronology.getInstance(paris).toString()); assertEquals("BuddhistChronology[Europe/Paris]", BuddhistChronology.getInstance(paris).toString()); assertEquals("CopticChronology[Europe/Paris]", CopticChronology.getInstance(paris).toString()); assertEquals("EthiopicChronology[Europe/Paris]", EthiopicChronology.getInstance(paris).toString()); assertEquals("IslamicChronology[Europe/Paris]", IslamicChronology.getInstance(paris).toString()); assertEquals("LenientChronology[ISOChronology[Europe/Paris]]", LenientChronology.getInstance(isoParis).toString()); assertEquals("StrictChronology[ISOChronology[Europe/Paris]]", StrictChronology.getInstance(isoParis).toString()); assertEquals("LimitChronology[ISOChronology[Europe/Paris], NoLimit, NoLimit]", LimitChronology.getInstance(isoParis, null, null).toString()); assertEquals("ZonedChronology[ISOChronology[UTC], Europe/Paris]", ZonedChronology.getInstance(isoParis, paris).toString()); }
public void testFormat_yearOfEraParse() { Chronology chrono = GJChronology.getInstanceUTC(); DateTimeFormatter f = DateTimeFormat .forPattern("YYYY-MM GG") .withChronology(chrono) .withLocale(Locale.UK); DateTime dt = new DateTime(2005, 10, 1, 0, 0, 0, 0, chrono); assertEquals(dt, f.parseDateTime("2005-10 AD")); assertEquals(dt, f.parseDateTime("2005-10 CE")); dt = new DateTime(-2005, 10, 1, 0, 0, 0, 0, chrono); assertEquals(dt, f.parseDateTime("2005-10 BC")); assertEquals(dt, f.parseDateTime("2005-10 BCE")); }
private void checkJodaConstructor1() { int COUNT = COUNT_SLOW; DateTime dt = new DateTime(GJChronology.getInstance()); int count = 0; for (int i = 0; i < AVERAGE; i++) { start("Joda", "new()"); for (int j = 0; j < COUNT; j++) { dt = new DateTime(GJChronology.getInstance()); if (count++ < 0) {System.out.println("Anti optimise");} } end(COUNT); } }
private void checkJodaConstructor2() { int COUNT = COUNT_VERY_FAST; DateTime dt = new DateTime(12345L, GJChronology.getInstance()); for (int i = 0; i < AVERAGE; i++) { start("Joda", "new(millis)"); for (int j = 0; j < COUNT; j++) { dt = new DateTime(12345L, GJChronology.getInstance()); if (dt == null) {System.out.println("Anti optimise");} } end(COUNT); } }
private void checkJodaConstructor3() { int COUNT = COUNT_SLOW; DateTime dt = new DateTime(1972, 10, 1, 0, 0, 0, 0, GJChronology.getInstance()); for (int i = 0; i < AVERAGE; i++) { start("Joda", "new(YMD)"); for (int j = 0; j < COUNT; j++) { dt = new DateTime(1972, 10, 1, 0, 0, 0, 0, GJChronology.getInstance()); if (dt == null) {System.out.println("Anti optimise");} } end(COUNT); } }
private void checkJodaGetYear() { int COUNT = COUNT_VERY_FAST; DateTime dt = new DateTime(GJChronology.getInstance()); for (int i = 0; i < AVERAGE; i++) { start("Joda", "getYear"); for (int j = 0; j < COUNT; j++) { int val = dt.getYear(); if (val == 0) {System.out.println("Anti optimise");} } end(COUNT); } }
private void checkJodaGetMonth() { int COUNT = COUNT_VERY_FAST; DateTime dt = new DateTime(GJChronology.getInstance()); for (int i = 0; i < AVERAGE; i++) { start("Joda", "getMonth"); for (int j = 0; j < COUNT; j++) { int val = dt.getMonthOfYear(); if (val == 0) {System.out.println("Anti optimise");} } end(COUNT); } }
private void checkJodaGetDay() { int COUNT = COUNT_VERY_FAST; DateTime dt = new DateTime(GJChronology.getInstance()); for (int i = 0; i < AVERAGE; i++) { start("Joda", "getDay"); for (int j = 0; j < COUNT; j++) { int val = dt.getDayOfMonth(); if (val == 0) {System.out.println("Anti optimise");} } end(COUNT); } }
private void checkJodaGetHour() { int COUNT = COUNT_VERY_FAST; DateTime dt = new DateTime(GJChronology.getInstance()); for (int i = 0; i < AVERAGE; i++) { start("Joda", "getHour"); for (int j = 0; j < COUNT; j++) { int val = dt.getHourOfDay(); if (val == -1) {System.out.println("Anti optimise");} } end(COUNT); } }
private void checkJodaSetYear() { int COUNT = COUNT_FAST; // Is it fair to use only MutableDateTime here? You decide. MutableDateTime dt = new MutableDateTime(GJChronology.getInstance()); for (int i = 0; i < AVERAGE; i++) { start("Joda", "setYear"); for (int j = 0; j < COUNT; j++) { dt.setYear(1972); if (dt == null) {System.out.println("Anti optimise");} } end(COUNT); } }
private void checkJodaSetHour() { int COUNT = COUNT_VERY_FAST; // Is it fair to use only MutableDateTime here? You decide. MutableDateTime dt = new MutableDateTime(GJChronology.getInstance()); for (int i = 0; i < AVERAGE; i++) { start("Joda", "setHour"); for (int j = 0; j < COUNT; j++) { dt.setHourOfDay(13); if (dt == null) {System.out.println("Anti optimise");} } end(COUNT); } }
private void checkJodaSetGetHour() { int COUNT = COUNT_VERY_FAST; // Is it fair to use only MutableDateTime here? You decide. MutableDateTime dt = new MutableDateTime(GJChronology.getInstance()); for (int i = 0; i < AVERAGE; i++) { start("Joda", "setGetHour"); for (int j = 0; j < COUNT; j++) { dt.setHourOfDay(13); int val = dt.getHourOfDay(); if (dt == null) {System.out.println("Anti optimise");} } end(COUNT); } }
private void checkJodaToString() { int COUNT = COUNT_SLOW; DateTime dt = new DateTime(GJChronology.getInstance()); DateTimeFormatter f = DateTimeFormat.forPattern("dd MMM yyyy"); for (int i = 0; i < AVERAGE; i++) { start("Joda", "toString"); for (int j = 0; j < COUNT; j++) { String str = dt.toString("dd MMM yyyy"); // String str = dt.toString(f); if (str == null) {System.out.println("Anti optimise");} } end(COUNT); } }
public void testConstructor_long_long_Chronology() throws Throwable { DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1); Interval test = new Interval(dt1.getMillis(), dt2.getMillis(), GJChronology.getInstance()); assertEquals(dt1.getMillis(), test.getStartMillis()); assertEquals(dt2.getMillis(), test.getEndMillis()); assertEquals(GJChronology.getInstance(), test.getChronology()); }
public void testConstructor_RI_RI_chronoStart() throws Throwable { DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0, GJChronology.getInstance()); DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1); Interval test = new Interval(dt1, dt2); assertEquals(dt1.getMillis(), test.getStartMillis()); assertEquals(dt2.getMillis(), test.getEndMillis()); assertEquals(GJChronology.getInstance(), test.getChronology()); }
public void testConstructor_RI_RI_chronoEnd() throws Throwable { DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1, GJChronology.getInstance()); Interval test = new Interval(dt1, dt2); assertEquals(dt1.getMillis(), test.getStartMillis()); assertEquals(dt2.getMillis(), test.getEndMillis()); assertEquals(ISOChronology.getInstance(), test.getChronology()); }
public void testGetChronology_Object_Zone() throws Exception { GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Paris")); assertEquals(GJChronology.getInstance(MOSCOW), CalendarConverter.INSTANCE.getChronology(cal, MOSCOW)); cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow")); assertEquals(GJChronology.getInstance(), CalendarConverter.INSTANCE.getChronology(cal, (DateTimeZone) null)); cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow")); cal.setGregorianChange(new Date(0L)); assertEquals(GJChronology.getInstance(MOSCOW, 0L, 4), CalendarConverter.INSTANCE.getChronology(cal, MOSCOW)); cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow")); cal.setGregorianChange(new Date(Long.MAX_VALUE)); assertEquals(JulianChronology.getInstance(PARIS), CalendarConverter.INSTANCE.getChronology(cal, PARIS)); cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow")); cal.setGregorianChange(new Date(Long.MIN_VALUE)); assertEquals(GregorianChronology.getInstance(PARIS), CalendarConverter.INSTANCE.getChronology(cal, PARIS)); Calendar uc = new MockUnknownCalendar(TimeZone.getTimeZone("Europe/Moscow")); assertEquals(ISOChronology.getInstance(PARIS), CalendarConverter.INSTANCE.getChronology(uc, PARIS)); try { Calendar bc = (Calendar) Class.forName("sun.util.BuddhistCalendar").newInstance(); bc.setTimeZone(TimeZone.getTimeZone("Europe/Moscow")); assertEquals(BuddhistChronology.getInstance(PARIS), CalendarConverter.INSTANCE.getChronology(bc, PARIS)); } catch (ClassNotFoundException ex) { // ignore } }
public void testGetChronology_Object_nullChronology() throws Exception { GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Paris")); assertEquals(GJChronology.getInstance(PARIS), CalendarConverter.INSTANCE.getChronology(cal, (Chronology) null)); cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow")); cal.setGregorianChange(new Date(0L)); assertEquals(GJChronology.getInstance(MOSCOW, 0L, 4), CalendarConverter.INSTANCE.getChronology(cal, (Chronology) null)); cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow")); cal.setGregorianChange(new Date(Long.MAX_VALUE)); assertEquals(JulianChronology.getInstance(MOSCOW), CalendarConverter.INSTANCE.getChronology(cal, (Chronology) null)); cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow")); cal.setGregorianChange(new Date(Long.MIN_VALUE)); assertEquals(GregorianChronology.getInstance(MOSCOW), CalendarConverter.INSTANCE.getChronology(cal, (Chronology) null)); cal = new GregorianCalendar(new MockUnknownTimeZone()); assertEquals(GJChronology.getInstance(), CalendarConverter.INSTANCE.getChronology(cal, (Chronology) null)); Calendar uc = new MockUnknownCalendar(TimeZone.getTimeZone("Europe/Moscow")); assertEquals(ISOChronology.getInstance(MOSCOW), CalendarConverter.INSTANCE.getChronology(uc, (Chronology) null)); try { Calendar bc = (Calendar) Class.forName("sun.util.BuddhistCalendar").newInstance(); bc.setTimeZone(TimeZone.getTimeZone("Europe/Moscow")); assertEquals(BuddhistChronology.getInstance(MOSCOW), CalendarConverter.INSTANCE.getChronology(bc, (Chronology) null)); } catch (ClassNotFoundException ex) { // ignore } }
public void testSetInto_Object_Chronology1() throws Exception { MutableInterval m = new MutableInterval(1000L, 2000L, GJChronology.getInstance()); NullConverter.INSTANCE.setInto(m, null, null); assertEquals(TEST_TIME_NOW, m.getStartMillis()); assertEquals(TEST_TIME_NOW, m.getEndMillis()); assertEquals(ISOChronology.getInstance(), m.getChronology()); }
public void testSetInto_Object_Chronology2() throws Exception { MutableInterval m = new MutableInterval(1000L, 2000L, GJChronology.getInstance()); NullConverter.INSTANCE.setInto(m, null, CopticChronology.getInstance()); assertEquals(TEST_TIME_NOW, m.getStartMillis()); assertEquals(TEST_TIME_NOW, m.getEndMillis()); assertEquals(CopticChronology.getInstance(), m.getChronology()); }
public void testSetIntoInterval_Object2() throws Exception { Interval i = new Interval(0L, 123L, CopticChronology.getInstance()); MutableInterval m = new MutableInterval(-1000L, 1000L, BuddhistChronology.getInstance()); ReadableIntervalConverter.INSTANCE.setInto(m, i, GJChronology.getInstance()); assertEquals(0L, m.getStartMillis()); assertEquals(123L, m.getEndMillis()); assertEquals(GJChronology.getInstance(), m.getChronology()); }
public void testSetIntoInterval_Object3() throws Exception { MutableInterval i = new MutableInterval(0L, 123L) { public Chronology getChronology() { return null; // bad } }; MutableInterval m = new MutableInterval(-1000L, 1000L, BuddhistChronology.getInstance()); ReadableIntervalConverter.INSTANCE.setInto(m, i, GJChronology.getInstance()); assertEquals(0L, m.getStartMillis()); assertEquals(123L, m.getEndMillis()); assertEquals(GJChronology.getInstance(), m.getChronology()); }
public void testConstructor_Object2() throws Throwable { Calendar cal = new GregorianCalendar(); cal.setTime(new Date(TEST_TIME1)); LocalTime test = new LocalTime(cal); assertEquals(GJChronology.getInstanceUTC(), test.getChronology()); assertEquals(1 + OFFSET_LONDON, test.getHourOfDay()); assertEquals(2, test.getMinuteOfHour()); assertEquals(3, test.getSecondOfMinute()); assertEquals(4, test.getMillisOfSecond()); }
public void testParseLocalDate_yearOfEra() { Chronology chrono = GJChronology.getInstanceUTC(); DateTimeFormatter f = DateTimeFormat .forPattern("YYYY-MM GG") .withChronology(chrono) .withLocale(Locale.UK); LocalDate date = new LocalDate(2005, 10, 1, chrono); assertEquals(date, f.parseLocalDate("2005-10 AD")); assertEquals(date, f.parseLocalDate("2005-10 CE")); date = new LocalDate(-2005, 10, 1, chrono); assertEquals(date, f.parseLocalDate("2005-10 BC")); assertEquals(date, f.parseLocalDate("2005-10 BCE")); }
public void testParseLocalDate_yearOfCentury() { Chronology chrono = GJChronology.getInstanceUTC(); DateTimeFormatter f = DateTimeFormat .forPattern("yy M d") .withChronology(chrono) .withLocale(Locale.UK) .withPivotYear(2050); LocalDate date = new LocalDate(2050, 8, 4, chrono); assertEquals(date, f.parseLocalDate("50 8 4")); }
public void testParseLocalDate_monthDay_feb29() { Chronology chrono = GJChronology.getInstanceUTC(); DateTimeFormatter f = DateTimeFormat .forPattern("M d") .withChronology(chrono) .withLocale(Locale.UK); assertEquals(new LocalDate(2000, 2, 29, chrono), f.parseLocalDate("2 29")); }
public void testParseLocalDate_monthDay_withDefaultYear_feb29() { Chronology chrono = GJChronology.getInstanceUTC(); DateTimeFormatter f = DateTimeFormat .forPattern("M d") .withChronology(chrono) .withLocale(Locale.UK) .withDefaultYear(2012); assertEquals(new LocalDate(2012, 2, 29, chrono), f.parseLocalDate("2 29")); }
public void testParseLocalDateTime_monthDay_feb29() { Chronology chrono = GJChronology.getInstanceUTC(); DateTimeFormatter f = DateTimeFormat .forPattern("M d H m") .withChronology(chrono) .withLocale(Locale.UK); assertEquals(new LocalDateTime(2000, 2, 29, 13, 40, 0, 0, chrono), f.parseLocalDateTime("2 29 13 40")); }