com.devexperts.util
Class TimeFormat

java.lang.Object
  extended by com.devexperts.util.TimeFormat

public class TimeFormat
extends Object

Utility class for parsing and formatting dates and times in ISO-compatible format.


Field Summary
static TimeFormat DEFAULT
          An instance of TimeFormat that corresponds to default timezone as returned by TimeZone.getDefault() method.
static TimeFormat GMT
          An instance of TimeFormat that corresponds to GMT timezone as returned by TimeZone.getTimeZone("GMT").
 
Method Summary
 String format(Date time)
          Converts Date object into string according to the format like yyyyMMdd-HHmmss.
 String format(long time)
          This is a shortcut for format(new Date(time)).
static TimeFormat getInstance(TimeZone timezone)
          Returns TimeFormat instance for a specified timezone.
 TimeZone getTimeZone()
          Returns timezone of this TimeFormat instance.
 Date parse(String value)
          Reads Date from String.
 TimeFormat withMillis()
          Returns TimeFormat instance that also includes milliseconds into string when using format method.
 TimeFormat withTimeZone()
          Returns TimeFormat instance that also includes timezone into string when using format method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT

public static final TimeFormat DEFAULT
An instance of TimeFormat that corresponds to default timezone as returned by TimeZone.getDefault() method.


GMT

public static final TimeFormat GMT
An instance of TimeFormat that corresponds to GMT timezone as returned by TimeZone.getTimeZone("GMT").

Method Detail

getInstance

public static TimeFormat getInstance(TimeZone timezone)
Returns TimeFormat instance for a specified timezone.

Parameters:
timezone - timezone for TimeFormat.
Throws:
NullPointerException - if timezone is null.

withTimeZone

public TimeFormat withTimeZone()
Returns TimeFormat instance that also includes timezone into string when using format method.


withMillis

public TimeFormat withMillis()
Returns TimeFormat instance that also includes milliseconds into string when using format method.


parse

public Date parse(String value)
           throws InvalidFormatException,
                  NullPointerException
Reads Date from String.

It accepts the following formats of date:

Here

Parameters:
value - String value to parse.
Returns:
Date parsed from value.
Throws:
InvalidFormatException - if value has wrong format.
NullPointerException - if value == null.
See Also:
SimpleDateFormat

format

public String format(Date time)
              throws NullPointerException
Converts Date object into string according to the format like yyyyMMdd-HHmmss. When withTimeZone() was used to acquire this TimeFormat instance, then time zone is also included and formatted according to RFC 822 format (for example "+0300"). When withMillis() was used to acquire this TimeFormat instance, the milliseconds are also included as .sss.

When time.getTime() == 0 this method returns string "0".

Parameters:
time - date and time to format.
Returns:
string representation of data and time.
Throws:
NullPointerException - if time is null.

format

public String format(long time)
This is a shortcut for format(new Date(time)).


getTimeZone

public TimeZone getTimeZone()
Returns timezone of this TimeFormat instance.

Returns:
timezone of this TimeFormat instance.


Copyright © 2013 Devexperts. All Rights Reserved.