Skip to content

Instantly share code, notes, and snippets.

@Odame
Forked from kristopherjohnson/TimestampUtils.java
Created January 28, 2017 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Odame/3c6d5e8ca68eecfceaa06e82698ea3be to your computer and use it in GitHub Desktop.
Save Odame/3c6d5e8ca68eecfceaa06e82698ea3be to your computer and use it in GitHub Desktop.
Methods for generating ISO 8601 timestamps in Java/Android
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);
}
/**
* 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() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment