Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2009 Stephen Colebourne
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.joda.time.convert;
 
 
CalendarConverter converts a java util Calendar to an instant or partial. The Calendar is converted to milliseconds and the chronology that best matches the calendar.

Author(s):
Stephen Colebourne
Since:
1.0
 
 final class CalendarConverter extends AbstractConverter
         implements InstantConverterPartialConverter {

    
Singleton instance.
 
     static final CalendarConverter INSTANCE = new CalendarConverter();

    
Restricted constructor.
 
     protected CalendarConverter() {
         super();
     }
 
     //-----------------------------------------------------------------------
     
Gets the chronology.

If a chronology is specified then it is used. Otherwise, it is the GJChronology if a GregorianCalendar is used, BuddhistChronology if a BuddhistCalendar is used or ISOChronology otherwise. The time zone is extracted from the calendar if possible, default used if not.

Parameters:
object the Calendar to convert, must not be null
chrono the chronology to use, null means use Calendar
Returns:
the chronology, never null
Throws:
java.lang.NullPointerException if the object is null
java.lang.ClassCastException if the object is an invalid type
 
     public Chronology getChronology(Object objectChronology chrono) {
         if (chrono != null) {
             return chrono;
         }
         Calendar cal = (Calendarobject;
         DateTimeZone zone = null;
         try {
             zone = DateTimeZone.forTimeZone(cal.getTimeZone());
             
         } catch (IllegalArgumentException ex) {
             zone = DateTimeZone.getDefault();
         }
         return getChronology(calzone);
     }

    
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.

Parameters:
object the Calendar to convert, must not be null
zone the specified zone to use, null means default zone
Returns:
the chronology, never null
Throws:
java.lang.NullPointerException if the object is null
java.lang.ClassCastException if the object is an invalid type
 
     public Chronology getChronology(Object objectDateTimeZone zone) {
         if (object.getClass().getName().endsWith(".BuddhistCalendar")) {
             return BuddhistChronology.getInstance(zone);
         } else if (object instanceof GregorianCalendar) {
             GregorianCalendar gc = (GregorianCalendarobject;
             long cutover = gc.getGregorianChange().getTime();
             if (cutover == .) {
                return GregorianChronology.getInstance(zone);
            } else if (cutover == .) {
                return JulianChronology.getInstance(zone);
            } else {
                return GJChronology.getInstance(zonecutover, 4);
            }
        } else {
            return ISOChronology.getInstance(zone);
        }
    }

    
Gets the millis, which is the Calendar millis value.

Parameters:
object the Calendar to convert, must not be null
chrono the chronology result from getChronology, non-null
Returns:
the millisecond value
Throws:
java.lang.NullPointerException if the object is null
java.lang.ClassCastException if the object is an invalid type
    public long getInstantMillis(Object objectChronology chrono) {
        Calendar calendar = (Calendarobject;
        return calendar.getTime().getTime();
    }
    //-----------------------------------------------------------------------
    
Returns Calendar.class.

Returns:
Calendar.class
    public Class<?> getSupportedType() {
        return Calendar.class;
    }
New to GrepCode? Check out our FAQ X