org.apache.poi.ss.usermodel
Class DateUtil

java.lang.Object
  extended by org.apache.poi.ss.usermodel.DateUtil
Direct Known Subclasses:
HSSFDateUtil

public class DateUtil
extends java.lang.Object

Contains methods for dealing with Excel dates.

Author:
Michael Harhen, Glen Stampoultzis (glens at apache.org), Dan Sherman (dsherman at isisph.com), Hack Kampbjorn (hak at 2mba.dk), Alex Jacoby (ajacoby at gmail.com), Pavel Krupets (pkrupets at palmtreebusiness dot com)

Constructor Summary
protected DateUtil()
           
 
Method Summary
protected static int absoluteDay(java.util.Calendar cal, boolean use1904windowing)
          Given a Calendar, return the number of days since 1900/12/31.
static double convertTime(java.lang.String timeStr)
          Converts a string of format "HH:MM" or "HH:MM:SS" to its (Excel) numeric equivalent
static double getExcelDate(java.util.Calendar date, boolean use1904windowing)
          Given a Date in the form of a Calendar, converts it into a double representing its internal Excel representation, which is the number of days since 1/1/1900.
static double getExcelDate(java.util.Date date)
          Given a Date, converts it into a double representing its internal Excel representation, which is the number of days since 1/1/1900.
static double getExcelDate(java.util.Date date, boolean use1904windowing)
          Given a Date, converts it into a double representing its internal Excel representation, which is the number of days since 1/1/1900.
static java.util.Date getJavaDate(double date)
          Given an Excel date with using 1900 date windowing, and converts it to a java.util.Date.
static java.util.Date getJavaDate(double date, boolean use1904windowing)
          Given an Excel date with either 1900 or 1904 date windowing, converts it to a java.util.Date.
static boolean isADateFormat(int formatIndex, java.lang.String formatString)
          Given a format ID and its format String, will check to see if the format represents a date format or not.
static boolean isCellDateFormatted(Cell cell)
          Check if a cell contains a date Since dates are stored internally in Excel as double values we infer it is a date if it is formatted as such.
static boolean isCellInternalDateFormatted(Cell cell)
          Check if a cell contains a date, checking only for internal excel date formats.
static boolean isInternalDateFormat(int format)
          Given a format ID this will check whether the format represents an internal excel date format or not.
static boolean isValidExcelDate(double value)
          Given a double, checks if it is a valid Excel date.
static java.util.Date parseYYYYMMDDDate(java.lang.String dateStr)
          Converts a string of format "YYYY/MM/DD" to its (Excel) numeric equivalent
static void setCalendar(java.util.Calendar calendar, int wholeDays, int millisecondsInDay, boolean use1904windowing)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DateUtil

protected DateUtil()
Method Detail

getExcelDate

public static double getExcelDate(java.util.Date date)
Given a Date, converts it into a double representing its internal Excel representation, which is the number of days since 1/1/1900. Fractional days represent hours, minutes, and seconds.

Parameters:
date - the Date
Returns:
Excel representation of Date (-1 if error - test for error by checking for less than 0.1)

getExcelDate

public static double getExcelDate(java.util.Date date,
                                  boolean use1904windowing)
Given a Date, converts it into a double representing its internal Excel representation, which is the number of days since 1/1/1900. Fractional days represent hours, minutes, and seconds.

Parameters:
date - the Date
use1904windowing - Should 1900 or 1904 date windowing be used?
Returns:
Excel representation of Date (-1 if error - test for error by checking for less than 0.1)

getExcelDate

public static double getExcelDate(java.util.Calendar date,
                                  boolean use1904windowing)
Given a Date in the form of a Calendar, converts it into a double representing its internal Excel representation, which is the number of days since 1/1/1900. Fractional days represent hours, minutes, and seconds.

Parameters:
date - the Calendar holding the date to convert
use1904windowing - Should 1900 or 1904 date windowing be used?
Returns:
Excel representation of Date (-1 if error - test for error by checking for less than 0.1)

getJavaDate

