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 double
INVALID
Value returned for unparseable date time String.
-
Constructor Summary
Constructors Constructor Description KmgDateTimeConverter()
Creates aKmgDateTimeConverter
instance.KmgDateTimeConverter(String defaultDateTimeFormatPattern)
KmgDateTimeConverter(DateFormat df)
Creates aKmgDateTimeConverter
instance usingdf
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static double
forDate(Date d)
Returns decimal seconds since 01-Jan-1970 00:00:00 UTC ford
.String
format(double z)
Formatsz
as a date time string using the internally storedDateFormat
instance.static String
format(double z, DateFormat df)
Formatsz
as a date time string usingdf
.static String
formatDate(double z)
static String
formatDateTime(double z)
Formatsz
as a date time string (e.g.dd.MM.yyyy HH:mm:ss z
usinggetDateTimeInstance( DateFormat.MEDIUM, DateFormat.LONG )
).static String
formatDateTime(double z, double dz)
Formatsz
as a date time string with precisiondz
in seconds.static String
formatDateTime(double z, DateFormat df)
Formatsz
as a date time string usingdf
.static String
formatTime(double z)
DateFormat
getDateFormat()
Returns the internally storedDateFormat
instance.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.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.double
parse(String text)
Parses text as a date time string.static double
parseDateTime(String text)
Parses text as a date time string usingparse
.static double
parseDateTime(String text, DateFormat df)
Parses text as a date time string usingdf
andparse
.double
parseDouble(String text)
ReturnsDouble.parseDouble(java.lang.String)
, but (if necessary) returnsDouble.NaN
instead of throwing aNumberFormatException
.protected Date
parseHilf(String text, ParsePosition pos)
Used internally.static Date
toDate(double z)
Returnsjava.util.
Date
for decimal seconds since 01-Jan-1970 00:00:00 UTC.String
toPattern()
Returns a pattern string describing the internally storedDateFormat
instance.static String
toPattern(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 aKmgDateTimeConverter
instance.
-
KmgDateTimeConverter
public KmgDateTimeConverter(DateFormat df)
Creates aKmgDateTimeConverter
instance usingdf
. If theDateFormat
does not match forparse(…)
,df
is adjusted internally.
-
KmgDateTimeConverter
public KmgDateTimeConverter(String defaultDateTimeFormatPattern)
-
-
Method Detail
-
getDateFormat
public DateFormat getDateFormat()
Returns the internally storedDateFormat
instance.
-
toPattern
public String toPattern()
Returns a pattern string describing the internally storedDateFormat
instance.
-
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)
Formatsz
as a date time string usingdf
.- Returns:
format
( z, df )
.
-
formatDateTime
public static String formatDateTime(double z)
Formatsz
as a date time string (e.g.dd.MM.yyyy HH:mm:ss z
usinggetDateTimeInstance( DateFormat.MEDIUM, DateFormat.LONG )
).
-
formatDateTime
public static String formatDateTime(double z, double dz)
Formatsz
as a date time string with precisiondz
in seconds.
-
formatDate
public static String formatDate(double z)
-
formatTime
public static String formatTime(double z)
-
format
public static String format(double z, DateFormat df)
Formatsz
as a date time string usingdf
.
-
format
public String format(double z)
Formatsz
as a date time string using the internally storedDateFormat
instance.
-
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 usingdf
andparse
.
-
parse
public double parse(String text)
Parses text as a date time string. Tries various date time patterns to find a matchingDateFormat
starting 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 TimeZone
e.g. GMT+01:00
explicit UTC = GMT+0000
yyyy-MM-dd'T'HH:mm:ss
2013-12-31T23:59:59
default TimeZone
yyyy-MM-dd'T'HH:mm
2013-12-31T23:59
default TimeZone
yyyy-MM-dd HH:mm:ss
2013-12-31 23:59:59
default TimeZone
yyyy-MM-dd HH:mm
2013-12-31 23:59
default TimeZone
yyyy-MM-dd
2013-12-31
default TimeZone
dateStyle:
SHORT
timeStyle:MEDIUM
e.g. 31.12.13 23:59:59
or31.12.2013 23:59:59
default DateTimeInstance
for theLocale
of the Java Virtual MachinedateStyle:
SHORT
timeStyle:SHORT
e.g. 31.12.13 23:59
or31.12.2013 23:59
default DateTimeInstance
for theLocale
of the Java Virtual MachinedateStyle:
SHORT
e.g. 31.12.13
or31.12.2013
default DateInstance
for theLocale
of the Java Virtual MachinetimeStyle:
MEDIUM
e.g. 23:59:59
default TimeInstance
forLocale
timeStyle:
SHORT
e.g. 23:59
default TimeInstance
forLocale
yyyyMMddHHmmss.SSS
20131231235959.123
with ms, default TimeZone
yyyyMMddHHmmssXX
yyyyMMddHHmmssX20131231235959+0100
20131231235959Zexplicit TimeZone
e.g. GMT+01:00
explicit UTC = GMT+0000
yyyyMMddHHmmss
20131231235959
default TimeZone
yyyyMMddHHmm
201312312359
default TimeZone
yyyyMMdd
20131231
default TimeZone
M/d/yyyy H:mm:ss
12/31/2013 23:59:59
MS-Excel csv-export, default TimeZone
M/d/yyyy H:mm
12/31/2013 23:59
MS-Excel csv-export, default TimeZone
M/d/yyyy
12/31/2013
MS-Excel csv-export, default TimeZone
If theTimeZone
is not given intext
and has not been changed for theDateFormat
df
usingdf.
setTimeZone(…)
,parse
uses the defaultTimeZone
of the Java Virtual Machine. The default can be changed usingTimeZone.setDefault(java.util.TimeZone)
.
The patterns withdateStyle
andtimeStyle
use aDateTimeInstance
for the defaultLocale
of 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.
Date
for decimal seconds since 01-Jan-1970 00:00:00 UTC.- Returns:
new
Date
(
Math.round
( 1000 * z ) )
-
parseDouble
public double parseDouble(String text)
ReturnsDouble.parseDouble(java.lang.String)
, but (if necessary) returnsDouble.NaN
instead of throwing aNumberFormatException
.
-
-