Joda-Time
FYI, here is the same kind of code but done in Joda-Time 2.4. The new java.time package in Java 8 (inspired by Joda-Time) could be used in a similar manner.
The java.util.Date and .Calendar classes are notoriously troublesome. Avoid them. Use either of the two libraries mentioned above.
Example Code
Your input string is close to standard ISO 8601 format, but not quite, missing the T
in the middle and an offset from UTC. Joda-Time has a built-in formatter for parsing/generating in ISO 8601. So rather than define our own formatter, let's tweak the input String a bit, inserting the T
.
String inputRaw = "2014-07-04 04:05:10"; // UTC
String input = inputRaw.replace( " ", "T" ); // Convert to standard ISO 8601 format.
Then we will tell the DateTime constructor to parse the String as being in UTC (zero offset). Unlike java.util.Date, a DateTime knows its own assigned time zone.
DateTime dateTimeUtc = new DateTime( input, DateTimeZone.UTC );
Easy to adjust to Toronto time, for fun or for debugging/sanity-check.
DateTime dateTimeToronto = dateTimeUtc.withZone( DateTimeZone.forID( "America/Toronto" ) );
Like java.util.Date, Joda-Time internally tracks time as a number of milliseconds since the Unix epoch (beginning of 1970) in UTC. That means using a 64-bit long
rather than the usual 32-bit int
. (By the way, java.time tracks nanoseconds.) So if we need seconds-since-unix-epoch, divide by one thousand.
long millisecondsSinceEpoch = dateTimeUtc.getMillis(); // Use a "long", not "int".
long secondsSinceEpoch = ( millisecondsSinceEpoch / 1000L );
Dump to console.
System.out.println( "input: " + input );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "dateTimeToronto: " + dateTimeToronto );
System.out.println( "millisecondsSinceEpoch: " + millisecondsSinceEpoch );
System.out.println( "secondsSinceEpoch: " + secondsSinceEpoch );
When run.
input: 2014-07-04T04:05:10
dateTimeUtc: 2014-07-04T04:05:10.000Z
dateTimeToronto: 2014-07-04T00:05:10.000-04:00
millisecondsSinceEpoch: 1404446710000
secondsSinceEpoch: 1404446710