parse more iso formats

This commit is contained in:
Milos Kozak 2017-12-03 17:34:29 +01:00
parent 8a7f0f22d9
commit 0101bc0a81
2 changed files with 33 additions and 13 deletions

View file

@ -29,6 +29,7 @@ public class DateUtil {
*/
private static String FORMAT_DATE_ISO = "yyyy-MM-dd'T'HH:mm:ssZ";
private static String FORMAT_DATE_ISO_MSEC = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
private static String FORMAT_DATE_ISO_MSEC_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
/**
* Takes in an ISO date string of the following format:
@ -42,15 +43,31 @@ public class DateUtil {
throws Exception {
SimpleDateFormat f = new SimpleDateFormat(FORMAT_DATE_ISO, Locale.getDefault());
Date date;
f.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
date = f.parse(isoDateString);
return date;
} catch (ParseException e) {
}
f = new SimpleDateFormat(FORMAT_DATE_ISO_MSEC, Locale.getDefault());
f.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
date = f.parse(isoDateString);
}
return date;
} catch (ParseException e) {
}
f = new SimpleDateFormat(FORMAT_DATE_ISO_MSEC_UTC, Locale.getDefault());
f.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
date = f.parse(isoDateString);
return date;
} catch (ParseException e) {
}
throw new ParseException("Unparseable date: " + isoDateString, 0);
}
/**
@ -72,6 +89,7 @@ public class DateUtil {
public static String toISOString(Date date) {
return toISOString(date, FORMAT_DATE_ISO, TimeZone.getTimeZone("UTC"));
}
public static String toISOString(long date) {
return toISOString(new Date(date), FORMAT_DATE_ISO, TimeZone.getTimeZone("UTC"));
}
@ -125,6 +143,7 @@ public class DateUtil {
public static String dateAndTimeString(Date date) {
return dateString(date) + " " + timeString(date);
}
public static String dateAndTimeString(long mills) {
return dateString(mills) + " " + timeString(mills);
}
@ -146,19 +165,19 @@ public class DateUtil {
}
public static String timeFrameString(long timeInMillis){
long remainingTimeMinutes = timeInMillis/(1000*60);
long remainingTimeHours = remainingTimeMinutes/60;
remainingTimeMinutes = remainingTimeMinutes%60;
return "(" + ((remainingTimeHours >0)?(remainingTimeHours + "h "):"") + remainingTimeMinutes + "')";
public static String timeFrameString(long timeInMillis) {
long remainingTimeMinutes = timeInMillis / (1000 * 60);
long remainingTimeHours = remainingTimeMinutes / 60;
remainingTimeMinutes = remainingTimeMinutes % 60;
return "(" + ((remainingTimeHours > 0) ? (remainingTimeHours + "h ") : "") + remainingTimeMinutes + "')";
}
public static String sinceString(long timestamp){
return timeFrameString(System.currentTimeMillis()-timestamp);
public static String sinceString(long timestamp) {
return timeFrameString(System.currentTimeMillis() - timestamp);
}
public static String untilString(long timestamp){
return timeFrameString(timestamp- System.currentTimeMillis());
public static String untilString(long timestamp) {
return timeFrameString(timestamp - System.currentTimeMillis());
}

View file

@ -19,6 +19,7 @@ public class DateUtilTest {
public void fromISODateStringTest() throws Exception {
assertEquals( 1511124634417L, DateUtil.fromISODateString("2017-11-19T22:50:34.417+0200").getTime());
assertEquals( 1511124634000L, DateUtil.fromISODateString("2017-11-19T22:50:34+0200").getTime());
assertEquals( 1512317365000L, DateUtil.fromISODateString("2017-12-03T16:09:25.000Z").getTime());
}