public class DateConverter extends Object
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. OpenXLS 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.
| Modifier and Type | Class and Description |
|---|---|
static class |
DateConverter.DateFormat
The set of supported serial date encoding schemes.
|
| Constructor and Description |
|---|
DateConverter() |
| Modifier and Type | Method and Description |
|---|---|
static Double |
calcDateValue(String dateString)
DATEVALUE
Returns the serial number of the date represented by date_text.
|
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)
Deprecated.
Use
getCalendarFromNumber( double, DateFormat ) instead. |
static Calendar |
getCalendarFromNumber(double date,
DateConverter.DateFormat format)
Parses the the given Excel serial date and returns a Calendar.
|
static Calendar |
getCalendarFromNumber(Object number)
Deprecated.
Use
getCalendarFromNumber(double) instead. |
static Date |
getDate(String str)
attempt to interpret a date string into a date
returns null if cannot be converted to date
|
static Date |
getDateFromCell(CellHandle cell)
Gets the Date for the given cell.
|
static Date |
getDateFromNumber(double date)
Deprecated.
Use
getCalendarFromNumber( double, DateFormat ) instead. |
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
OpenXLS XML files.
|
static Calendar |
getNonLocalizedCalendarFromNumber(Object number)
Deprecated.
Identical to
getDateFromNumber(Object). |
static Date |
getNonLocalizedDateFromNumber(Object number)
Deprecated.
Identical to
getDateFromNumber(Object). |
static boolean |
getValidate()
Returns whether input validation is on.
|
static double |
getXLSDateVal(Calendar cal)
Deprecated.
Use
getXLSDateVal( Calendar, DateFormat ) instead. |
static double |
getXLSDateVal(Calendar cal,
DateConverter.DateFormat format)
Converts the value of the given Calendar to an Excel serial date.
|
static double |
getXLSDateVal(Date date)
Deprecated.
Use
getXLSDateVal( Date, DateFormat ) instead. |
static double |
getXLSDateVal(Date date,
DateConverter.DateFormat format)
Converts the given Date to an Excel serial 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
OpenXLS XML files.
|
static void |
setCalendar(Calendar cal)
Sets the calendar used to perform date calculation.
|
static void |
setValidate(boolean validate)
Sets whether to perform input validation.
|
public static Calendar getCalendar()
public static void setCalendar(Calendar cal)
cal - the calendar that should be used for date calculationspublic static boolean getValidate()
public static void setValidate(boolean validate)
public static boolean isParseableDateString(String str)
public static Date getDate(String str)
public static double getXLSDateVal(Calendar cal)
getXLSDateVal( Calendar, DateFormat ) instead.cal - the Calendar to convertpublic static double getXLSDateVal(Calendar cal, DateConverter.DateFormat format)
cal - the Calendar to convertformat - the serial date format to usepublic static double getXLSDateVal(Date date, DateConverter.DateFormat format)
date - the date to be convertedformat - the serial date format to usepublic static double getXLSDateVal(Date date)
getXLSDateVal( Date, DateFormat ) instead.date - the date to be convertedpublic static Calendar getCalendarFromNumber(double date)
getCalendarFromNumber( double, DateFormat ) instead.date - the Excel serial date to be interpretedpublic static Calendar getCalendarFromNumber(double date, DateConverter.DateFormat format)
date - the Excel serial date to be interpretedformat - the date format with which to interpret the serial datepublic static Date getDateFromNumber(double date)
getCalendarFromNumber( double, DateFormat ) instead.date - the Excel serial date to be interpretedformat - the date format with which to interpret the serial datepublic static Date getDateFromNumber(Object number)
getDateFromNumber(double) instead.number - a Number representing the serial date to be interpretedClassCastException - if the passed object is not a Numberpublic static Date getNonLocalizedDateFromNumber(Object number)
getDateFromNumber(Object).number - a Number representing the serial date to be interpretedClassCastException - if the passed object is not a Numberpublic static Calendar getCalendarFromNumber(Object number)
getCalendarFromNumber(double) instead.date - the Excel serial date or datestring to be interpretedClassCastException - if the passed object is not a Numberpublic static Calendar getNonLocalizedCalendarFromNumber(Object number)
getDateFromNumber(Object).number - a Number representing the serial date to be interpretedClassCastException - if the passed object is not a Numberpublic static Date getDateFromCell(CellHandle cell)
cell - a CellHandle whose value should be interpretedpublic static Calendar getCalendarFromCellWithPrecision(Cell cell, int roundingCalendarField)
public static Calendar getCalendarFromCell(Cell cell)
cell - a CellHandle whose value should be interpretedpublic static boolean isDatePattern(String possibleDate)
possibleDate - the string to check for date formatspublic static Calendar convertStringToCalendar(String dateStr)
dateStr - public static String getFormattedDateVal(CellHandle cell)
public static Date parseDate(String s, int pat)
public static Double calcDateValue(String dateString)
String - dateStringCopyright © 2002–2019 Starter Inc.. All rights reserved.