Skip to content

Instantly share code, notes, and snippets.

@kibotu
Created January 26, 2018 12:40
Show Gist options
  • Save kibotu/bddce838e811702934e15e4653e63f2b to your computer and use it in GitHub Desktop.
Save kibotu/bddce838e811702934e15e4653e63f2b to your computer and use it in GitHub Desktop.
Timestamp converter, RFC 822, RFC 850, ANSI C's Asctime(), iso8601
/**
* Utilities for converting to/from the HTTP 1. 0 timestamp formats.
* They are:
* <pre>
* <ul>
* <li>Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123</li>
* <li>Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036</li>
* <li>Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format</li>
* <li>2016-09-19T15:22:21+02:00 ; iso8601() format</li>
* </ul>
* </pre>
* HTTP 1.1 requires clients and servers to read these formats, though they
* should only write RFC 1123 format.
* <p>
* <p>Source: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html
* (via Google search "http timestamp format").
*/
public class TimestampConvert {
/**
* EEE, dd MMM yyyy HH:mm:ss zzz
*/
private static final SimpleDateFormat rfc1123Format;
/**
* EEEEEEEEE, dd-MMM-yy HH:mm:ss zzz
*/
private static final SimpleDateFormat rfc1036Format;
/**
* EEE MMM dd HH:mm:ss yyyy
*/
private static final SimpleDateFormat asctimeFormat;
/**
* yyyy-MM-dd'T'HH:mm:ssXXX
*/
private static final SimpleDateFormat iso8601Format;
static {
rfc1123Format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
rfc1036Format = new SimpleDateFormat("EEEEEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.ENGLISH);
rfc1036Format.setTimeZone(TimeZone.getTimeZone("GMT"));
asctimeFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.ENGLISH);
asctimeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
// iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss" + (isJUnitTest() ? "XXX" : "Z"), Locale.ENGLISH);
// asctimeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
}
/**
* Converts an ISO-8601-style timestamp to a Date.
*/
public static Date iso8601ToDate(String timestamp) throws ParseException {
return iso8601Format.parse(timestamp);
}
/**
* Converts a Date to an ISO-8601-style timestamp.
*/
public static String dateToIso8601(Date date) {
return iso8601Format.format(date);
}
/**
* Converts an RFC-1123-style timestamp to a Date.
*/
public static Date rfc1123ToDate(String timestamp) throws ParseException {
return rfc1123Format.parse(timestamp);
}
/**
* Converts a Date to an RFC-1123-style timestamp.
*/
public static String dateToRfc1123(Date date) {
return rfc1123Format.format(date);
}
/**
* Converts an RFC-1036-style timestamp to a Date.
*/
public static Date rfc1036ToDate(String timestamp) throws ParseException {
return rfc1036Format.parse(timestamp);
}
/**
* Converts a Date to an RFC-1036-style timestamp.
*/
public static String dateToRfc1036(Date date) {
return rfc1036Format.format(date);
}
/**
* Converts an asctime-style timestamp to a Date.
*/
public static Date asctimeToDate(String timestamp) throws ParseException {
return asctimeFormat.parse(timestamp);
}
/**
* Converts a Date to an asctime-style timestamp.
*/
public static String dateToAsctime(Date date) {
return asctimeFormat.format(date);
}
/**
* Converts an HTTP-style timestamp to a Date. The timestamp could be any of
* RFC 1123, RFC 1036, or C's asctime().
*
* @throws ParseException if none of the formats apply.
*/
public static Date httpToDate(String timestamp) throws ParseException {
Date date;
// Try the different formats in order of preference
try {
date = rfc1123ToDate(timestamp);
} catch (ParseException e) {
try {
date = rfc1036ToDate(timestamp);
} catch (ParseException e1) {
date = asctimeToDate(timestamp);
}
}
return date;
}
private static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss";
public static Date GetUTCDateTimeAsDate(Date date) {
//note: doesn't check for null
return StringDateToDate(GetUTCDateTimeAsString(date));
}
public static String GetUTCDateTimeAsString(Date date) {
final SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
return sdf.format(date);
}
public static Date StringDateToDate(String StrDate) {
Date dateToReturn = null;
SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
dateToReturn = dateFormat.parse(StrDate);
} catch (ParseException e) {
e.printStackTrace();
}
return dateToReturn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment