com.ionic.sdk.core.date.DateTime8601 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ionic-sdk Show documentation
Show all versions of ionic-sdk Show documentation
The Ionic Java SDK provides an easy-to-use interface to the Ionic Platform.
package com.ionic.sdk.core.date;
import com.ionic.sdk.error.IonicException;
import com.ionic.sdk.error.SdkError;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* Utility methods for converting a Date
object to / from an ISO 8601 string.
*/
public final class DateTime8601 {
/**
* Parse a ISO 8601 date string as a date.
*
* @param date the formatted string input
* @return the corresponding date
* @throws IonicException on invalid input
*/
public static Date fromString(final String date) throws IonicException {
return new DateTime8601().fromStringInner(date);
}
/**
* Encode a date as an ISO 8601 date string.
*
* @param date the date to represent
* @return the formatted string
*/
public static String toString(final Date date) {
return new DateTime8601().toStringInner(date);
}
/**
* The formatter to use when converting Date
objects to / from String
objects.
*/
private final DateFormat dateFormat;
/**
* Constructor.
*/
private DateTime8601() {
this.dateFormat = new SimpleDateFormat(ISO_8601_MILLI_Z, Locale.getDefault());
dateFormat.setTimeZone(TZ_GMT);
}
/**
* Parse a ISO 8601 date string as a date.
*
* @param date the formatted string input
* @return the corresponding date
* @throws IonicException on invalid input
*/
private Date fromStringInner(final String date) throws IonicException {
try {
return dateFormat.parse(date);
} catch (ParseException e) {
throw new IonicException(SdkError.ISAGENT_ERROR, e);
}
}
/**
* Encode a date as an ISO 8601 date string.
*
* @param date the date to represent
* @return the formatted string
*/
private String toStringInner(final Date date) {
return dateFormat.format(date);
}
/**
* The format string used to represent a GMT date as a string.
*/
private static final String ISO_8601_MILLI_Z = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
/**
* The format string used to represent a GMT date as a string.
*/
private static final String TZ_ID_GMT = "GMT";
/**
* The timezone ID corresponding to GMT.
*/
private static final TimeZone TZ_GMT = TimeZone.getTimeZone(TZ_ID_GMT);
}