Java SimpleDateFormat: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(25 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=External=


* http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
* https://docs.oracle.com/javase/10/docs/api/java/text/SimpleDateFormat.html


=Internal=
=Internal=


* [[java Format#Subjects|java Format]]
* [[java Format#Subjects|java Format]]
* [[ISO 8601]]
=Overview=
<tt>DateFormat.parse()</tt> returns a <tt>Date</tt> instance whose <tt>getTime()</tt> is always relative to GMT, regardless of the default JVM timezone or the timezone offset specified in the date string. For more details on <tt>DateFormat.parse()</tt> and timezone, see [[Java_Time#Time_Zone_and_DateFormat|Java Time - Time Zone and DateFormat]].
=Thread Safety=
SimpleDateFormat instances are '''NOT thread safe'''.  It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally. A common solution is to '''clone''' the format instance to avoid troubles.


=Samples=
=Samples=
Line 13: Line 22:
</pre>
</pre>


produces
produces:


<pre>
<pre>
13/01/31 01:00:00,000 PM
13/01/31 01:00:00,000 PM
</pre>
</pre>


=Excel, CSV and milliseconds=
=Excel, CSV and milliseconds=
Line 32: Line 40:
=Hour in Day=
=Hour in Day=


__Warning__ If __not__ using AM/PM (no "a" in the format), make sure you use HH for hours (instead of hh).
'''Warning''' If '''not''' using AM/PM (no "a" in the format), make sure you use HH for hours (instead of hh).


According to the documentation:
According to the documentation:
Line 39: Line 47:
* "H" - Hour in a day (0-23)
* "H" - Hour in a day (0-23)


=Time Zone=
=Month=
 
<pre>
Z
</pre>
 
for -0800


Just one single "Z" not four or five.
To parse "Sep" use "MMM".


=UTC=
To parse "09" use "MM".


Use [time zone|java SimpleDateFormat#Time_Zone] (see above) and "0000".
="T" in timestamp=


Examples:
If "T" shows in the timestamp, it can be specified as 'T' in format.


This is UTC.
Example: "2016-08-03T13:54:39.464-0400" is parsed with:


<pre>
<pre>
15/01/01 01:01:01+0000
new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
</pre>
</pre>


This is PDT.
=DateFormat and Timezone=
 
<pre>
15/01/01 01:01:01-0700
</pre>
 
 
=Z in Time Stamp=


"Z" means GMT in ISO 8601
{{Internal|Java_Time#Time_Zone_and_DateFormat|Java Time - Time Zone and DateFormat}}


To parse that, add "X" in the pattern.
=ISO 8601 Format=


=Various Examples=
<syntaxhighlight lang='java'>
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")
</syntaxhighlight>


"2015-09-22T18:03:46Z" is parsed by "yyyy-MM-dd'T'HH:mm:ssX"
Also see: {{Internal|ISO_8601#Java_SimpleDateFormat|ISO 8601}}

Latest revision as of 17:20, 14 November 2018

External

Internal

Overview

DateFormat.parse() returns a Date instance whose getTime() is always relative to GMT, regardless of the default JVM timezone or the timezone offset specified in the date string. For more details on DateFormat.parse() and timezone, see Java Time - Time Zone and DateFormat.

Thread Safety

SimpleDateFormat instances are NOT thread safe. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally. A common solution is to clone the format instance to avoid troubles.

Samples

"yy/MM/dd hh:mm:ss,SSS a"

produces:

13/01/31 01:00:00,000 PM

Excel, CSV and milliseconds

Excel understands "." so you may want to:

13/01/31 01:00:00.000 

More here: https://home.feodorov.com:9443/wiki/Wiki.jsp?page=ExcelTimeAndDateFunctions#section-ExcelTimeAndDateFunctions-HandlingMillisecondsInExcel

Hour in Day

Warning If not using AM/PM (no "a" in the format), make sure you use HH for hours (instead of hh).

According to the documentation:

  • "h" - Hour in am/pm (1-12)
  • "H" - Hour in a day (0-23)

Month

To parse "Sep" use "MMM".

To parse "09" use "MM".

"T" in timestamp

If "T" shows in the timestamp, it can be specified as 'T' in format.

Example: "2016-08-03T13:54:39.464-0400" is parsed with:

new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");

DateFormat and Timezone

Java Time - Time Zone and DateFormat

ISO 8601 Format

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")

Also see:

ISO 8601