article
How To Convert Between Serial Dates and Java™ Dates
October 31, 2014

This article provides some Java™ code that extends the functionality in the GregorianCalendar class so that it can be initialized using dates in multiple formats. This Java™ code can also generate a serial date for the current calendar date. The extended class is very handy, since it allows for easy conversion between Java™'s native Date type and the serial date format which is used in the FINCAD library. It could also be used to convert serial dates that come from SQL Server, Excel or FINCAD functions into standard Java™ dates.

» Click to evaluate the lastest trial version of FINCAD Analytics.

To run this example, you need to first compile the code in SerialDate.java using the command below

javac SerialDate.java

You then use the command below to run the example code in the current folder
java –classpath .\; SerialDate

The example also allows you to pass command line arguments so that you can see the results of calculating a serial date for a specific date. You can pass a known serial date, just the year, month and day or year, month, day, hour, minute and second.

The default case where no command line argument is provided causes the SerialDate class to be initialized in a variety of ways and the results are printed to the console.

The SerialDate class has multiple constructors to allow the class to be initialized using Dates in a variety of formats.

// Default constructor – use setDate method to initialize it later
public SerialDate()

// Initialize using numeric equivalent for standard Java Date (i.e. getTime() equivalent)
public SerialDate( long JavaDateTime )

// Initialize using standard Java Date
public SerialDate( Date dJavaDateTime )

// Initialize using Year, Month and Day
public SerialDate(int iYear, int iMonth, int iDay)

// Initialize using Year, Month, Day, Hour, Minute and Second
public SerialDate(int iYear, int iMonth, int iDay, int iHrs, int iMin, int iSec)

// Initialize using a serial date value
public SerialDate(double date)

The setDate() method is called internally when you initialize the class using a serial date. It can also be used when you create the instance using the default constructor.

The ToDouble() method is used to return the serial date associated with the current calendar date.

The member variable bShowConversions is used to control whether or not the date information is printed to the console when one of the class constructors is called. When set to 'True' the Print() method is called and this causes the date from the Date class to be printed in a variety of formats to show what is happening inside the class.

Note:
1) When passing a single hardwired input using a numeric value, you need to typecast based on what that numeric value represents. If it is a serial date then typecast it as double and if it is the number of milliseconds since January 1, 1970 then typecast it as long.

2) The Microsoft Knowledge Base article referenced below is a bit dated and uses some Date methods that are now deprecated.

3) There are also some routines inside the FINCAD math library that are useful for creating serial dates and getting the year, month and day given a particular serial date.

aaDateSerial( Year, Month, Day ); // This FINCAD routine builds a serial date given Year, Month and Day

aaDay( SerialDate ); // Returns the numeric value for the day of the month for the given serial date

aaMonth( SerialDate ); // Returns the numeric value for the month given the serial date

aaYear( SerialDate ); // Returns the numeric value for the year given the serial date

These FINCAD routines are useful when working with serial dates in Java™ but the SerialDate class provided with this article supports more date formats and provides easy conversion between the date types. This will make it easier for you to work with serial dates in Java™ and will make it quicker for you to get started working with FINCAD Developer in Java™.

References

1. “How To OleDate: Conversion Between DATE and java.util.Date", Microsoft Knowledge Base article (169795), August 25, 2005
2. “Calculating Java dates" by Robert Nielson, JavaWorld.com online article, December 29, 2000
3. “Calculate, customize, and parse dates with Java Date and Calendar classes", Katherine Wright, TechRepublic online article, October 8, 2001

Disclaimer

Your use of the information in this article is at your own risk. The information in this article is provided on an "as is" basis and without any representation, obligation, or warranty from FINCAD of any kind, whether express or implied. We hope that such information will assist you, but it should not be used or relied upon as a substitute for your own independent research.

For more information or a customized demonstration of the software, contact a FINCAD Representative.