List of usage examples for java.util Calendar getTimeInMillis
public long getTimeInMillis()
From source file:com.taobao.adfs.database.tdhsocket.client.util.ConvertUtil.java
public static Long getTimeFromString(String stringVal, @Nullable Calendar cal) throws SQLException { if (stringVal == null) { return null; }// ww w. j av a 2s . com String val = stringVal.trim(); if (val.length() == 0) { return null; } if (val.equals("0") || val.equals("0000-00-00") || val.equals("0000-00-00 00:00:00") || val.equals("00000000000000") || val.equals("0")) { Calendar calendar = null; if (cal != null) { calendar = Calendar.getInstance(cal.getTimeZone()); } else { calendar = Calendar.getInstance(); } calendar.set(Calendar.YEAR, 1); calendar.set(Calendar.MONTH, 0); calendar.set(Calendar.DAY_OF_MONTH, 1); return calendar.getTimeInMillis(); } DateFormat dateFormat = DateFormat.getDateTimeInstance(); if (cal != null) { TimeZone timeZone = cal.getTimeZone(); dateFormat.setTimeZone(timeZone); } try { return dateFormat.parse(val).getTime(); } catch (ParseException e) { throw new SQLException("Parse date failure:" + val); } }
From source file:com.twitter.elephanttwin.util.DateUtil.java
public static String getMysqlTimeInterval(Calendar startTime, Calendar endTime) { return String.format("[%s - %s]", MYSQL_TIMESTAMP_FORMATTER.print(startTime.getTimeInMillis()), MYSQL_TIMESTAMP_FORMATTER.print(endTime.getTimeInMillis())); }
From source file:Main.java
/** * @param period : today, this_week, this_month, this_year, none * @return a date filter string for making query. e.g. date >= 2013-1-1 for this_year * return null if period is none./*from w w w . j av a 2s . c o m*/ */ @SuppressWarnings("deprecation") public static String getDateFilterString(String period) { if (period == null) return null; Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; // 1 - 12 int date = calendar.get(Calendar.DATE); // 1 - 31 String dateFilterString = null; if (period.equalsIgnoreCase("today")) { dateFilterString = "date >= " + year + "-" + month + "-" + date; } else if (period.contains("week")) { int offset = calendar.get(Calendar.DAY_OF_WEEK) - calendar.getFirstDayOfWeek(); long timeInMillis = calendar.getTimeInMillis() - offset * 24 * 60 * 60 * 1000; Date firstDay = new Date(timeInMillis); dateFilterString = "date >= " + (firstDay.getYear() + 1900) + "-" + firstDay.getMonth() + "-" + firstDay.getDate(); } else if (period.contains("month")) { dateFilterString = "date >= " + year + "-" + month + "-1"; } else if (period.contains("year")) { dateFilterString = "date >= " + year + "-1-1"; } return dateFilterString; }
From source file:com.projity.script.object.TimeIntervals.java
public static TimeWindow generateWindow(long start, int scale, int sign) { int timeType, timeType2 = 0, number2; int timeIncrement = 1, timeIncrement2 = 1; switch (scale) { case TimeIntervals.DAY: timeType = Calendar.DAY_OF_MONTH; timeType2 = Calendar.WEEK_OF_YEAR; break;/*from ww w . j a v a 2 s . c o m*/ case TimeIntervals.WEEK: timeType = Calendar.WEEK_OF_YEAR; timeType2 = Calendar.MONTH; break; case TimeIntervals.MONTH: timeType = Calendar.MONTH; timeType2 = Calendar.MONTH; timeIncrement2 = 3; break; case TimeIntervals.QUARTER: timeType = Calendar.MONTH; timeType2 = Calendar.YEAR; timeIncrement = 3; break; case TimeIntervals.YEAR: timeType = Calendar.YEAR; timeType2 = Calendar.YEAR; break; default: return null; } Calendar c = Calendar.getInstance(DateUtils.UTC_TIME_ZONE, Locale.US);//DateTime.calendarInstance(); c.setTimeInMillis(start); //adapt start floorCal(scale, c); long s1 = c.getTimeInMillis(); floorCal(scale + 1, c); long s2 = c.getTimeInMillis(); c.setTimeInMillis(s1); long s; while ((s = c.getTimeInMillis()) >= s2) { //can occur with week, month scale s1 = s; c.add(timeType, -timeIncrement); } //set approximative end c.setTimeInMillis(s1); c.add(timeType, sign * timeIncrement * WINDOW_INTERVALS); TimeWindow win = new TimeWindow(); if (sign > 0) win.setS(s1); else win.setE(s1); if (sign > 0) win.setE(c.getTimeInMillis()); else win.setS(c.getTimeInMillis()); return win; }
From source file:org.hxzon.demo.jfreechart.OtherDatasetDemo.java
private static long millisForDate(int date, int month, int year) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, date, 12, 0); return calendar.getTimeInMillis(); }
From source file:com.hp.test.framework.Reporting.Utlis.java
public static String gettimeinMilliSec() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date now = new Date(); String dateInString = sdf.format(now); Date date = sdf.parse(dateInString); Calendar calendar = Calendar.getInstance(); calendar.setTime(date);/*from w w w.j a v a 2 s . c om*/ return String.valueOf(calendar.getTimeInMillis()); }
From source file:com.taobao.tdhs.client.util.ConvertUtil.java
public static Long getTimeFromString(String stringVal, @Nullable Calendar cal) throws SQLException { if (stringVal == null) { return null; }//from w w w . j a va 2s . c o m String val = stringVal.trim(); if (val.length() == 0) { return null; } if (val.equals("0") || val.equals("0000-00-00") || val.equals("0000-00-00 00:00:00") || val.equals("00000000000000") || val.equals("0")) { Calendar calendar = null; if (cal != null) { calendar = Calendar.getInstance(cal.getTimeZone()); } else { calendar = Calendar.getInstance(); } calendar.set(Calendar.YEAR, 1); calendar.set(Calendar.MONTH, 0); calendar.set(Calendar.DAY_OF_MONTH, 1); return calendar.getTimeInMillis(); } DateFormat dateFormat; if (val.length() == "yyyy-MM-dd".length()) { dateFormat = new SimpleDateFormat("yyyy-MM-dd"); } else { dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } if (cal != null) { TimeZone timeZone = cal.getTimeZone(); dateFormat.setTimeZone(timeZone); } try { return dateFormat.parse(val).getTime(); } catch (ParseException e) { throw new SQLException("Parse date failure:" + val); } }
From source file:com.arm.connector.bridge.core.Utils.java
public static int getUTCOffset() { TimeZone tz = TimeZone.getDefault(); Calendar cal = GregorianCalendar.getInstance(tz); return tz.getOffset(cal.getTimeInMillis()); }
From source file:net.seratch.taskun.util.CalendarUtil.java
public static boolean isFirstAfterSecond(Calendar first, Calendar second) { long firstValue = first.getTimeInMillis(); long secondValue = second.getTimeInMillis(); return (firstValue > secondValue) ? true : false; }
From source file:fr.paris.lutece.plugins.appointment.modules.resource.web.AppointmentResourceJspBean.java
/** * Get the resource calendar for a week/*from w w w . j a va 2s.c om*/ * @param request The request * @param resource The resource * @param nOffsetWeek The week offset * @param locale The locale * ...) * @return The HTML content to display */ public static String getResourceCalendar(HttpServletRequest request, IResource resource, int nOffsetWeek, Locale locale) { AppointmentService appointmentService = AppointmentService.getService(); Map<String, Object> model = new HashMap<String, Object>(); model.put(MARK_RESOURCE, resource); Date dateMonday = appointmentService.getDateMonday(nOffsetWeek); Calendar calendar = new GregorianCalendar(); calendar.setTime(dateMonday); calendar.add(Calendar.DAY_OF_WEEK, 7); Date dateMax = new Date(calendar.getTimeInMillis()); List<Integer> listIdAppointments = AppointmentResourceHome.findIdAppointmentsByResourceAndDate( resource.getIdResource(), resource.getResourceType(), dateMonday, dateMax); List<Appointment> listAppointment = new ArrayList<Appointment>(listIdAppointments.size()); Map<Integer, List<CalendarAppointmentResourceDTO>> mapCalendarAppointmentResourceByDayOfWeek = new HashMap<Integer, List<CalendarAppointmentResourceDTO>>(); int nStartingHour = 0; int nStartingMinute = 0; int nEndingHour = 0; int nEndingMinute = 0; int nMinGlobalStartingTime = 9999; int nMaxGlobalEndingTime = 0; int nMinDuration = -1; for (int i = 1; i < 8; i++) { mapCalendarAppointmentResourceByDayOfWeek.put(i, new ArrayList<CalendarAppointmentResourceDTO>()); } for (int nIdAppointment : listIdAppointments) { Appointment appointment = AppointmentHome.findByPrimaryKey(nIdAppointment); listAppointment.add(appointment); AppointmentSlot slot = AppointmentSlotHome.findByPrimaryKey(appointment.getIdSlot()); CalendarAppointmentResourceDTO calendarAppointmentResource = new CalendarAppointmentResourceDTO( appointment.getIdAppointment(), slot.getStartingHour(), slot.getStartingMinute(), slot.getEndingHour(), slot.getEndingMinute(), getAppointmentRecap(appointment, locale)); int nStartingTimeSlot = (slot.getStartingHour() * 60) + slot.getStartingMinute(); if (nStartingTimeSlot < nMinGlobalStartingTime) { nMinGlobalStartingTime = nStartingTimeSlot; nStartingHour = slot.getStartingHour(); nStartingMinute = slot.getStartingMinute(); } int nEndingTimeSlot = (slot.getEndingHour() * 60) + slot.getEndingMinute(); if (nEndingTimeSlot > nMaxGlobalEndingTime) { nMaxGlobalEndingTime = nEndingTimeSlot; nEndingHour = slot.getEndingHour(); nEndingMinute = slot.getEndingMinute(); } if ((calendarAppointmentResource.getDuration() < nMinDuration) || (nMinDuration == -1)) { nMinDuration = calendarAppointmentResource.getDuration(); } int nDayOfWeek = appointmentService.getDayOfWeek(appointment.getDateAppointment()); List<CalendarAppointmentResourceDTO> listCalendar = mapCalendarAppointmentResourceByDayOfWeek .get(nDayOfWeek); listCalendar.add(calendarAppointmentResource); } List<CalendarDayDTO> listDays = new ArrayList<CalendarDayDTO>(7); for (Entry<Integer, List<CalendarAppointmentResourceDTO>> entry : mapCalendarAppointmentResourceByDayOfWeek .entrySet()) { CalendarDayDTO day = new CalendarDayDTO(); Calendar calendarDay = new GregorianCalendar(); calendarDay.setTime(dateMonday); calendarDay.add(Calendar.DAY_OF_WEEK, entry.getKey() - 1); day.setDate(calendarDay.getTime()); List<CalendarAppointmentResourceDTO> listCalendarApp = entry.getValue(); Collections.sort(listCalendarApp); day.setListAppointmentResourceDTO(listCalendarApp); listDays.add(day); } Collections.sort(listDays); List<AppointmentForm> listAppointmentForm = AppointmentFormHome.getActiveAppointmentFormsList(); for (AppointmentForm appointmentForm : listAppointmentForm) { int nOpeningTime = (appointmentForm.getOpeningHour() * 60) + appointmentForm.getOpeningMinutes(); if (nOpeningTime < nMinGlobalStartingTime) { nMinGlobalStartingTime = nOpeningTime; nStartingHour = appointmentForm.getOpeningHour(); nStartingMinute = appointmentForm.getOpeningMinutes(); } int nClosingTime = (appointmentForm.getClosingHour() * 60) + appointmentForm.getClosingMinutes(); if (nClosingTime > nMaxGlobalEndingTime) { nMaxGlobalEndingTime = nClosingTime; nEndingHour = appointmentForm.getClosingHour(); nEndingMinute = appointmentForm.getClosingMinutes(); } if ((appointmentForm.getDurationAppointments() < nMinDuration) || (nMinDuration < 0)) { nMinDuration = appointmentForm.getDurationAppointments(); } } model.put(MARK_LIST_DAYS, listDays); model.put(PARAMETER_OFFSET_WEEK, nOffsetWeek); model.put(MARK_LIST_DAYS_OF_WEEK, MESSAGE_LIST_DAYS_OF_WEEK); model.put(MARK_STARTING_TIME, nMinGlobalStartingTime); model.put(MARK_ENDING_TIME, nMaxGlobalEndingTime); model.put(MARK_DURATION, nMinDuration); model.put(MARK_STARTING_HOUR, nStartingHour); model.put(MARK_STARTING_MINUTE, nStartingMinute); model.put(MARK_ENDING_HOUR, nEndingHour); model.put(MARK_ENDING_MINUTE, nEndingMinute); HtmlTemplate template = AppTemplateService.getTemplate(TEMPLATE_RESOURCE_CALENDAR, locale, model); return template.getHtml(); }