public static java.util.Date getJavaDate(double date)
Given an Excel date with using 1900 date windowing, and converts it to a java.util.Date. NOTE: If the default TimeZone in Java uses Daylight Saving Time then the conversion back to an Excel date may not give the same value, that is the comparison excelDate == getExcelDate(getJavaDate(excelDate,false)) is not always true. For example if default timezone is Europe/Copenhagen, on 2004-03-28 the minute after 01:59 CET is 03:00 CEST, if the excel date represents a time between 02:00 and 03:00 then it is converted to past 03:00 summer time

Parameters:
date - The Excel date.
Returns:
Java representation of the date, or null if date is not a valid Excel date
See Also:
TimeZone

getJavaDate

public static java.util.Date getJavaDate(double date,
                                         boolean use1904windowing)
Given an Excel date with either 1900 or 1904 date windowing, converts it to a java.util.Date. NOTE: If the default TimeZone in Java uses Daylight Saving Time then the conversion back to an Excel date may not give the same value, that is the comparison excelDate == getExcelDate(getJavaDate(excelDate,false)) is not always true. For example if default timezone is Europe/Copenhagen, on 2004-03-28 the minute after 01:59 CET is 03:00 CEST, if the excel date represents a time between 02:00 and 03:00 then it is converted to past 03:00 summer time

Parameters:
date - The Excel date.
use1904windowing - true if date uses 1904 windowing, or false if using 1900 date windowing.
Returns:
Java representation of the date, or null if date is not a valid Excel date
See Also:
TimeZone

setCalendar

public static void setCalendar(java.util.Calendar calendar,
                               int wholeDays,
                               int millisecondsInDay,
                               boolean use1904windowing)

isADateFormat

public static boolean isADateFormat(int formatIndex,
                                    java.lang.String formatString)
Given a format ID and its format String, will check to see if the format represents a date format or not. Firstly, it will check to see if the format ID corresponds to an internal excel date format (eg most US date formats) If not, it will check to see if the format string only contains date formatting characters (ymd-/), which covers most non US date formats.

Parameters:
formatIndex - The index of the format, eg from ExtendedFormatRecord.getFormatIndex
formatString - The format string, eg from FormatRecord.getFormatString
See Also:
isInternalDateFormat(int)

isInternalDateFormat

public static boolean isInternalDateFormat(int format)
Given a format ID this will check whether the format represents an internal excel date format or not.

See Also:
isADateFormat(int, java.lang.String)

isCellDateFormatted

public static boolean isCellDateFormatted(Cell cell)
Check if a cell contains a date Since dates are stored internally in Excel as double values we infer it is a date if it is formatted as such.

See Also:
isADateFormat(int, String), isInternalDateFormat(int)

isCellInternalDateFormatted

public static boolean isCellInternalDateFormatted(Cell cell)
Check if a cell contains a date, checking only for internal excel date formats. As Excel stores a great many of its dates in "non-internal" date formats, you will not normally want to use this method.

See Also:
isADateFormat(int,String), isInternalDateFormat(int)

isValidExcelDate

public static boolean isValidExcelDate(double value)
Given a double, checks if it is a valid Excel date.

Parameters:
value - the double value
Returns:
true if valid

absoluteDay

protected static int absoluteDay(java.util.Calendar cal,
                                 boolean use1904windowing)
Given a Calendar, return the number of days since 1900/12/31.

Parameters:
cal - the Calendar
Returns:
days number of days since 1900/12/31
Throws:
java.lang.IllegalArgumentException - if date is invalid

convertTime

public static double convertTime(java.lang.String timeStr)
Converts a string of format "HH:MM" or "HH:MM:SS" to its (Excel) numeric equivalent

Returns:
a double between 0 and 1 representing the fraction of the day

parseYYYYMMDDDate

public static java.util.Date parseYYYYMMDDDate(java.lang.String dateStr)
Converts a string of format "YYYY/MM/DD" to its (Excel) numeric equivalent

Returns:
a double representing the (integer) number of days since the start of the Excel epoch


Copyright 2012 The Apache Software Foundation or its licensors, as applicable.