2016-06-05 01:40:35 +02:00
|
|
|
package info.nightscout.utils;
|
|
|
|
|
2017-01-03 19:06:35 +01:00
|
|
|
import android.text.format.DateUtils;
|
|
|
|
|
2016-06-05 01:40:35 +02:00
|
|
|
import java.text.DateFormat;
|
2017-07-02 12:49:25 +02:00
|
|
|
import java.text.ParseException;
|
2016-06-05 01:40:35 +02:00
|
|
|
import java.text.SimpleDateFormat;
|
2016-10-20 23:50:31 +02:00
|
|
|
import java.util.Calendar;
|
2016-06-05 01:40:35 +02:00
|
|
|
import java.util.Date;
|
2016-10-20 23:50:31 +02:00
|
|
|
import java.util.GregorianCalendar;
|
2016-06-05 01:40:35 +02:00
|
|
|
import java.util.TimeZone;
|
2016-10-20 23:50:31 +02:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2016-06-05 01:40:35 +02:00
|
|
|
|
2017-01-03 19:06:35 +01:00
|
|
|
import info.nightscout.androidaps.MainApp;
|
2017-06-26 12:44:03 +02:00
|
|
|
import info.nightscout.androidaps.R;
|
2017-01-03 19:06:35 +01:00
|
|
|
|
2016-06-05 01:40:35 +02:00
|
|
|
/**
|
|
|
|
* 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'";
|
2017-07-02 12:49:25 +02:00
|
|
|
public static String FORMAT_DATE_ISO_MSEC = "yyyy-MM-dd'T'HH:mm:ss.SSS'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);
|
2017-07-02 12:49:25 +02:00
|
|
|
Date date;
|
2016-07-16 22:37:10 +02:00
|
|
|
f.setTimeZone(TimeZone.getTimeZone("UTC"));
|
2017-07-02 12:49:25 +02:00
|
|
|
try {
|
|
|
|
date = f.parse(isoDateString);
|
|
|
|
} catch (ParseException e) {
|
|
|
|
f = new SimpleDateFormat(FORMAT_DATE_ISO_MSEC);
|
|
|
|
f.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
|
date = f.parse(isoDateString);
|
|
|
|
}
|
2016-07-16 22:37:10 +02:00
|
|
|
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-10-20 23:50:31 +02:00
|
|
|
|
|
|
|
public static Date toDate(Integer seconds) {
|
|
|
|
Calendar calendar = new GregorianCalendar();
|
|
|
|
calendar.set(Calendar.HOUR_OF_DAY, seconds / 60 / 60);
|
|
|
|
String a = calendar.getTime().toString();
|
|
|
|
calendar.set(Calendar.MINUTE, (seconds / 60) % 60);
|
|
|
|
String b = calendar.getTime().toString();
|
|
|
|
calendar.set(Calendar.SECOND, 0);
|
|
|
|
String c = calendar.getTime().toString();
|
|
|
|
return calendar.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int toSeconds(String hh_colon_mm) {
|
2017-09-17 01:30:07 +02:00
|
|
|
Pattern p = Pattern.compile("(\\d+):(\\d+)( a.m.| p.m.| AM | PM)");
|
2016-10-20 23:50:31 +02:00
|
|
|
Matcher m = p.matcher(hh_colon_mm);
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
if (m.find()) {
|
|
|
|
retval = SafeParse.stringToInt(m.group(1)) * 60 * 60 + SafeParse.stringToInt(m.group(2)) * 60;
|
2017-09-18 20:20:28 +02:00
|
|
|
if (m.group(3).equals(" a.m.") || m.group(3).equals(" AM") && m.group(1).equals("12"))
|
2017-08-16 19:33:41 +02:00
|
|
|
retval -= 12 * 60 * 60;
|
2017-09-18 20:20:28 +02:00
|
|
|
if (m.group(3).equals(" p.m.") || m.group(3).equals(" PM") && !(m.group(1).equals("12")))
|
2017-08-16 19:33:41 +02:00
|
|
|
retval += 12 * 60 * 60;
|
2016-10-20 23:50:31 +02:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:06:35 +01:00
|
|
|
public static String dateString(Date date) {
|
2017-01-07 23:26:28 +01:00
|
|
|
//return DateUtils.formatDateTime(MainApp.instance(), date.getTime(), DateUtils.FORMAT_SHOW_DATE); this provide month name not number
|
|
|
|
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
|
|
|
|
return df.format(date);
|
2017-01-03 19:06:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String dateString(long mills) {
|
2017-01-07 23:26:28 +01:00
|
|
|
//return DateUtils.formatDateTime(MainApp.instance(), mills, DateUtils.FORMAT_SHOW_DATE); this provide month name not number
|
|
|
|
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
|
|
|
|
return df.format(mills);
|
2017-01-03 19:06:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String timeString(Date date) {
|
|
|
|
return DateUtils.formatDateTime(MainApp.instance(), date.getTime(), DateUtils.FORMAT_SHOW_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String timeString(long mills) {
|
|
|
|
return DateUtils.formatDateTime(MainApp.instance(), mills, DateUtils.FORMAT_SHOW_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String dateAndTimeString(Date date) {
|
|
|
|
return dateString(date) + " " + timeString(date);
|
|
|
|
}
|
|
|
|
public static String dateAndTimeString(long mills) {
|
|
|
|
return dateString(mills) + " " + timeString(mills);
|
|
|
|
}
|
2017-06-26 12:44:03 +02:00
|
|
|
|
|
|
|
public static String minAgo(long time) {
|
|
|
|
int mins = (int) ((System.currentTimeMillis() - time) / 1000 / 60);
|
|
|
|
return String.format(MainApp.sResources.getString(R.string.minago), mins);
|
|
|
|
}
|
|
|
|
|
2016-06-05 01:40:35 +02:00
|
|
|
}
|