How do I convert a string of ISO-8601 datetime (ex: 2012-05-31T13:48:04Z
) to number of seconds( 10 digit integer
) using Java?
3 Answers
try this way
String DateStr="2012-05-31T13:48:04Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date d=sdf.parse(DateStr);
System.out.println(d.getTime());
output 1338452284000
From the comments of OP getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.Source

- 13,738
- 20
- 78
- 116
-
-
I don't see where the *10-digit integer* is which the OP is looking for. Also, the OP wants the seconds and not the milliseconds. Plus this would give the no. of seconds since `Epoch`. You need to mention quite a few things in here mate! :) – Rahul Dec 02 '13 at 07:16
-
-
@R.J I was on the way of editing few things.As you know we are not supposed to give the complete code so I just gave an example likely a hint.If you say then i will provide the entire code. – SpringLearner Dec 02 '13 at 07:19
-
@JqueryLearner - I did not say that you need to provide the entire code(you did already provide a lot though). You could have mentioned that it is in millis and that it needs to be converted to int. Also, the fact that it gives the milli/seconds since Epoch, etc. – Rahul Dec 02 '13 at 07:20
-
1@R.J Thanks for your suggestion,I have edited my answer.Thanks again for suggesting for better answer – SpringLearner Dec 02 '13 at 07:23
tl;dr
Instant.parse( "2012-05-31T13:48:04Z" )
.getEpochSecond()
1338472084
See this code run live at IdeOne.com.
Using java.time
Much easier with the java.time classes that supplant the troublesome old legacy date-time classes.
Easy to parse your input string as the java.time classes use ISO 8601 formats by default when generating/parsing strings. So no need to specify a formatting pattern.
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Instant instant = Instant.parse( "2012-05-31T13:48:04Z" ) ;
I am guessing that by “seconds” you meant the number of seconds elapsed since the beginning of 1970 UTC (1970-01-01T00:00:00Z
). The Instant
class can tell you the number of seconds since that Unix epoch.
long secondsSinceEpoch = instant.getEpochSecond() ;
1338472084
Beware of data loss, obviously. You are ignoring any fractional second that may be present in your date-time value.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.

- 1
- 1

- 303,325
- 100
- 852
- 1,154