List of usage examples for java.util Calendar setTimeInMillis
public void setTimeInMillis(long millis)
From source file:adalid.commons.util.TimeUtils.java
public static synchronized Date getDate(java.util.Date date) { if (date == null) { return currentDate(); }/* www .j av a 2 s. c om*/ Calendar c = Calendar.getInstance(); c.setTimeInMillis(date.getTime()); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return new Date(c.getTimeInMillis()); }
From source file:adalid.commons.util.TimeUtils.java
public static Calendar newDateCalendar(java.util.Date date) { if (date == null) { return null; }/* ww w .ja va 2 s .c o m*/ Calendar c = Calendar.getInstance(); c.setTimeInMillis(date.getTime()); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c; }
From source file:adalid.commons.util.TimeUtils.java
public static String jdbcTemporalString(Object object) { java.util.Date date = object instanceof java.util.Date ? (java.util.Date) object : null; if (date == null) { return null; }/*from ww w . ja va 2 s .c o m*/ Calendar c = Calendar.getInstance(); c.setTimeInMillis(date.getTime()); int dd = c.get(Calendar.YEAR); int MM = c.get(Calendar.MONTH); int yy = c.get(Calendar.DAY_OF_MONTH); int hh = c.get(Calendar.HOUR_OF_DAY); int mm = c.get(Calendar.MINUTE); int ss = c.get(Calendar.SECOND); int ms = c.get(Calendar.MILLISECOND); boolean dateless = dd == 1 && MM == Calendar.JANUARY && yy == 1970; boolean timeless = hh + mm + ss + ms == 0; return timeless ? jdbcDateString(date) : dateless ? jdbcTimeString(date) : jdbcTimestampString(date); }
From source file:adalid.commons.util.TimeUtils.java
public static String defaultTemporalString(Object object) { java.util.Date date = object instanceof java.util.Date ? (java.util.Date) object : null; if (date == null) { return null; }//from w w w . java 2s . co m Calendar c = Calendar.getInstance(); c.setTimeInMillis(date.getTime()); int dd = c.get(Calendar.YEAR); int MM = c.get(Calendar.MONTH); int yy = c.get(Calendar.DAY_OF_MONTH); int hh = c.get(Calendar.HOUR_OF_DAY); int mm = c.get(Calendar.MINUTE); int ss = c.get(Calendar.SECOND); int ms = c.get(Calendar.MILLISECOND); boolean dateless = dd == 1 && MM == Calendar.JANUARY && yy == 1970; boolean timeless = hh + mm + ss + ms == 0; return timeless ? defaultDateString(date) : dateless ? defaultTimeString(date) : defaultTimestampString(date); }
From source file:Dates.java
/** * Date Arithmetic function (date2 - date1). subtract a date from another * date, return the difference as the required fields. E.g. if specified * Calendar.Date, the smaller range of fields is ignored and this method * return the difference of days./*from w ww. j a va 2 s . c o m*/ * * @param date2 * The date2. * @param tz * The time zone. * @param field * The time field; e.g., Calendar.DATE, Calendar.YEAR, it's default * value is Calendar.DATE * @param date1 * The date1. */ public static final long subtract(Date date2, TimeZone tz, int field, Date date1) { if (tz == null) tz = TimeZones.getCurrent(); boolean negative = false; if (date1.after(date2)) { negative = true; final Date d = date1; date1 = date2; date2 = d; } final Calendar cal1 = Calendar.getInstance(tz); cal1.setTimeInMillis(date1.getTime());// don't call cal.setTime(Date) which // will reset the TimeZone. final Calendar cal2 = Calendar.getInstance(tz); cal2.setTimeInMillis(date2.getTime());// don't call cal.setTime(Date) which // will reset the TimeZone. int year1 = cal1.get(Calendar.YEAR); int year2 = cal2.get(Calendar.YEAR); switch (field) { case Calendar.YEAR: { return negative ? (year1 - year2) : (year2 - year1); } case Calendar.MONTH: { int month1 = cal1.get(Calendar.MONTH); int month2 = cal2.get(Calendar.MONTH); int months = 12 * (year2 - year1) + month2 - month1; return negative ? -months : months; } case Calendar.HOUR: { long time1 = date1.getTime(); long time2 = date2.getTime(); long min1 = (time1 < 0 ? (time1 - (1000 * 60 * 60 - 1)) : time1) / (1000 * 60 * 60); long min2 = (time2 < 0 ? (time2 - (1000 * 60 * 60 - 1)) : time2) / (1000 * 60 * 60); return negative ? (min1 - min2) : (min2 - min1); } case Calendar.MINUTE: { long time1 = date1.getTime(); long time2 = date2.getTime(); long min1 = (time1 < 0 ? (time1 - (1000 * 60 - 1)) : time1) / (1000 * 60); long min2 = (time2 < 0 ? (time2 - (1000 * 60 - 1)) : time2) / (1000 * 60); return negative ? (min1 - min2) : (min2 - min1); } case Calendar.SECOND: { long time1 = date1.getTime(); long time2 = date2.getTime(); long sec1 = (time1 < 0 ? (time1 - (1000 - 1)) : time1) / 1000; long sec2 = (time2 < 0 ? (time2 - (1000 - 1)) : time2) / 1000; return negative ? (sec1 - sec2) : (sec2 - sec1); } case Calendar.MILLISECOND: { return negative ? (date1.getTime() - date2.getTime()) : (date2.getTime() - date1.getTime()); } case Calendar.DATE: default: /* default, like -1 */ { int day1 = cal1.get(Calendar.DAY_OF_YEAR); int day2 = cal2.get(Calendar.DAY_OF_YEAR); int maxDay1 = year1 == year2 ? 0 : cal1.getActualMaximum(Calendar.DAY_OF_YEAR); int days = maxDay1 - day1 + day2; final Calendar cal = Calendar.getInstance(tz); for (int year = year1 + 1; year < year2; year++) { cal.set(Calendar.YEAR, year); days += cal.getActualMaximum(Calendar.DAY_OF_YEAR); } return negative ? -days : days; } } }
From source file:adalid.commons.util.TimeUtils.java
private static Timestamp merge(java.util.Date date, java.util.Date time) { Calendar d = Calendar.getInstance(); Calendar t = Calendar.getInstance(); d.setTimeInMillis(date.getTime()); t.setTimeInMillis(time.getTime());//from ww w.ja v a 2s . c o m d.set(Calendar.HOUR_OF_DAY, t.get(Calendar.HOUR_OF_DAY)); d.set(Calendar.MINUTE, t.get(Calendar.MINUTE)); d.set(Calendar.SECOND, t.get(Calendar.SECOND)); d.set(Calendar.MILLISECOND, t.get(Calendar.MILLISECOND)); return new Timestamp(d.getTimeInMillis()); }
From source file:com.espertech.esper.schedule.ScheduleComputeHelper.java
private static long compute(ScheduleSpec spec, long afterTimeInMillis) { while (true) { Calendar after; if (spec.getOptionalTimeZone() != null) { after = Calendar.getInstance(TimeZone.getTimeZone(spec.getOptionalTimeZone())); } else {/*from w w w .j av a2 s .c o m*/ after = Calendar.getInstance(); } after.setTimeInMillis(afterTimeInMillis); ScheduleCalendar result = new ScheduleCalendar(); result.setMilliseconds(after.get(Calendar.MILLISECOND)); SortedSet<Integer> minutesSet = spec.getUnitValues().get(ScheduleUnit.MINUTES); SortedSet<Integer> hoursSet = spec.getUnitValues().get(ScheduleUnit.HOURS); SortedSet<Integer> monthsSet = spec.getUnitValues().get(ScheduleUnit.MONTHS); SortedSet<Integer> secondsSet = null; boolean isSecondsSpecified = false; if (spec.getUnitValues().containsKey(ScheduleUnit.SECONDS)) { isSecondsSpecified = true; secondsSet = spec.getUnitValues().get(ScheduleUnit.SECONDS); } if (isSecondsSpecified) { result.setSecond(nextValue(secondsSet, after.get(Calendar.SECOND))); if (result.getSecond() == -1) { result.setSecond(nextValue(secondsSet, 0)); after.add(Calendar.MINUTE, 1); } } result.setMinute(nextValue(minutesSet, after.get(Calendar.MINUTE))); if (result.getMinute() != after.get(Calendar.MINUTE)) { result.setSecond(nextValue(secondsSet, 0)); } if (result.getMinute() == -1) { result.setMinute(nextValue(minutesSet, 0)); after.add(Calendar.HOUR_OF_DAY, 1); } result.setHour(nextValue(hoursSet, after.get(Calendar.HOUR_OF_DAY))); if (result.getHour() != after.get(Calendar.HOUR_OF_DAY)) { result.setSecond(nextValue(secondsSet, 0)); result.setMinute(nextValue(minutesSet, 0)); } if (result.getHour() == -1) { result.setHour(nextValue(hoursSet, 0)); after.add(Calendar.DAY_OF_MONTH, 1); } // This call may change second, minute and/or hour parameters // They may be reset to minimum values if the day rolled result.setDayOfMonth(determineDayOfMonth(spec, after, result)); boolean dayMatchRealDate = false; while (!dayMatchRealDate) { if (checkDayValidInMonth(result.getDayOfMonth(), after.get(Calendar.MONTH), after.get(Calendar.YEAR))) { dayMatchRealDate = true; } else { after.add(Calendar.MONTH, 1); } } int currentMonth = after.get(Calendar.MONTH) + 1; result.setMonth(nextValue(monthsSet, currentMonth)); if (result.getMonth() != currentMonth) { result.setSecond(nextValue(secondsSet, 0)); result.setMinute(nextValue(minutesSet, 0)); result.setHour(nextValue(hoursSet, 0)); result.setDayOfMonth(determineDayOfMonth(spec, after, result)); } if (result.getMonth() == -1) { result.setMonth(nextValue(monthsSet, 0)); after.add(Calendar.YEAR, 1); } // Perform a last valid date check, if failing, try to compute a new date based on this altered after date int year = after.get(Calendar.YEAR); if (!(checkDayValidInMonth(result.getDayOfMonth(), result.getMonth() - 1, year))) { afterTimeInMillis = after.getTimeInMillis(); continue; } return getTime(result, after.get(Calendar.YEAR), spec.getOptionalTimeZone()); } }
From source file:org.zoumbox.mh_dla_notifier.sp.PublicScriptsProxy.java
public static List<MhSpRequest> listLatestRequests(Context context, String trollId, int count) { List<MhSpRequest> result = new ArrayList<MhSpRequest>(); String query = String.format(SQL_LIST_REQUESTS, count); MhDlaSQLHelper helper = new MhDlaSQLHelper(context); SQLiteDatabase database = helper.getReadableDatabase(); Calendar calendar = Calendar.getInstance(); Cursor cursor = database.rawQuery(query, new String[] { trollId }); while (cursor.moveToNext()) { long startTimeMillis = cursor.getLong(0); long endTimeMillis = cursor.getLong(1); String scriptName = cursor.getString(2); String status = cursor.getString(3); calendar.setTimeInMillis(startTimeMillis); Date date = calendar.getTime(); PublicScript script = PublicScript.valueOf(scriptName); long duration = 0; if (endTimeMillis > 0) { duration = endTimeMillis - startTimeMillis; }//from w w w . j a va2 s. c om MhSpRequest request = new MhSpRequest(date, duration, script, status); result.add(request); } cursor.close(); database.close(); return result; }
From source file:org.zoumbox.mh_dla_notifier.sp.PublicScriptsProxy.java
public static List<MhSpRequest> listLatestRequestsSince(Context context, String trollId, int dayCount) { List<MhSpRequest> result = new ArrayList<MhSpRequest>(); Calendar instance = Calendar.getInstance(); instance.add(Calendar.HOUR_OF_DAY, dayCount * -24); Date sinceDate = instance.getTime(); MhDlaSQLHelper helper = new MhDlaSQLHelper(context); SQLiteDatabase database = helper.getReadableDatabase(); Calendar calendar = Calendar.getInstance(); Cursor cursor = database.rawQuery(SQL_LIST_REQUESTS_SINCE, new String[] { trollId, "" + sinceDate.getTime() }); while (cursor.moveToNext()) { long startTimeMillis = cursor.getLong(0); long endTimeMillis = cursor.getLong(1); String scriptName = cursor.getString(2); String status = cursor.getString(3); calendar.setTimeInMillis(startTimeMillis); Date date = calendar.getTime(); PublicScript script = PublicScript.valueOf(scriptName); long duration = 0; if (endTimeMillis > 0) { duration = endTimeMillis - startTimeMillis; }/*from w w w.j a v a 2s.c om*/ MhSpRequest request = new MhSpRequest(date, duration, script, status); result.add(request); } cursor.close(); database.close(); return result; }
From source file:ddf.metrics.reporting.internal.rrd4j.RrdMetricsRetriever.java
/** * Formats timestamp (in seconds since Unix epoch) into human-readable format of MMM DD YYYY * hh:mm:ss./*from w w w .j a v a 2 s .co m*/ * <p> * Example: Apr 10 2013 09:14:43 * * @param timestamp time in seconds since Unix epoch of Jan 1, 1970 12:00:00 * @return formatted date/time string of the form MMM DD YYYY hh:mm:ss */ static String getCalendarTime(long timestamp) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(TimeUnit.SECONDS.toMillis(timestamp)); String calTime = MONTHS[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.DATE) + " " + calendar.get(Calendar.YEAR) + " "; calTime += addLeadingZero(calendar.get(Calendar.HOUR_OF_DAY)) + ":"; calTime += addLeadingZero(calendar.get(Calendar.MINUTE)) + ":"; calTime += addLeadingZero(calendar.get(Calendar.SECOND)); return calTime; }