com.extentech.ExtenXLS
Class DateConverter

java.lang.Object
  extended by com.extentech.ExtenXLS.DateConverter

public class DateConverter
extends Object

Provides methods for conversion to and from Excel serial date values.

Excel stores dates as the number of days since midnight on January 1, 1900. Times are represented as fractional days. For example, 6:00 AM on February 2, 1900 is represented as 33.25. Excel incorrectly treats 1900 as a leap year, so serial dates after February 28, 1900 are one higher than they otherwise should be and the value 60 is unmapped. It also interprets the value 0 as January 0, 1900.

Excel does not support negative serial date values, so it cannot handle dates prior to 1900. It also does not currently accept date values with a year of 10000 or greater. ExtenXLS does not currently support negative date values, but this feature is planned. If you wish to restrict the output to the subset of values supported by Excel, you may enable input validation by calling setValidate(boolean).

Due to the inherent inaccuracy of floating-point types, values from this class can only be guaranteed to equal values generated by Excel to eight decimal places. This provides accuracy to the unit milliseconds, the maximum precision of Java's date classes. Accuracy outside the range supported by Excel is not guaranteed and will degrade as the values get farther from zero.

Author:
John McMahon, Sam Hanes

Field Summary
 int DEBUGLEVEL
          Deprecated. No longer used internally. Should not have been public.
static int millsin8hours
          Deprecated. No longer used internally. Should not have been public.
static int millsinday
          Deprecated. No longer used internally. Should not have been public.
static int millsinhour
          Deprecated. No longer used internally. Should not have been public.
static int numdaysofEPOCH
          Deprecated. No longer used internally. Should not have been public.
static double xlsmillisec
          Deprecated. No longer used internally. Should not have been public.
static int xlsmultiplier
          Deprecated. No longer used internally. Should not have been public.
 
Constructor Summary
DateConverter()
           
 
Method Summary
static Calendar convertStringToCalendar(String dateStr)
          Converts a string representation of a date into a valid calendar object date must be in format mm/dd/yy (or yyyy)
static Calendar getCalendar()
          Gets a clone of the calendar used for date calculation.
static Calendar getCalendarFromCell(Cell cell)
          Gets the Calendar for the given cell.
static Calendar getCalendarFromCellWithPrecision(Cell cell, int roundingCalendarField)
          returns a Java Calendar from a CellHandle containing an Excel-formatted Date The Excel date format does not map 100% accurately to Java dates, due to the limitation of the precision of the Excel floating-point value record.
static Calendar getCalendarFromNumber(double date)
          Gets the Calendar for the given Excel serial date.
static Calendar getCalendarFromNumber(Object number)
          Deprecated. Use getCalendarFromNumber(double) instead.
static Date getDateFromCell(CellHandle cell)
          Gets the Date for the given cell.
static Date getDateFromNumber(double date)
          Gets the Date for the given Excel serial date.
static Date getDateFromNumber(Object number)
          Deprecated. Use getDateFromNumber(double) instead.
static String getFormattedDateVal(CellHandle cell)
          Deprecated. The date format handling in this method is wildly incorrect. It is retained only to provide compatibility with legacy ExtenXLS XML files.
static Calendar getNonLocalizedCalendarFromNumber(Object number)
          Deprecated. Identical to getDateFromNumber(Object).
static Date getNonLocalizedDateFromNumber(Object number)
          Deprecated. Identical to getDateFromNumber(Object).
static long getNumDaysFrom00(String s)
          gives the numeric number of days since 1900 for the given string date.
static boolean getValidate()
          Returns whether input validation is on.
static double getXLSDateVal(Calendar cal)
          Gets the Excel serial date value for the given Calendar object.
static double getXLSDateVal(Date date)
          Gets the Excel serial date for the given date in the default time zone.
static boolean isDatePattern(String possibleDate)
          Inspects a string to determine if it is a date.
static boolean isParseableDateString(String str)
          returns whether this method will work with your input string
static Date parseDate(String s, int pat)
          Deprecated. The date format handling in this method is wildly incorrect. It is retained only to provide compatibility with legacy ExtenXLS XML files.
static void setCalendar(Calendar cal)
          Sets the calendar used to perform date calculation.
static void setDaylightSavingsOffset(Date date)
          Deprecated. Use Calendar instead.
static void setValidate(boolean validate)
          Sets whether to perform input validation.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

numdaysofEPOCH

public static int numdaysofEPOCH
Deprecated. No longer used internally. Should not have been public.

millsinday

public static int millsinday
Deprecated. No longer used internally. Should not have been public.

millsin8hours

public static int millsin8hours
Deprecated. No longer used internally. Should not have been public.

millsinhour

public static int millsinhour
Deprecated. No longer used internally. Should not have been public.

xlsmillisec

public static double xlsmillisec
Deprecated. No longer used internally. Should not have been public.

xlsmultiplier

public static int xlsmultiplier
Deprecated. No longer used internally. Should not have been public.

DEBUGLEVEL

public int DEBUGLEVEL
Deprecated. No longer used internally. Should not have been public.
Constructor Detail

DateConverter

public DateConverter()
Method Detail

getCalendar

public static Calendar getCalendar()
Gets a clone of the calendar used for date calculation.


setCalendar

public static void setCalendar(Calendar cal)
Sets the calendar used to perform date calculation. This allows you to set the locale and time zone to something other than the system default.

Parameters:
cal - the calendar that should be used for date calculations

getValidate

public static boolean getValidate()
Returns whether input validation is on.


setValidate

public static void setValidate(boolean validate)
Sets whether to perform input validation.


isParseableDateString

public static boolean isParseableDateString(String str)
returns whether this method will work with your input string


