/** * format time * * @param date date * * @return formatted time */ public static String getTimeAsISO8601(Date date) { return getTimeAsISO8601(date.getTime()); }
/** * utility to convert a given number of days to milliseconds * * @param days days * * @return milliseconds */ public static long daysToMillis(double days) { return hoursToMillis(days * 24); }
/** * utility to convert a given number of hours to milliseconds * * @param hour hours * * @return milliseconds */ public static long hoursToMillis(double hour) { return minutesToMillis(hour * 60); }
throws java.text.ParseException { Date fromDttm = DateUtil.parseRelative(dflt, fromDate, -1); Date toDttm = DateUtil.parseRelative(dflt, toDate, +1); fromDttm = DateUtil.parse(fromDate); toDttm = DateUtil.parse(toDate); "Cannot do relative From Date when To Date is not set"); fromDttm = DateUtil.getRelativeDate(toDttm, fromDate); "Cannot do relative From Date when To Date is not set"); toDttm = DateUtil.getRelativeDate(fromDttm, toDate);
dttm = cal.getTime(); } else if (s.equals("yesterday")) { dttm = new Date(cal.getTime().getTime() - daysToMillis(1)); } else if (s.equals("tomorrow")) { dttm = new Date(cal.getTime().getTime() + daysToMillis(1)); } else if (s.startsWith("last") || s.startsWith("next")) { List toks = StringUtil.split(s, " ", true, true); return roundByDay(dttm, roundDays);
for (int i = 0; i < 20; i++) { dates.add(new DatedObject(new Date(now + DateUtil.minutesToMillis(20) - i * 10 * 60 * 1000))); dateSelection.setStartOffset(DateUtil.hoursToMillis(-2)); dateSelection.setRoundTo(DateUtil.hoursToMillis(12)); dateSelection.setInterval(DateUtil.minutesToMillis(15)); dateSelection.setIntervalRange(DateUtil.minutesToMillis(6));
/** * This finds the SDF to use for the given date string * * @param dateString example date * * @return formatter to use */ public static SimpleDateFormat findFormatter(String dateString) { if ((lastSdf != null) && (lengthLastDate == dateString.length())) { try { synchronized (lastSdf) { lastSdf.parse(dateString); return lastSdf; } } catch (ParseException pe) {} } SimpleDateFormat[] sdfs = getFormatters(); for (int i = 0; i < sdfs.length; i++) { try { synchronized (sdfs[i]) { Date dttm = sdfs[i].parse(dateString); lengthLastDate = dateString.length(); lastSdf = sdfs[i]; return sdfs[i]; } } catch (ParseException pe) {} } throw new IllegalArgumentException("Could not find date format for:" + dateString); }
/** * main * * @param args args * * @throws Exception On badness */ public static void main(String[] args) throws Exception { debug = true; if (args.length == 0) { args = new String[] { "2000-10-01T10:53", "2000", "2000-10", "2000-10-01", "2000/10/01" }; } for (String arg : args) { System.err.println(arg + " = " + parse(arg)); } }
/** * Get a new date relative to the given date. * * @param from base date * @param relativeTimeString Relative time string, e.g., -1 hour * * @return new date */ public static Date getRelativeDate(Date from, String relativeTimeString) { Date result = new Date(from.getTime() + parseRelativeTimeString(relativeTimeString)); return result; }
/** * parse the array of date strings and returns the date as seconds * * @param s array of date strings * * @return array of seconds * * @throws java.text.ParseException On badness */ public static double[] toSeconds(String[] s) throws java.text.ParseException { double[] d = new double[s.length]; if (s.length == 0) { return d; } SimpleDateFormat sdf = findFormatter(s[0]); double lastTime = 0; for (int i = 0; i < s.length; i++) { d[i] = sdf.parse(s[i]).getTime() / 1000.0; if (d[i] < lastTime) { System.out.println("****" + s[i]); } // else System.out.println (s[i]); lastTime = d[i]; // System.out.println(d[i]); } return d; }
SimpleDateFormat[] sdfs = getFormatters();
/** * format current time * * @return current time formatted */ public static String getCurrentSystemTimeAsISO8601() { return getTimeAsISO8601(System.currentTimeMillis()); }