this would be done using different SimpleDateFormat patterns.
Here a list of patterns for the individual declarations in RFC 3339:
- date-fullyear:
yyyy
- date-month:
MM
- date-mday:
dd
- time-hour:
HH
- time -minute:
mm
- time-second:
ss
- time-secfrac:
.SSS
(S
means millisecond, though - it is not clear what would happen if there are more or less than 3 digits of these.) - time-numoffset: (like
+02:00
seems to be not supported - instead it supports the formats+0200
,GMT+02:00
and some named time zones usingz
andZ
.) - time-offset:
'Z'
(not supporting other time zones) - you should useformat.setTimezone(TimeZone.getTimeZone("UTC"))
before using this.) - partial-time:
HH:mm:ss
orHH:mm:ss.SSS
. - full-time:
HH:mm:ss'Z'
orHH:mm:ss.SSS'Z'
. - full-date:
yyyy-MM-dd
- date-time:
yyyy-MM-dd'T'HH:mm:ss'Z'
oryyyy-MM-dd'T'HH:mm:ss.SSS'Z'
As we can see, this seems not to be able to parse everything. Maybe it would be a better idea to implement an RFC3339DateFormat
from scratch (using regular expressions, for simplicity, or parsing by hand, for efficiency).
so,just the below is what i want
System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.format(new Date()));
参考:https://stackoverflow.com/questions/6038136/how-do-i-parse-rfc-3339-datetimes-with-java
https://stackoverflow.com/questions/289311/output-rfc-3339-timestamp-in-java
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html#iso8601timezone