getNumDaysFrom00

public static long getNumDaysFrom00(String s)
gives the numeric number of days since 1900 for the given string date.

Parameters:
s - - the date to retrieve the numeric number of days.

setDaylightSavingsOffset

public static void setDaylightSavingsOffset(Date date)
Deprecated. Use Calendar instead.

Adds the daylight saving offset for the default zone to the given date object. WARNING: This violates the contract of Date, whose value must be in UTC milliseconds since the epoch and thus cannot be adjusted for daylight saving time.

Parameters:
date - the Date whose value should be changed

getXLSDateVal

public static double getXLSDateVal(Calendar cal)
Gets the Excel serial date value for the given Calendar object.

Parameters:
cal - the Calendar to convert
Returns:
the Excel serial date representing the given calendar's value in the calendar's time zone

getXLSDateVal

public static double getXLSDateVal(Date date)
Gets the Excel serial date for the given date in the default time zone.

Parameters:
date - the date to be converted
Returns:
the Excel serial date value corresponding to the given date

getCalendarFromNumber

public static Calendar getCalendarFromNumber(double date)
Gets the Calendar for the given Excel serial date.

Parameters:
date - the Excel serial date to be interpreted
Returns:
a Calendar representing the given Excel date interpreted in the default time zone

getDateFromNumber

public static Date getDateFromNumber(double date)
Gets the Date for the given Excel serial date.

Parameters:
number - a Number representing the serial date to be interpreted
Returns:
a Date representing the given Excel date interpreted in the default time zone

getDateFromNumber

public static Date getDateFromNumber(Object number)
Deprecated. Use getDateFromNumber(double) instead.

Gets the Date for the given Excel serial date.

Parameters:
number - a Number representing the serial date to be interpreted
Returns:
a Date representing the given Excel date interpreted in the default time zone
Throws:
ClassCastException - if the passed object is not a Number

getNonLocalizedDateFromNumber

public static Date getNonLocalizedDateFromNumber(Object number)
Deprecated. Identical to getDateFromNumber(Object).

Gets the Date for the given Excel serial date.

Parameters:
number - a Number representing the serial date to be interpreted
Returns:
a Date representing the given Excel date interpreted in the default time zone
Throws:
ClassCastException - if the passed object is not a Number

getCalendarFromNumber

public static Calendar getCalendarFromNumber(Object number)
Deprecated. Use getCalendarFromNumber(double) instead.

Gets the Calendar for the given Excel serial date.

Parameters:
date - the Excel serial date to be interpreted
Returns:
a Calendar representing the given Excel date interpreted in the default time zone
Throws:
ClassCastException - if the passed object is not a Number

getNonLocalizedCalendarFromNumber

public static Calendar getNonLocalizedCalendarFromNumber(Object number)
Deprecated. Identical to getDateFromNumber(Object).

Gets the Date for the given Excel serial date.

Parameters:
number - a Number representing the serial date to be interpreted
Returns:
a Date representing the given Excel date interpreted in the default time zone
Throws:
ClassCastException - if the passed object is not a Number

getDateFromCell

public static Date getDateFromCell(CellHandle cell)
Gets the Date for the given cell.

Parameters:
cell - a CellHandle whose value should be interpreted
Returns:
a Date representing the given Excel date interpreted in the default time zone

getCalendarFromCellWithPrecision

public static Calendar getCalendarFromCellWithPrecision(Cell cell,
                                                        int roundingCalendarField)
returns a Java Calendar from a CellHandle containing an Excel-formatted Date The Excel date format does not map 100% accurately to Java dates, due to the limitation of the precision of the Excel floating-point value record. Due to this, ExtenXLS dates may be too precise, this method will round the java.util.Date returned to the precision entered. Rounding is handled by the ROUND_HALF_UP method. Pass in a static Calendar precision, options are Calendar.HOUR Calendar.MINUTE Calendar.SECOND Calendar.MILLISECOND

Returns:
Calendar - A GregorianCalendar value of the Cell

getCalendarFromCell

public static Calendar getCalendarFromCell(Cell cell)
Gets the Calendar for the given cell.

Parameters:
cell - a CellHandle whose value should be interpreted
Returns:
a Calendar representing the given Excel date interpreted in the default time zone

isDatePattern

public static boolean isDatePattern(String possibleDate)
Inspects a string to determine if it is a date. Looks for a pattern such as mm/dd/yy, m/d/yy, mm/dd/yyyy etc.

Parameters:
possibleDate - the string to check for date formats
Returns:
whether the given string matches a known date format

convertStringToCalendar

public static Calendar convertStringToCalendar(String dateStr)
Converts a string representation of a date into a valid calendar object date must be in format mm/dd/yy (or yyyy)

Parameters:
dateStr -
Returns:
null if not a valid date

getFormattedDateVal

public static String getFormattedDateVal(CellHandle cell)
Deprecated. The date format handling in this method is wildly incorrect. It is retained only to provide compatibility with legacy ExtenXLS XML files.

Returns the value of the cell as a date formatted as a String date representation. The format is determined by inspecting the excel format. Currently supported formats in this method are: mm/dd/yy dd-mmm-yy dd-mmm mmm-yy mm/dd/yy hh:mm If the cell's date forrmat pattern falls outside of this range, the default output will be in the following format mm/dd/yyyy

Returns:
String, the value of a cell formatted as a date

parseDate

public static Date parseDate(String s,
                             int pat)
Deprecated. The date format handling in this method is wildly incorrect. It is retained only to provide compatibility with legacy ExtenXLS XML files.



Copyright © 2011 Extentech Inc. All Rights Reserved.