Android examples for java.util:Hour
Checks if time now is between quiet hours "from" and "to" values.
//package com.java2s; import java.util.Calendar; public class Main { /**//ww w. j a v a 2s . c om * Checks if time now is between quiet hours "from" and "to" values. Creates 3 new * new timestamps and sets hours and minutes for quiet hours "from" and "to" * and then compares the timestamps in milliseconds. * @param quietFrom Quiet hours from timestamp in milliseconds. * @param quietTo Quiet hours to timestamp in milliseconds. * @return Is time now between quiet hours "from" and "to" values? */ public static Boolean isNowBetweenQuietHours(Long quietFrom, Long quietTo) { Calendar timeNow = Calendar.getInstance(); Calendar timeFrom = Calendar.getInstance(); timeFrom.set(Calendar.HOUR_OF_DAY, getHourFromMilliseconds(quietFrom)); timeFrom.set(Calendar.MINUTE, getMinuteFromMilliseconds(quietFrom)); Calendar timeTo = Calendar.getInstance(); timeTo.set(Calendar.HOUR_OF_DAY, getHourFromMilliseconds(quietTo)); timeTo.set(Calendar.MINUTE, getMinuteFromMilliseconds(quietTo)); if (timeTo.before(timeFrom)) { timeTo.add(Calendar.DATE, 1); } return timeNow.after(timeFrom) && timeNow.before(timeTo) ? true : false; } /** * Gets hour of day from milliseconds. * @param milliSeconds Timestamp in milliseconds. * @return Hour of day. */ public static int getHourFromMilliseconds(Long milliSeconds) { Calendar time = Calendar.getInstance(); time.setTimeInMillis(milliSeconds); return time.get(Calendar.HOUR_OF_DAY); } /** * Gets minutes value from milliseconds. * @param milliSeconds Timestamp in milliseconds. * @return Minutes. */ public static int getMinuteFromMilliseconds(Long milliSeconds) { Calendar time = Calendar.getInstance(); time.setTimeInMillis(milliSeconds); return time.get(Calendar.MINUTE); } }