List of usage examples for java.util Calendar getTimeInMillis
public long getTimeInMillis()
From source file:com.projity.util.DateTime.java
public static long minuteFloor(long date) { Calendar cal = DateTime.calendarInstance(); cal.setTimeInMillis(date);// w w w .ja va2s .c om cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 0); return cal.getTimeInMillis(); }
From source file:Main.java
public static String convertToHumanReadableTime(Date givenDate, long currentTimeLong) { Calendar currentTime = Calendar.getInstance(); currentTime.setTimeZone(TimeZone.getTimeZone("UTC")); currentTime.setTimeInMillis(currentTimeLong); Calendar givenTime = Calendar.getInstance(); givenTime.setTimeZone(TimeZone.getTimeZone("UTC")); givenTime.setTime(givenDate);/*from ww w .j a v a 2s . co m*/ // Step 1: To see if time difference is less than 24 hours of not long timeDiff = currentTime.getTimeInMillis() - givenTime.getTimeInMillis(); if (timeDiff <= 0) { return "Now"; } String humanString = null; // Checking if timeDiff is less than 24 or not if ((timeDiff / TIME_DAY) >= 1) { // days int days = (int) (timeDiff / TIME_DAY); humanString = String.format(Locale.getDefault(), "%dd", days); } else { // checking if greater than hour if ((timeDiff / TIME_HOUR) >= 1) { humanString = String.format(Locale.getDefault(), "%dh", (timeDiff / TIME_HOUR)); } else if ((timeDiff / TIME_MINUTE) >= 1) { humanString = String.format(Locale.getDefault(), "%dm", (timeDiff / TIME_MINUTE)); } else { humanString = String.format(Locale.getDefault(), "%ds", (timeDiff / TIME_MILLIS)); } } return humanString; }
From source file:com.mozilla.socorro.RawDumpSizeScan.java
/** * Generates an array of scans for different salted ranges for the given dates * @param startDate//from w ww. ja v a2 s.co m * @param endDate * @return */ public static Scan[] generateScans(Calendar startCal, Calendar endCal) { SimpleDateFormat rowsdf = new SimpleDateFormat("yyMMdd"); ArrayList<Scan> scans = new ArrayList<Scan>(); String[] salts = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; long endTime = DateUtil.getEndTimeAtResolution(endCal.getTimeInMillis(), Calendar.DATE); while (startCal.getTimeInMillis() < endTime) { int d = Integer.parseInt(rowsdf.format(startCal.getTime())); for (int i = 0; i < salts.length; i++) { Scan s = new Scan(); s.setCaching(4); // disable block caching s.setCacheBlocks(false); s.addColumn(RAW_DATA_BYTES, DUMP_BYTES); s.setStartRow(Bytes.toBytes(salts[i] + String.format("%06d", d))); s.setStopRow(Bytes.toBytes(salts[i] + String.format("%06d", d + 1))); System.out.println("Adding start-stop range: " + salts[i] + String.format("%06d", d) + " - " + salts[i] + String.format("%06d", d + 1)); scans.add(s); } startCal.add(Calendar.DATE, 1); } return scans.toArray(new Scan[scans.size()]); }
From source file:Main.java
/** * Add date time with one minute.//from w ww . ja v a2 s . c o m * * @param listing the listing * @param currentTime the current time * @param isPast true/false if past time */ private static void addDateTimeWithOneMinute(final List<Long> listing, final long currentTime, final boolean isPast) { final Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(currentTime); if (isPast) { calendar.add(Calendar.MINUTE, -1); } else { calendar.add(Calendar.SECOND, 62); } listing.add(calendar.getTimeInMillis()); }
From source file:Main.java
public static void setAlarm(Context context, Intent intent, int notificationId, Calendar calendar) { intent.putExtra("NOTIFICATION_ID", notificationId); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);/*w w w. j av a2s .co m*/ } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); } else { alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); } }
From source file:es.tekniker.framework.ktek.util.Utils.java
public static long getTimeinMillis4TimeGMT(int hours, int minutes, int seconds) { Calendar c = getCalendarGMT(); System.out.println("current: " + c.getTime()); c.set(Calendar.HOUR_OF_DAY, hours); c.set(Calendar.MINUTE, minutes); c.set(Calendar.SECOND, seconds); System.out.println("GMT Time: " + c.getTime()); long timeinmillis = c.getTimeInMillis(); System.out.println(" system time in millis " + timeinmillis); return timeinmillis; }
From source file:Main.java
/** * * This method is a utility method to create a new java.sql.Date in one line. * * @param year/*www.ja v a 2 s. c o m*/ * @param month * @param day * * @return a populated java.sql.Date with the year, month, and day specified, and no values for hour, minute, second, * millisecond * */ public static java.sql.Date newDate(Integer year, Integer month, Integer day) { // test for null arguments if (year == null) { throw new IllegalArgumentException("Argument 'year' passed in was null."); } if (month == null) { throw new IllegalArgumentException("Argument 'month' passed in was null."); } if (day == null) { throw new IllegalArgumentException("Argument 'day' passed in was null."); } Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month); calendar.set(Calendar.DAY_OF_MONTH, day); calendar.clear(Calendar.HOUR_OF_DAY); calendar.clear(Calendar.MINUTE); calendar.clear(Calendar.SECOND); calendar.clear(Calendar.MILLISECOND); return new java.sql.Date(calendar.getTimeInMillis()); }
From source file:Main.java
public static boolean isInTimeNow(int start, int end) { Calendar calendar = new ThreadLocal<Calendar>() { protected Calendar initialValue() { return Calendar.getInstance(); }/*from w ww .j a v a2 s . c o m*/ }.get(); long current = System.currentTimeMillis(); calendar.setTimeInMillis(current); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); long originToday = calendar.getTimeInMillis(); long distance = current - originToday; if (start <= distance && distance <= end) { return true; } return false; }
From source file:com.projity.util.DateTime.java
public static long closestDate(long date) { Calendar cal = DateTime.calendarInstance(); cal.setTimeInMillis(date);//from ww w. j a v a 2 s. co m // cal.set(Calendar.SECOND,0); // now going to seconds cal.set(Calendar.MILLISECOND, 0); return cal.getTimeInMillis(); }
From source file:Main.java
/** * Formats a date in the xml xs:dateTime format, for mountain time. This * method does not support any other timezone. * * @param calendar the calendar to format * * @return the formatted date/*from w w w.java 2s . c o m*/ */ public static String xsDateTimeFormatMountain(final Calendar calendar) { final SimpleDateFormat dateFormat; final SimpleDateFormat timeFormat; final TimeZone currentTimeZone; dateFormat = new SimpleDateFormat("yyyy-MM-dd"); timeFormat = new SimpleDateFormat("HH:mm:ss"); currentTimeZone = TimeZone.getTimeZone("Canada/Mountain"); final long offset; final long hours; final String tzID; offset = currentTimeZone.getOffset(calendar.getTimeInMillis()); hours = -offset / (1000 * 60 * 60); tzID = "-0" + hours + ":00"; // assume single digit hour, we always are return dateFormat.format(calendar.getTime()) + 'T' + timeFormat.format(calendar.getTime()) + tzID; }