1 16 package org.joda.time.convert; 17 18 import java.util.Calendar ; 19 import java.util.GregorianCalendar ; 20 21 import org.joda.time.Chronology; 22 import org.joda.time.DateTimeZone; 23 import org.joda.time.chrono.BuddhistChronology; 24 import org.joda.time.chrono.GJChronology; 25 import org.joda.time.chrono.GregorianChronology; 26 import org.joda.time.chrono.ISOChronology; 27 import org.joda.time.chrono.JulianChronology; 28 29 37 final class CalendarConverter extends AbstractConverter 38 implements InstantConverter, PartialConverter { 39 40 43 static final CalendarConverter INSTANCE = new CalendarConverter(); 44 45 48 protected CalendarConverter() { 49 super(); 50 } 51 52 67 public Chronology getChronology(Object object, Chronology chrono) { 68 if (chrono != null) { 69 return chrono; 70 } 71 Calendar cal = (Calendar ) object; 72 DateTimeZone zone = null; 73 try { 74 zone = DateTimeZone.forTimeZone(cal.getTimeZone()); 75 76 } catch (IllegalArgumentException ex) { 77 zone = DateTimeZone.getDefault(); 78 } 79 return getChronology(cal, zone); 80 } 81 82 93 public Chronology getChronology(Object object, DateTimeZone zone) { 94 if (object.getClass().getName().endsWith(".BuddhistCalendar")) { 95 return BuddhistChronology.getInstance(zone); 96 } else if (object instanceof GregorianCalendar ) { 97 GregorianCalendar gc = (GregorianCalendar ) object; 98 long cutover = gc.getGregorianChange().getTime(); 99 if (cutover == Long.MIN_VALUE) { 100 return GregorianChronology.getInstance(zone); 101 } else if (cutover == Long.MAX_VALUE) { 102 return JulianChronology.getInstance(zone); 103 } else { 104 return GJChronology.getInstance(zone, cutover, 4); 105 } 106 } else { 107 return ISOChronology.getInstance(zone); 108 } 109 } 110 111 120 public long getInstantMillis(Object object, Chronology chrono) { 121 Calendar calendar = (Calendar ) object; 122 return calendar.getTime().getTime(); 123 } 124 125 131 public Class getSupportedType() { 132 return Calendar .class; 133 } 134 135 } 136 | Popular Tags |