-
-
Save gkhays/2f259acb7ded87df1fbc to your computer and use it in GitHub Desktop.
Methods for generating ISO 8601 timestamps in Java/Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.kristopherjohnson.util.test; | |
import static org.junit.Assert.*; | |
import java.util.Date; | |
import net.kristopherjohnson.util.TimestampUtils; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class TestTimeStampUtils { | |
String[] loginTimes = { | |
"2015-04-30T15:48:34.000Z", | |
"2015-03-26T20:38:55.000Z", | |
"2015-03-26T20:37:42.000Z" }; | |
long[] expectedTimes = { 1430426914, 1427420335, 1427420262 }; | |
@Before | |
public void setUp() throws Exception { | |
} | |
@SuppressWarnings("deprecation") | |
@Test | |
public void testISO8609ForCurrent() { | |
Date now = new Date(); | |
String timeStamp = TimestampUtils.getISO8601StringForCurrentDate(); | |
assertTrue(timeStamp.startsWith(String.valueOf(now.getYear() + 1900))); | |
} | |
@Test | |
public void testISO8609ToUnixTime() { | |
int idx = 0; | |
long unixtime; | |
for (String timeStamp : loginTimes) { | |
unixtime = TimestampUtils.iso8601StringToUnixTime(timeStamp); | |
assertEquals(expectedTimes[idx], unixtime); | |
idx++; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.kristopherjohnson.util; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.Locale; | |
import java.util.TimeZone; | |
/** | |
* Methods for dealing with timestamps | |
*/ | |
public class TimestampUtils { | |
/** | |
* Return an ISO 8601 combined date and time string for current date/time | |
* | |
* @return String with format "yyyy-MM-dd'T'HH:mm:ss'Z'" | |
*/ | |
public static String getISO8601StringForCurrentDate() { | |
Date now = new Date(); | |
return getISO8601StringForDate(now); | |
} | |
/** | |
* ISO 8601/RFC 3339 represents timezone information a little bit | |
* differently than what is expected by {@link #SimpleDateFormat}. For | |
* example the Google Directory REST API returns timestamps in the form of: | |
* {@code 2015-04-30T15:48:34.000Z}. Convert these to a UNIX timestamp | |
* (number of seconds from the epoch). Where the Unix Epoch started January | |
* 1st, 1970 at UTC. | |
* | |
* @see <a href="http://www.unixtimestamp.com/">Epoch Unix Time Stamp | |
* Converter</a> | |
* | |
* @param timeStamp | |
* {@link #String} in the format of | |
* {@code 2015-04-30T15:48:34.000Z} | |
* @return the number of seconds from the epoch | |
*/ | |
public static long iso8601StringToUnixTime(String timeStamp) { | |
long unixTime = -1; | |
TimeZone tzGMT = TimeZone.getTimeZone("GMT"); | |
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); | |
format.setTimeZone(tzGMT); | |
try { | |
// Reminder: getTime returns the number of milliseconds from the | |
// Unix epoch. | |
unixTime = format.parse(timeStamp).getTime() / 1000; | |
} catch (ParseException e) { | |
// Return initialized value of -1; | |
} | |
return unixTime; | |
} | |
/** | |
* Return an ISO 8601 combined date and time string for specified date/time | |
* | |
* @param date | |
* Date | |
* @return String with format "yyyy-MM-dd'T'HH:mm:ss'Z'" | |
*/ | |
private static String getISO8601StringForDate(Date date) { | |
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); | |
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); | |
return dateFormat.format(date); | |
} | |
/** | |
* Private constructor: class cannot be instantiated | |
*/ | |
private TimestampUtils() { | |
} | |
} |
@karldusenberydlink Have you considered doing the conversion with java.sql.Timestamp
?
This turns out to be an interesting problem if it is not possible to convert to a java.sql.Timestamp
and insert into the database using JDBC. So if you are willing to use Java 8, one solution may be:
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String timeStamp = format.format(LocalDateTime.now(Clock.systemUTC()));
Which will yield:
2019-06-27T17:08:04.285Z
You may also continue to use the older date and time objects, but the fraction of a second portion will not be that accurate.
Date now = new Date();
TimeZone tzGMT = TimeZone.getTimeZone("GMT");
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format.setTimeZone(tzGMT);
String timeStamp = format.format(now);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This great, but I need to convert from epoch/unix time to ISO 8601/RFC 3339 in the form of: { 2015-04-30T15:48:34.000Z} for the database I am working with now.