2016-06-05 01:40:35 +02:00
|
|
|
package info.nightscout.utils;
|
|
|
|
|
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.TimeZone;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Class DateUtil. A simple wrapper around SimpleDateFormat to ease the handling of iso date string <-> date obj
|
|
|
|
* with TZ
|
|
|
|
*/
|
2016-07-16 22:37:10 +02:00
|
|
|
public class DateUtil {
|
2016-06-05 01:40:35 +02:00
|
|
|
|
2016-07-16 22:37:10 +02:00
|
|
|
/**
|
|
|
|
* The date format in iso.
|
|
|
|
*/
|
|
|
|
public static String FORMAT_DATE_ISO = "yyyy-MM-dd'T'HH:mm:ss'Z'";
|
2016-06-05 01:40:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes in an ISO date string of the following format:
|
|
|
|
* yyyy-mm-ddThh:mm:ss.ms+HoMo
|
|
|
|
*
|
|
|
|
* @param isoDateString the iso date string
|
|
|
|
* @return the date
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
|
|
|
public static Date fromISODateString(String isoDateString)
|
2016-07-16 22:37:10 +02:00
|
|
|
throws Exception {
|
|
|
|
SimpleDateFormat f = new SimpleDateFormat(FORMAT_DATE_ISO);
|
|
|
|
f.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
|
Date date = f.parse(isoDateString);
|
|
|
|
return date;
|
2016-06-05 01:40:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render date
|
|
|
|
*
|
2016-07-16 22:37:10 +02:00
|
|
|
* @param date the date obj
|
2016-06-05 01:40:35 +02:00
|
|
|
* @param format - if not specified, will use FORMAT_DATE_ISO
|
2016-07-16 22:37:10 +02:00
|
|
|
* @param tz - tz to set to, if not specified uses local timezone
|
2016-06-05 01:40:35 +02:00
|
|
|
* @return the iso-formatted date string
|
|
|
|
*/
|
2016-07-16 22:37:10 +02:00
|
|
|
public static String toISOString(Date date, String format, TimeZone tz) {
|
|
|
|
if (format == null) format = FORMAT_DATE_ISO;
|
|
|
|
if (tz == null) tz = TimeZone.getDefault();
|
2016-06-05 01:40:35 +02:00
|
|
|
DateFormat f = new SimpleDateFormat(format);
|
|
|
|
f.setTimeZone(tz);
|
|
|
|
return f.format(date);
|
|
|
|
}
|
|
|
|
|
2016-07-16 22:37:10 +02:00
|
|
|
public static String toISOString(Date date) {
|
|
|
|
return toISOString(date, FORMAT_DATE_ISO, TimeZone.getTimeZone("UTC"));
|
|
|
|
}
|
2016-07-21 00:18:45 +02:00
|
|
|
public static String toISOString(long date) {
|
|
|
|
return toISOString(new Date(date), FORMAT_DATE_ISO, TimeZone.getTimeZone("UTC"));
|
|
|
|
}
|
2016-06-05 01:40:35 +02:00
|
|
|
}
|