List of usage examples for java.util GregorianCalendar getTimeInMillis
public long getTimeInMillis()
From source file:Main.java
/** * Calculates the difference in days, rounding the operands down to the nearest day. * Ex. Jan 3rd 12:31 pm - Jan 2nd 4:00 pm * = Jan 3rd - Jan 2nd//from w w w. ja v a 2 s . c o m * = 1 day * @return The difference in days. */ public static int differenceInDays(GregorianCalendar minuend, GregorianCalendar subtrahend) { GregorianCalendar minuendFloor = new GregorianCalendar(minuend.get(Calendar.YEAR), minuend.get(Calendar.MONTH), minuend.get(Calendar.DAY_OF_MONTH)); GregorianCalendar subtrahendFloor = new GregorianCalendar(subtrahend.get(Calendar.YEAR), subtrahend.get(Calendar.MONTH), subtrahend.get(Calendar.DAY_OF_MONTH)); GregorianCalendar result = new GregorianCalendar(); result.setTimeInMillis(minuendFloor.getTimeInMillis() - subtrahendFloor.getTimeInMillis()); return result.get(Calendar.DAY_OF_YEAR); }
From source file:de.escoand.readdaily.ReminderHandler.java
public static void startReminder(final Context context, final int hour, final int minute) { Intent intent = new Intent(context, ReminderHandler.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE); GregorianCalendar cal = new GregorianCalendar(); // get next reminder cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, minute); cal.set(Calendar.SECOND, 0);/* w ww. j av a 2s .co m*/ cal.set(Calendar.MILLISECOND, 0); if (cal.before(Calendar.getInstance())) cal.add(Calendar.DATE, 1); am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent); LogHandler.log(Log.WARN, "activated " + hour + ":" + minute); }
From source file:de.arago.rike.zombie.ZombieHelper.java
static String toPrettyJSON(List ticks) { List<OverdueMilestone> milestones = getOverdueMilestones(true); Collections.sort(milestones, new DoneSorter()); final List data = new ArrayList(); final Map open = new HashMap(); final Map in_progress = new HashMap(); final Map done = new HashMap(); data.add(open);//ww w . j a va 2 s. com data.add(in_progress); data.add(done); open.put("Label", "open"); open.put("color", "red"); in_progress.put("Label", "in_progress"); in_progress.put("color", "yellow"); done.put("Label", "done"); done.put("color", "green"); List openData = new ArrayList(); List in_progressData = new ArrayList(); List doneData = new ArrayList(); open.put("data", openData); in_progress.put("data", in_progressData); done.put("data", doneData); long now = new Date().getTime(); int i = 1; for (OverdueMilestone o : milestones) { Milestone stone = o.getMilestone(); ticks.add("<a href='/web/guest/rike/-/show/milestone/" + stone.getId() + "'>" + StringEscapeUtils.escapeHtml(stone.getTitle()) + "</a>"); GregorianCalendar c = new GregorianCalendar(); c.setTime(stone.getDueDate()); c.add(Calendar.HOUR_OF_DAY, -((o.getWorkLeftInHours() - o.getWorkInProgressInHours()) * 7 * 24) / stone.getPerformance()); long time1 = c.getTimeInMillis(); c.add(Calendar.HOUR_OF_DAY, -(o.getWorkInProgressInHours() * 7 * 24) / stone.getPerformance()); long time2 = c.getTimeInMillis(); c.add(Calendar.HOUR_OF_DAY, -(o.getWorkDoneInHours() * 7 * 24) / stone.getPerformance()); long time3 = c.getTimeInMillis(); List item = new ArrayList(); item.add(time1); item.add(i); item.add(stone.getDueDate().getTime()); item.add(""); openData.add(item); item = new ArrayList(); item.add(time2); item.add(i); item.add(time1); item.add(""); in_progressData.add(item); item = new ArrayList(); item.add(time3); item.add(i); item.add(time2); item.add(""); doneData.add(item); ++i; } return JSONArray.toJSONString(data); }
From source file:com.bangalore.barcamp.BCBUtils.java
public static void setAlarmForSession(Context context, Slot slot, Session session, int slotpos, int sessionpos) { BCBSharedPrefUtils.setAlarmSettingsForID(context, session.id, BCBSharedPrefUtils.ALARM_SET); PendingIntent intent = BCBUtils.createPendingIntentForID(context, session.id, slotpos, sessionpos); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); int hour = slot.startTime / 100; int mins = slot.startTime % 100; Log.e("Session", "hour : " + hour + " mins :" + mins); GregorianCalendar date = new GregorianCalendar(2013, Calendar.SEPTEMBER, 14, hour, mins); long timeInMills = date.getTimeInMillis() - 300000; alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMills, intent); }
From source file:com.saax.gestorweb.util.DAOAleatorio.java
public static Date getDataByOffset(int offsetDataAtual, boolean up) { GregorianCalendar gc = new GregorianCalendar(); Date hoje = DateUtils.truncate(new Date(), Calendar.DATE); gc.setTime(hoje); // hoje truncando as horas for (int i = 0; i < offsetDataAtual; i++) { gc.roll(GregorianCalendar.DAY_OF_MONTH, up); if (gc.getActualMaximum(GregorianCalendar.DAY_OF_MONTH) == gc.get(GregorianCalendar.DAY_OF_MONTH)) { gc.roll(GregorianCalendar.MONTH, up); }//from w w w .ja v a 2 s .c o m } return DateUtils.truncate(new Date(gc.getTimeInMillis()), Calendar.DATE); }
From source file:org.onosproject.drivers.bti.Bti7000SnmpAlarmConsumer.java
/** * Converts an SNMP string representation into a {@link Date} object, * and applies time zone conversion to provide the time on the local machine, ie PSM server. * * @param actAlarmDateAndTime MIB-II DateAndTime formatted. May optionally contain * a timezone offset in 3 extra bytes * @param sysInfoTimeZone Must be supplied if actAlarmDateAndTime is just local time (with no timezone) * @param swVersion Must be supplied if actAlarmDateAndTime is just local time (with no timezone) * @return adjusted {@link Date} or a simple conversion if other fields are null. *//*from www.j av a2s . co m*/ public static Date getLocalDateAndTime(String actAlarmDateAndTime, String sysInfoTimeZone, String swVersion) { if (StringUtils.isBlank(actAlarmDateAndTime)) { return null; } GregorianCalendar decodedDateAndTimeCal = btiMakeCalendar(OctetString.fromHexString(actAlarmDateAndTime)); if ((sysInfoTimeZone == null) || (swVersion == null)) { return decodedDateAndTimeCal.getTime(); } TimeZone javaTimeZone = getTimeZone(); decodedDateAndTimeCal.setTimeZone(javaTimeZone); GregorianCalendar localTime = new GregorianCalendar(); localTime.setTimeInMillis(decodedDateAndTimeCal.getTimeInMillis()); return localTime.getTime(); }
From source file:eu.openanalytics.rsb.Util.java
/** * Converts a {@link GregorianCalendar} into a {@link XMLGregorianCalendar} * /*from www . j a va2 s . c om*/ * @param calendar * @return */ public static XMLGregorianCalendar convertToXmlDate(final GregorianCalendar calendar) { final GregorianCalendar zuluDate = new GregorianCalendar(); zuluDate.setTimeZone(TimeZone.getTimeZone("UTC")); zuluDate.setTimeInMillis(calendar.getTimeInMillis()); final XMLGregorianCalendar xmlDate = XML_DATATYPE_FACTORY.newXMLGregorianCalendar(zuluDate); return xmlDate; }
From source file:com.telefonica.iot.cygnus.utils.CommonUtils.java
/** * Gets the timestamp within a TimeInstant metadata, if exists. * @param metadata//from w w w.j av a 2 s . com * @return The timestamp within a TimeInstant metadata */ public static Long getTimeInstant(String metadata) { Long res = null; JSONParser parser = new JSONParser(); JSONArray mds; try { mds = (JSONArray) parser.parse(metadata); } catch (ParseException e) { LOGGER.error("Error while parsing the metadaga. Details: " + e.getMessage()); return null; } // try catch for (Object mdObject : mds) { JSONObject md = (JSONObject) mdObject; String mdName = (String) md.get("name"); if (mdName.equals("TimeInstant")) { String mdValue = (String) md.get("value"); if (isANumber(mdValue)) { res = new Long(mdValue); } else { DateTime dateTime; try { // ISO 8601 without miliseconds dateTime = FORMATTER1.parseDateTime(mdValue); } catch (Exception e1) { LOGGER.debug(e1.getMessage()); try { // ISO 8601 with miliseconds dateTime = FORMATTER2.parseDateTime(mdValue); } catch (Exception e2) { LOGGER.debug(e2.getMessage()); try { // ISO 8601 with microsencods String mdValueTruncated = mdValue.substring(0, mdValue.length() - 4) + "Z"; dateTime = FORMATTER2.parseDateTime(mdValueTruncated); } catch (Exception e3) { LOGGER.debug(e3.getMessage()); try { // SQL timestamp without miliseconds dateTime = FORMATTER3.parseDateTime(mdValue); } catch (Exception e4) { LOGGER.debug(e4.getMessage()); try { // SQL timestamp with miliseconds dateTime = FORMATTER4.parseDateTime(mdValue); } catch (Exception e5) { LOGGER.debug(e5.getMessage()); try { // SQL timestamp with microseconds String mdValueTruncated = mdValue.substring(0, mdValue.length() - 3); dateTime = FORMATTER4.parseDateTime(mdValueTruncated); } catch (Exception e6) { LOGGER.debug(e6.getMessage()); try { // ISO 8601 with offset (without milliseconds) dateTime = FORMATTER5.parseDateTime(mdValue); } catch (Exception e7) { LOGGER.debug(e7.getMessage()); try { // ISO 8601 with offset (with milliseconds) Matcher matcher = FORMATTER6_PATTERN.matcher(mdValue); if (matcher.matches()) { String mdValueTruncated = matcher.group(1) + "." + matcher.group(2).substring(0, 3) + matcher.group(3); dateTime = FORMATTER6.parseDateTime(mdValueTruncated); } else { LOGGER.debug("ISO8601 format does not match"); return null; } // if } catch (Exception e8) { LOGGER.debug(e8.getMessage()); return null; } // try catch } // try catch } // try catch } // try catch } // try catch } // try catch } // try catch } // try catch GregorianCalendar cal = dateTime.toGregorianCalendar(); res = cal.getTimeInMillis(); } // if else break; } // if } // for return res; }
From source file:simpleserver.thread.AutoBackup.java
private static long dateMillis(File file) throws ParseException { GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date(file));//from w w w . j a va2 s .co m return cal.getTimeInMillis(); }
From source file:org.rifidi.emulator.reader.llrp.report.LLRPReportController.java
/** * Gets the UTC Time.//from w w w . j ava 2 s . c o m * * @param dateString * @param timeString * @return time in milliseconds */ @SuppressWarnings("deprecation") private static long getUTCTimeInMillis(String dateString, String timeString) { /* * TODO: baindaid solution until radio saves and returns java date and * time objects */ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); Date date, time = null; try { date = dateFormat.parse(dateString); time = timeFormat.parse(timeString); } catch (ParseException e) { logger.error("There was a problem converting the date or time string into a Date object"); date = new Date(); time = new Date(); } GregorianCalendar gc = new GregorianCalendar(date.getYear(), date.getMonth(), date.getDate(), time.getHours(), time.getMinutes(), time.getSeconds()); return gc.getTimeInMillis(); }