MATLAB: How to convert Java dates to MATLAB dates and retain daylight saving time details when using MATLAB 7.9 (R2009a)


I have a Java program that returns a list of dates to MATLAB. I would like to convert these days to MATLAB dates while retaining the daylight saving time and other details of the dates.

Best Answer

In order to do this, you will need to use the Java class "Calendar", as follows:
Calendar cal = cal.setTimeZone(TimeZone.getTimeZone("GMT"));
javaSerialDate = cal.getTimeInMillis() - cal.get(cal.ZONE_OFFSET) - cal.get(cal.DST_OFFSET);
You can then convert the date to a MATLAB date with the following MATLAB code:
matlabSerialDate = javaSerialDate / 1000 / 3600 / 24 + datenum('1970-01-01')
% -OR-
matlabSerialDate = datenum([1970 1 1 0 0 javaSerialDate / 1000])