com.metsci.glimpse.util.units.time.format
Class TimeStampFormatStandard
java.lang.Object
com.metsci.glimpse.util.units.time.format.TimeStampFormatStandard
- All Implemented Interfaces:
- TimeStampFormat, java.lang.Cloneable
public class TimeStampFormatStandard
- extends java.lang.Object
- implements TimeStampFormat
Formats and parses TimeStamps according to a specified format string, in the spirit of trusty
old printf. This is similar to java.text.SimpleDateFormat, but with a few improvements:
- Instances are thread-safe
- Fractional seconds are handled intelligently
- Time-zone is associated with the format, not with the date/time object
- Formatting options are specified printf-style (e.g. "%3S" instead of "SSS")
A good basic example is TimeStampFormat.iso8601:
- format: %y-%M-%dT%H:%m:%SZ
- output: 2008-06-11T13:40:12.5439231Z
Any characters not inside a format term -- such as the "T", "Z", dashes, and colons in the
above example -- will simply show up in as literals in the output string. Just like printf.
So if you want date and time separated by a space, and "UTC" instead of "Z":
- format: %y-%M-%d %H:%m:%S UTC
- output: 2008-06-11 13:40:12.5439231 UTC
To make text fields all uppercase or all lowercase, use ^ or /:
- %N --> January
- %^N --> JANUARY
- %/N --> january
To abbreviate the text month, use 3:
- %N --> January
- %3N --> Jan
To change padding of numeric fields, use <, >, or !:
- %H:%m --> 04:10
- %<H:%m --> 4 :10
- %>H:%m --> 4:10
- %!H:%m --> 4:10
Specify the number of decimal places used by %S with a digit:
- %S --> 12.5439231
- %3S --> 12.544
Other features that would be nice:
- text day of week (%E --> Tuesday, %3E --> Tue)
- 2-digit year (%2y)
- hour in am/pm (1-12)
- am/pm
Rollover and the %S Field
The %S field is special: its precision affects when other fields (potentially all of them)
roll over to the next value. For instance, if a time formatted with a %S gives the string
"1999/12/31 23:59:59.999", then it will give "2000/01/01 00:00:00" when formatted with %0S.
Note that the value of every field changed, solely because of a change in the precision of
the %S field.
If a format string does not contain a %S field, other fields will behave as though there
were a full-precision %S. For instance:
- time: 1999/12/31 23:59:59.999
- format: %y-%M-%d
- output: 1999/12/31
If, for some strange reason, a format string contains multiple %S fields, the precision of
the rightmost one is used for figuring rollover.
Method Summary |
java.lang.String |
format(java.math.BigDecimal posixSeconds)
|
com.metsci.glimpse.util.units.time.format.TimeStampFormatStandard.Field |
newField(char code,
java.lang.String flags)
|
java.math.BigDecimal |
parse(java.lang.String string)
|
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
TimeStampFormatStandard
public TimeStampFormatStandard(java.lang.String format,
java.lang.String timeZoneName)
TimeStampFormatStandard
public TimeStampFormatStandard(java.lang.String format,
java.util.TimeZone timeZone)
newField
public com.metsci.glimpse.util.units.time.format.TimeStampFormatStandard.Field newField(char code,
java.lang.String flags)
parse
public java.math.BigDecimal parse(java.lang.String string)
- Specified by:
parse
in interface TimeStampFormat
- Returns:
- seconds since the epoch
format
public java.lang.String format(java.math.BigDecimal posixSeconds)
- Specified by:
format
in interface TimeStampFormat
- Returns:
- a date-time string, formatted according to this format
Copyright © 2012 Metron, Inc.. All Rights Reserved.