Class KmgDateTimeConverter
- java.lang.Object
-
- KmgDateTimeConverter
-
public class KmgDateTimeConverter extends Object
Collection of methods for the conversion between a double value containing decimal seconds since 01-Jan-1970 00:00:00 UTC and a date time String. A negative double value represents a date time before 1-Jan-1970.
-
-
Field Summary
Fields Modifier and Type Field Description static doubleINVALIDValue returned for unparseable date time String.
-
Constructor Summary
Constructors Constructor Description KmgDateTimeConverter()Creates aKmgDateTimeConverterinstance.KmgDateTimeConverter(String defaultDateTimeFormatPattern)KmgDateTimeConverter(DateFormat df)Creates aKmgDateTimeConverterinstance usingdf.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static doubleforDate(Date d)Returns decimal seconds since 01-Jan-1970 00:00:00 UTC ford.Stringformat(double z)Formatszas a date time string using the internally storedDateFormatinstance.static Stringformat(double z, DateFormat df)Formatszas a date time string usingdf.static StringformatDate(double z)static StringformatDateTime(double z)Formatszas a date time string (e.g.dd.MM.yyyy HH:mm:ss zusinggetDateTimeInstance( DateFormat.MEDIUM, DateFormat.LONG )).static StringformatDateTime(double z, double dz)Formatszas a date time string with precisiondzin seconds.static StringformatDateTime(double z, DateFormat df)Formatszas a date time string usingdf.static StringformatTime(double z)DateFormatgetDateFormat()Returns the internally storedDateFormatinstance.static DateFormatgetDateTimeInstance(int dateStyle, int timeStyle)Restores the jdk8 behaviour ofDateFormat.getDateTimeInstance(int, int)to avoid the verbose date time format introduced with jdk9.static DateFormatgetDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)Restores the jdk8 behaviour ofDateFormat.getDateTimeInstance(int, int, Locale)to avoid the verbose date time format introduced with jdk9.doubleparse(String text)Parses text as a date time string.static doubleparseDateTime(String text)Parses text as a date time string usingparse.static doubleparseDateTime(String text, DateFormat df)Parses text as a date time string usingdfandparse.doubleparseDouble(String text)ReturnsDouble.parseDouble(java.lang.String), but (if necessary) returnsDouble.NaNinstead of throwing aNumberFormatException.protected DateparseHilf(String text, ParsePosition pos)Used internally.static DatetoDate(double z)Returnsjava.util.Datefor decimal seconds since 01-Jan-1970 00:00:00 UTC.StringtoPattern()Returns a pattern string describing the internally storedDateFormatinstance.static StringtoPattern(DateFormat df)Returns a pattern string describingdf.
-
-
-
Field Detail
-
INVALID
public static final double INVALID
Value returned for unparseable date time String.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
KmgDateTimeConverter
public KmgDateTimeConverter()
Creates aKmgDateTimeConverterinstance.
-
KmgDateTimeConverter
public KmgDateTimeConverter(DateFormat df)
Creates aKmgDateTimeConverterinstance usingdf. If theDateFormatdoes not match forparse(…),dfis adjusted internally.
-
KmgDateTimeConverter
public KmgDateTimeConverter(String defaultDateTimeFormatPattern)
-
-
Method Detail
-
getDateFormat
public DateFormat getDateFormat()
Returns the internally storedDateFormatinstance.
-
toPattern
public String toPattern()
Returns a pattern string describing the internally storedDateFormatinstance.
-
toPattern
public static String toPattern(DateFormat df)
Returns a pattern string describingdf.
-
getDateTimeInstance
public static DateFormat getDateTimeInstance(int dateStyle, int timeStyle)
Restores the jdk8 behaviour ofDateFormat.getDateTimeInstance(int, int)to avoid the verbose date time format introduced with jdk9. Workaround for Bug ID: → JDK-8194735 (section Comments).- See Also:
DateFormat.getDateTimeInstance(int, int)
-
getDateTimeInstance
public static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
Restores the jdk8 behaviour ofDateFormat.getDateTimeInstance(int, int, Locale)to avoid the verbose date time format introduced with jdk9. Workaround for Bug ID: → JDK-8194735 (section Comments).
-
formatDateTime
public static String formatDateTime(double z, DateFormat df)
Formatszas a date time string usingdf.- Returns:
format( z, df ).
-
formatDateTime
public static String formatDateTime(double z)
Formatszas a date time string (e.g.dd.MM.yyyy HH:mm:ss zusinggetDateTimeInstance( DateFormat.MEDIUM, DateFormat.LONG )).
-
formatDateTime
public static String formatDateTime(double z, double dz)
Formatszas a date time string with precisiondzin seconds.
-
formatDate
public static String formatDate(double z)
-
formatTime
public static String formatTime(double z)
-
format
public static String format(double z, DateFormat df)
Formatszas a date time string usingdf.
-
format
public String format(double z)
Formatszas a date time string using the internally storedDateFormatinstance.
-
parseDateTime
public static double parseDateTime(String text)
Parses text as a date time string usingparse.
-
parseDateTime
public static double parseDateTime(String text, DateFormat df)
Parses text as a date time string usingdfandparse.
-
parse
public double parse(String text)
Parses text as a date time string. Tries various date time patterns to find a matchingDateFormatstarting with the internally stored instance.
The patterns are tested in the sequence given in the following table until a matching format is found:pattern text (example) comment yyyy-MM-dd'T'HH:mm:ssXX
yyyy-MM-dd'T'HH:mm:ssX2013-12-31T23:59:59+0100
2013-12-31T23:59:59Zexplicit TimeZonee.g. GMT+01:00
explicit UTC = GMT+0000yyyy-MM-dd'T'HH:mm:ss2013-12-31T23:59:59default TimeZoneyyyy-MM-dd'T'HH:mm2013-12-31T23:59default TimeZoneyyyy-MM-dd HH:mm:ss2013-12-31 23:59:59default TimeZoneyyyy-MM-dd HH:mm2013-12-31 23:59default TimeZoneyyyy-MM-dd2013-12-31default TimeZonedateStyle:SHORT
timeStyle:MEDIUMe.g. 31.12.13 23:59:59
or31.12.2013 23:59:59default DateTimeInstancefor theLocaleof the Java Virtual MachinedateStyle:SHORT
timeStyle:SHORTe.g. 31.12.13 23:59
or31.12.2013 23:59default DateTimeInstancefor theLocaleof the Java Virtual MachinedateStyle:SHORTe.g. 31.12.13
or31.12.2013default DateInstancefor theLocaleof the Java Virtual MachinetimeStyle:MEDIUMe.g. 23:59:59default TimeInstanceforLocaletimeStyle:SHORTe.g. 23:59default TimeInstanceforLocaleyyyyMMddHHmmss.SSS20131231235959.123with ms, default TimeZoneyyyyMMddHHmmssXX
yyyyMMddHHmmssX20131231235959+0100
20131231235959Zexplicit TimeZonee.g. GMT+01:00
explicit UTC = GMT+0000yyyyMMddHHmmss20131231235959default TimeZoneyyyyMMddHHmm201312312359default TimeZoneyyyyMMdd20131231default TimeZoneM/d/yyyy H:mm:ss12/31/2013 23:59:59MS-Excel csv-export, default TimeZoneM/d/yyyy H:mm12/31/2013 23:59MS-Excel csv-export, default TimeZoneM/d/yyyy12/31/2013MS-Excel csv-export, default TimeZone
If theTimeZoneis not given intextand has not been changed for theDateFormatdfusingdf.setTimeZone(…),parseuses the defaultTimeZoneof the Java Virtual Machine. The default can be changed usingTimeZone.setDefault(java.util.TimeZone).
The patterns withdateStyleandtimeStyleuse aDateTimeInstancefor the defaultLocaleof the Java Virtual Machine.
-
parseHilf
protected Date parseHilf(String text, ParsePosition pos)
Used internally.
-
forDate
public static double forDate(Date d)
Returns decimal seconds since 01-Jan-1970 00:00:00 UTC ford.- Returns:
0.001 * d.getTime()
-
toDate
public static Date toDate(double z)
Returnsjava.util.Datefor decimal seconds since 01-Jan-1970 00:00:00 UTC.- Returns:
newDate(Math.round( 1000 * z ) )
-
parseDouble
public double parseDouble(String text)
ReturnsDouble.parseDouble(java.lang.String), but (if necessary) returnsDouble.NaNinstead of throwing aNumberFormatException.
-
-