DateUtils { public static String getDurationISO8601(long timeInMSec) { double totalTime = 0.0; int year = (int) Math.floor((timeInMSec - totalTime) / ONE_YEAR_IN_MILLI); totalTime += year * ONE_YEAR_IN_MILLI; int month = (int) Math.floor((timeInMSec - totalTime) / ONE_MONTH_IN_MILLI); totalTime += month * ONE_MONTH_IN_MILLI; int day = (int) Math.floor((timeInMSec - totalTime) / ONE_DAY_IN_MILLI); totalTime += day * ONE_DAY_IN_MILLI; int hour = (int) Math.floor((timeInMSec - totalTime) / ONE_HOUR_IN_MILLI); totalTime += hour * ONE_HOUR_IN_MILLI; int min = (int) Math.floor((timeInMSec - totalTime) / ONE_MINUTE_IN_MILLI); totalTime += min * ONE_MINUTE_IN_MILLI; int sec = (int) Math.floor((timeInMSec - totalTime) / ONE_SECOND_IN_MILLI); totalTime += sec * ONE_SECOND_IN_MILLI; int milli = (int) Math.floor((timeInMSec - totalTime)); StringBuilder sb = new StringBuilder(); if (milli > 0) { sb.insert(0, "." + String.format("%03d", milli)); } if (sec > 0) { sb.insert(0, sec + "S"); } if (min > 0) { sb.insert(0, min + "M"); } if (hour > 0) { sb.insert(0, hour + "H"); } if (sb.length() > 0) { sb.insert(0, "T"); } if (day > 0) { sb.insert(0, day + "D"); } if (month > 0) { sb.insert(0, month + "M"); } if (year > 0) { sb.insert(0, year + "Y"); } sb.insert(0, "P"); if (sb.length() == 1) { sb.append("0D"); } return sb.toString(); } private  DateUtils(); static XMLGregorianCalendar dateToXMLGregorianCalendar(Date date); static String getDurationMinSecMsec(long timeInMSec); static String getDurationISO8601(long timeInMSec); static String getDateAsGMTNoZeroTimeString(Date date); static String getDateAsGMTString(Date date); static String getDateAsIsoString(double value, String unitsString); static double getDate(Date date, String unitsString); static double getDate(Date date, DateUnit dateUnit); static Date getDate(double value, String unitsString); static DateUnit getDateUnit(String unitsString); static Date getDate(double value, DateUnit dateUnit); static String getDateAsGMTString(double value, String unitsString); static String getDateAsGMTString(double value, DateUnit dateUnit); static String getDateAsGMTNoZeroTimeString(double value, String unitsString); static Date parseDate(String dateStr, String dateFormat); static Date parseDate(String source, int setTimeTo0ForBeginOfDays1ForEndOfDayNegativeForNow); static boolean contains(ReadableInterval interval, long millisInstant); static boolean contains(ReadableInterval interval, ReadableInterval intervalCompareTo); static boolean containsNow(ReadableInterval interval); static boolean intersects(ReadableInterval interval1, ReadableInterval interval2); static Interval intersect(ReadableInterval interval1, ReadableInterval interval2); static DateTime dateTimeToUTC(DateTime dateTime); static DateTime dateTimeToUTC(Date date); static String getDateTimeAsUTCString(DateTime dateTime); static String getDateTimeAsUTCString(Date date); static String getDateTimeAsUTCString(DateTime dateTime, String pattern); static String getDateTimeAsUTCString(Date date, String pattern); static String dateToString(DateTime dt); static String dateTimeToString(DateTime dt, final String pattern); static String dateTimeToString(DateTime dt); static Date stringToDate(String s); static Period stringToPeriod(String s); static String getDate(String st); static Date parseDate(String lastupdate); static DateTime parseDateTime(String lastupdate); static final Map<String, FastDateFormat> DATETIME_FORMATTERS; static final Map<String, DateTimeFormatter> JODA_DATETIME_FORMATTERS; static final String PERIOD_PATTERN_ISO_STANDARD; static final String PERIOD_PATTERN_ISO_ALTERNATE; static final String PERIOD_PATTERN_ISO_ALTERNATE_WITH_WEEKS; static final String PERIOD_PATTERN_ISO_ALTERNATE_EXTENDED; static final String PERIOD_PATTERN_ISO_ALTERNATE_EXTENDED_WITH_WEEKS; static final Map<String, PeriodFormatter> PERIOD_FORMATTERS; static final long ONE_SECOND_IN_MILLI; static final long ONE_MINUTE_IN_MILLI; static final long ONE_HOUR_IN_MILLI; static final long ONE_DAY_IN_MILLI; static final long ONE_MONTH_IN_MILLI; static final long ONE_YEAR_IN_MILLI; static final String DATE_PATTERN; static final String DATETIME_PATTERN; static final String DATETIME_MILLIS_EU_PATTERN; static final String DATETIME_MILLIS_PATTERN; static final String DATETIME_T_PATTERN; static final String DATETIME_T_MILLIS_PATTERN; static final String DATETIME_T_MILLIS_EU_PATTERN; static final TimeZone GMT_TIMEZONE; static final TimeZone UTC_TIMEZONE; static final FastDateFormat DATETIME_TO_STRING_DEFAULT; static final FastDateFormat DATE_TO_STRING_DEFAULT; static final String[] DATE_FORMATS; }
@Test public void testGetDurationISO8601() { assertTrue(DateUtils.getDurationISO8601(1).equalsIgnoreCase("PT.001")); assertTrue(DateUtils.getDurationISO8601(1000).equalsIgnoreCase("PT1S")); assertTrue(DateUtils.getDurationISO8601(60000).equalsIgnoreCase("PT1M")); assertTrue(DateUtils.getDurationISO8601(3600000).equalsIgnoreCase("PT1H")); assertTrue(DateUtils.getDurationISO8601(2 * DateUtils.ONE_SECOND_IN_MILLI + 44).equalsIgnoreCase("PT2S.044")); assertTrue(DateUtils.getDurationISO8601(2 * DateUtils.ONE_MINUTE_IN_MILLI + 3 * DateUtils.ONE_SECOND_IN_MILLI + 44) .equalsIgnoreCase("PT2M3S.044")); assertTrue(DateUtils .getDurationISO8601(2 * DateUtils.ONE_HOUR_IN_MILLI + (2 * DateUtils.ONE_MINUTE_IN_MILLI + 3 * DateUtils.ONE_SECOND_IN_MILLI + 44)) .equalsIgnoreCase("PT2H2M3S.044")); assertTrue(DateUtils .getDurationISO8601(6 * DateUtils.ONE_DAY_IN_MILLI + (2 * DateUtils.ONE_HOUR_IN_MILLI + 2 * DateUtils.ONE_MINUTE_IN_MILLI + 3 * DateUtils.ONE_SECOND_IN_MILLI + 44)) .equalsIgnoreCase("P6DT2H2M3S.044")); assertTrue(DateUtils .getDurationISO8601(DateUtils.ONE_MONTH_IN_MILLI + 6 * DateUtils.ONE_DAY_IN_MILLI + (2 * DateUtils.ONE_HOUR_IN_MILLI + 2 * DateUtils.ONE_MINUTE_IN_MILLI + 3 * DateUtils.ONE_SECOND_IN_MILLI + 44)) .equalsIgnoreCase("P1M6DT2H2M3S.044")); assertTrue(DateUtils .getDurationISO8601(DateUtils.ONE_YEAR_IN_MILLI + (2 * DateUtils.ONE_HOUR_IN_MILLI + 2 * DateUtils.ONE_MINUTE_IN_MILLI + 3 * DateUtils.ONE_SECOND_IN_MILLI + 44)) .equalsIgnoreCase("P1YT2H2M3S.044")); assertTrue(DateUtils .getDurationISO8601(DateUtils.ONE_YEAR_IN_MILLI + 3 * DateUtils.ONE_MONTH_IN_MILLI + (2 * DateUtils.ONE_HOUR_IN_MILLI + 2 * DateUtils.ONE_MINUTE_IN_MILLI + 3 * DateUtils.ONE_SECOND_IN_MILLI + 44)) .equalsIgnoreCase("P1Y3MT2H2M3S.044")); assertTrue(DateUtils .getDurationISO8601(DateUtils.ONE_YEAR_IN_MILLI + 3 * DateUtils.ONE_MONTH_IN_MILLI + 5 * DateUtils.ONE_DAY_IN_MILLI + (2 * DateUtils.ONE_HOUR_IN_MILLI + 2 * DateUtils.ONE_MINUTE_IN_MILLI + 3 * DateUtils.ONE_SECOND_IN_MILLI + 44)) .equalsIgnoreCase("P1Y3M5DT2H2M3S.044")); }