List of usage examples for java.util Calendar HOUR
int HOUR
To view the source code for java.util Calendar HOUR.
Click Source Link
get
and set
indicating the hour of the morning or afternoon. From source file:de.austinpadernale.holidays.Holiday.java
public HolidayResult calculate(int year, Locale locale) { HolidayResult result = new HolidayResult(); result.setName(getName(locale));// w w w . j a v a 2 s . co m Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); switch (holidayType) { case FixedDate: cal.set(year, this.getMonth() - 1, this.getDay()); result.setDate(cal); break; case EasterBased: Calendar easter = calcEasterSunday(year); cal.set(year, easter.get(Calendar.MONTH), easter.get(Calendar.DATE)); cal.add(Calendar.DATE, this.getOffset()); result.setDate(cal); break; case FixedDayOfWeek: cal.set(year, this.getMonth() - 1, 1); while (cal.get(Calendar.DAY_OF_WEEK) != this.getDayOfWeek()) { cal.add(Calendar.DATE, 1); } switch (this.getWeek()) { case 1: //do nothing break; case 2: case 3: case 5: cal.add(Calendar.DATE, (this.getWeek() - 1) * 7); break; } if ((cal.get(Calendar.MONTH) + 1) != this.getMonth()) { cal.add(Calendar.DATE, -7); } result.setDate(cal); break; case November23rdBased: cal.set(year, 10, 23); int dow = cal.get(Calendar.DAY_OF_WEEK); while (dow != this.getDayOfWeek()) { cal.add(Calendar.DATE, 1); dow = cal.get(Calendar.DAY_OF_WEEK); } cal.add(Calendar.DATE, this.getWeek() * 7); result.setDate(cal); break; } return result; }
From source file:com.persistent.cloudninja.controller.InstanceHealthDataController.java
/** * Get the start and end time./* www. j a v a 2s .com*/ * @param hour the number of hour difference between start and end time. * @return List containing start and end date. */ private List<String> getStartEndTime(int hour) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); Calendar cal = Calendar.getInstance(); String end = dateFormat.format(cal.getTime()); cal.add(Calendar.HOUR, -hour); String start = dateFormat.format(cal.getTime()); List<String> time = new ArrayList<String>(); time.add(start); time.add(end); return time; }
From source file:eionet.util.Util.java
/** * A method for calculating time difference in MILLISECONDS, between a date-time specified in input parameters and the current * date-time. <BR>//from w w w . ja va2 s . com * This should be useful for calculating sleep time for code that has a certain schedule for execution. * * @param hour * An integer from 0 to 23. If less than 0 or more than 23, then the closest next hour to current hour is taken. * @param date * An integer from 1 to 31. If less than 1 or more than 31, then the closest next date to current date is taken. * @param month * An integer from Calendar.JANUARY to Calendar.DECEMBER. If out of those bounds, the closest next month to current * month is taken. * @param wday * An integer from 1 to 7. If out of those bounds, the closest next weekday to weekday month is taken. * @param zone * A String specifying the time-zone in which the calculations should be done. Please see Java documentation an * allowable time-zones and formats. * @return Time difference in milliseconds. */ public static long timeDiff(int hour, int date, int month, int wday, String zone) { GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone(zone)); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); cal.setFirstDayOfWeek(Calendar.MONDAY); /* * here we force the hour to be one of the defualts if (hour < 0) hour = 0; if (hour > 23) hour = 23; */ int cur_hour = cal.get(Calendar.HOUR); if (cal.get(Calendar.AM_PM) == Calendar.PM) { cur_hour = 12 + cur_hour; } // here we assume that every full hour is accepted /* * if (hour < 0 || hour > 23) { hour = cur_hour>=23 ? 0 : cur_hour + 1; } */ if (wday >= 1 && wday <= 7) { int cur_wday = cal.get(Calendar.DAY_OF_WEEK); if (hour < 0 || hour > 23) { if (cur_wday != wday) { hour = 0; } else { hour = cur_hour >= 23 ? 0 : cur_hour + 1; } } int amount = wday - cur_wday; if (amount < 0) { amount = 7 + amount; } if (amount == 0 && cur_hour >= hour) { amount = 7; } cal.add(Calendar.DAY_OF_WEEK, amount); } else if (month >= Calendar.JANUARY && month <= Calendar.DECEMBER) { // do something about when every date is accepted if (date < 1) { date = 1; } if (date > 31) { date = 31; } int cur_month = cal.get(Calendar.MONTH); int amount = month - cur_month; if (amount < 0) { amount = 12 + amount; } if (amount == 0) { if (cal.get(Calendar.DATE) > date) { amount = 12; } else if (cal.get(Calendar.DATE) == date) { if (cur_hour >= hour) { amount = 12; } } } // cal.set(Calendar.DATE, date); cal.add(Calendar.MONTH, amount); if (date > cal.getActualMaximum(Calendar.DATE)) { date = cal.getActualMaximum(Calendar.DATE); } cal.set(Calendar.DATE, date); } else if (date >= 1 && date <= 31) { int cur_date = cal.get(Calendar.DATE); if (cur_date > date) { cal.add(Calendar.MONTH, 1); } else if (cur_date == date) { if (cur_hour >= hour) { cal.add(Calendar.MONTH, 1); } } cal.set(Calendar.DATE, date); } else { if (hour < 0 || hour > 23) { hour = cur_hour >= 23 ? 0 : cur_hour + 1; } if (cur_hour >= hour) { cal.add(Calendar.DATE, 1); } } if (hour >= 12) { cal.set(Calendar.HOUR, hour - 12); cal.set(Calendar.AM_PM, Calendar.PM); } else { cal.set(Calendar.HOUR, hour); cal.set(Calendar.AM_PM, Calendar.AM); } Date nextDate = cal.getTime(); Date currDate = new Date(); long nextTime = cal.getTime().getTime(); long currTime = (new Date()).getTime(); return nextTime - currTime; }
From source file:com.appeligo.epg.demo.DemoEPGService.java
public ScheduledProgram getLastShowing(String lineupId, String programId) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_MONTH, -1); cal.set(Calendar.HOUR, 18); cal.set(Calendar.MINUTE, 0);/*from ww w.j ava 2s .c o m*/ StationChannel station = createStationChannel("41", "Versus", "VS", "Versus"); return createScheduledProgram(cal.getTime(), programId, station); }
From source file:org.smartplatforms.openid.connect.token.SmartTofuUserApprovalHandler.java
@Override public AuthorizationRequest updateAfterApproval(AuthorizationRequest authorizationRequest, Authentication userAuthentication) { String userId = userAuthentication.getName(); String clientId = authorizationRequest.getClientId(); ClientDetails client = clientDetailsService.loadClientByClientId(clientId); // This must be re-parsed here because SECOAUTH forces us to call things in a strange order if (Boolean.parseBoolean(authorizationRequest.getApprovalParameters().get("user_oauth_approval")) && authorizationRequest.getExtensions().get(CSRF) != null && authorizationRequest.getExtensions() .get(CSRF).equals(authorizationRequest.getApprovalParameters().get(CSRF))) { authorizationRequest.setApproved(true); // process scopes from user input Set<String> allowedScopes = Sets.newHashSet(); Map<String, String> approvalParams = authorizationRequest.getApprovalParameters(); Set<String> keys = approvalParams.keySet(); for (String key : keys) { if (key.startsWith("scope_")) { //This is a scope parameter from the approval page. The value sent back should //be the scope string. Check to make sure it is contained in the client's //registered allowed scopes. String scope = approvalParams.get(key); Set<String> approveSet = Sets.newHashSet(scope); //Make sure this scope is allowed for the given client if (systemScopes.scopesMatch(client.getScope(), approveSet)) { // If it's structured, assign the user-specified parameter SystemScope systemScope = systemScopes.getByValue(scope); if (systemScope != null && systemScope.isStructured()) { String paramValue = approvalParams.get("scopeparam_" + scope); if (!Strings.isNullOrEmpty(paramValue)) { allowedScopes.add(scope + ":" + paramValue); } else { allowedScopes.add(scope); }// www. jav a2s. c o m // .. and if it's unstructured, we're all set } else { allowedScopes.add(scope); } } } } // inject the user-allowed scopes into the auth request authorizationRequest.setScope(allowedScopes); //Only store an ApprovedSite if the user has checked "remember this decision": String remember = authorizationRequest.getApprovalParameters().get("remember"); if (!Strings.isNullOrEmpty(remember) && !remember.equals("none")) { Date timeout = null; if (remember.equals("one-hour")) { // set the timeout to one hour from now Calendar cal = Calendar.getInstance(); cal.add(Calendar.HOUR, 1); timeout = cal.getTime(); } ApprovedSite newSite = approvedSiteService.createApprovedSite(clientId, userId, timeout, allowedScopes); String newSiteId = newSite.getId().toString(); authorizationRequest.getExtensions().put(APPROVED_SITE, newSiteId); } setAuthTime(authorizationRequest); } return authorizationRequest; }
From source file:org.fusesource.camel.component.sap.SapIDocTestSupport.java
@BeforeClass public static void setupIDocTestSupportClass() { DATE_VALUE = Calendar.getInstance(); DATE_VALUE.set(1861, Calendar.APRIL, 12); TIME_VALUE = Calendar.getInstance(); TIME_VALUE.set(Calendar.HOUR, 4); TIME_VALUE.set(Calendar.MINUTE, 30); TIME_VALUE.set(Calendar.SECOND, 15); }
From source file:DateUtil.java
public static int getHour(Date date) { calendar.setTime(date); return calendar.get(Calendar.HOUR); }
From source file:fedora.server.security.servletfilters.CacheElement.java
private static final void checkCalcExpiration(int duration, int unit) throws IllegalArgumentException { if (duration < 0) { throw new IllegalArgumentException("bad duration==" + duration); }//from w w w .j av a 2 s . c o m switch (unit) { case Calendar.MILLISECOND: case Calendar.SECOND: case Calendar.MINUTE: case Calendar.HOUR: break; default: throw new IllegalArgumentException("bad unit==" + unit); } }
From source file:com.appeligo.epg.demo.DemoEPGService.java
public ScheduledProgram getNextShowing(String lineupId, String programId) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_MONTH, 1); cal.set(Calendar.HOUR, 18); cal.set(Calendar.MINUTE, 0);/*from w w w . j av a2s .c o m*/ StationChannel station = createStationChannel("41", "Versus", "VS", "Versus"); return createScheduledProgram(cal.getTime(), programId, station); }
From source file:gbc.jtimecalc.AbstractTimeDifferenceCalculatorTest.java
public void shouldReturn30Days1Hour0Minutes0seconds() { Calendar end = prepareCalendar(2006, Calendar.FEBRUARY, 1, 1, 0, 0, 0); // 1.02.2006 01:00:00.0 setEndTime(end.getTimeInMillis());/*w w w.j a v a2 s. c o m*/ Calendar start = (Calendar) end.clone(); start.add(Calendar.DATE, -30); start.add(Calendar.HOUR, -1); setStartTime(start.getTimeInMillis()); // 2.01.2006 00:00:00.0 expectedValue = messages.get("30Days1Hour"); }