List of usage examples for java.util GregorianCalendar get
public int get(int field)
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>/*w w w . j a v a 2 s . c om*/ * 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:org.webical.test.web.component.MonthViewPanelTest.java
/** * Test rendering of the panel without events. *///from w ww .java 2 s . co m public void testRenderingWithoutEvents() { log.debug("testRenderingWithoutEvents"); // Add an EventManager annotApplicationContextMock.putBean("eventManager", new MockEventManager()); final GregorianCalendar currentDate = CalendarUtils.newTodayCalendar(getFirstDayOfWeek()); // render start page with a MonthViewPanel wicketTester.startPage(new ITestPageSource() { private static final long serialVersionUID = 1L; public Page getTestPage() { return new PanelTestPage(new MonthViewPanel(PanelTestPage.PANEL_MARKUP_ID, 1, currentDate) { private static final long serialVersionUID = 1L; @Override public void onAction(IAction action) { /* NOTHING TO DO */ } }); } }); // Basic Assertions wicketTester.assertRenderedPage(PanelTestPage.class); wicketTester.assertComponent(PanelTestPage.PANEL_MARKUP_ID, MonthViewPanel.class); // Weekday headers wicketTester.assertComponent(PanelTestPage.PANEL_MARKUP_ID + ":monthHeaderRepeater", RepeatingView.class); DateFormat dateFormat = new SimpleDateFormat("E", getTestSession().getLocale()); GregorianCalendar weekCal = CalendarUtils.duplicateCalendar(currentDate); weekCal.setTime(CalendarUtils.getFirstDayOfWeek(currentDate.getTime(), getFirstDayOfWeek())); for (int i = 0; i < 7; ++i) { wicketTester.assertLabel(PanelTestPage.PANEL_MARKUP_ID + ":monthHeaderRepeater:headerDay" + i, dateFormat.format(weekCal.getTime())); weekCal.add(GregorianCalendar.DAY_OF_WEEK, 1); } // Set the correct dates to find the first and last day of the month GregorianCalendar monthFirstDayDate = CalendarUtils.duplicateCalendar(currentDate); monthFirstDayDate .setTime(CalendarUtils.getFirstDayOfWeekOfMonth(currentDate.getTime(), getFirstDayOfWeek())); log.debug("testRenderingWithoutEvents:monthFirstDayDate " + monthFirstDayDate.getTime()); // Assert the first day in the view wicketTester.assertComponent(PanelTestPage.PANEL_MARKUP_ID + ":monthRowRepeater:week" + monthFirstDayDate.get(GregorianCalendar.WEEK_OF_YEAR) + ":monthDayRepeater:day" + monthFirstDayDate.get(GregorianCalendar.DAY_OF_YEAR), MonthDayPanel.class); GregorianCalendar monthLastDayDate = CalendarUtils.duplicateCalendar(currentDate); monthLastDayDate.setTime(CalendarUtils.getLastWeekDayOfMonth(currentDate.getTime(), getFirstDayOfWeek())); log.debug("testRenderingWithoutEvents:monthLastDayDate " + monthLastDayDate.getTime()); // Assert the last day in the view wicketTester.assertComponent(PanelTestPage.PANEL_MARKUP_ID + ":monthRowRepeater:week" + monthLastDayDate.get(GregorianCalendar.WEEK_OF_YEAR) + ":monthDayRepeater:day" + monthLastDayDate.get(GregorianCalendar.DAY_OF_YEAR), MonthDayPanel.class); }
From source file:org.tolven.analysis.bean.SnapshotBean.java
/** * Based on the age and gender of the patient, this function validated the patients who satisfies * the two cohort properties - Age Ranges and gender.Return either true or false. *//*www . ja v a2s . c o m*/ @Override public Boolean validateCohortProperties(String type, MenuData patient, AppEvalAdaptor app) { Boolean genderCondition = false; Boolean ageCondition = false; String lowRangeType = ""; String highRangeType = ""; String calcType = ""; int lowRange = 0; int highRange = 0; int ageY = 0; int ageM = 0; int ageD = 0; int ageW = 0; int calcAge = 0; //gender Condition if (app.getAccount().getProperty().get("org.tolven.cohort." + type + ".gender") == null) { genderCondition = true; } else if (app.getAccount().getProperty().get("org.tolven.cohort." + type + ".gender").equals("")) { genderCondition = true; } else if ((app.getAccount().getProperty().get("org.tolven.cohort." + type + ".gender") .equals(patient.getString04().trim())) || (app.getAccount().getProperty().get("org.tolven.cohort." + type + ".gender").equals("Both"))) { genderCondition = true; } //age Condition if (app.getAccount().getProperty().get("org.tolven.cohort." + type + ".ageRangeCodes") == null) { ageCondition = true; } else if (app.getAccount().getProperty().get("org.tolven.cohort." + type + ".ageRangeCodes").equals("")) { ageCondition = true; } else { for (int i = 1; i < app.getAccount().getProperty().get("org.tolven.cohort." + type + ".ageRangeCodes") .split(",").length; i++) { ageCondition = false; lowRange = Integer.parseInt(app.getAccount().getProperty() .get("org.tolven.cohort." + type + ".ageRangeCodes").split(",")[i].split("~")[0]); lowRangeType = app.getAccount().getProperty().get("org.tolven.cohort." + type + ".ageRangeCodes") .split(",")[i].split("~")[1]; highRange = Integer.parseInt(app.getAccount().getProperty() .get("org.tolven.cohort." + type + ".ageRangeCodes").split(",")[i].split("~")[2]); highRangeType = app.getAccount().getProperty().get("org.tolven.cohort." + type + ".ageRangeCodes") .split(",")[i].split("~")[3]; GregorianCalendar n = new GregorianCalendar(); n.setTime(new Date()); GregorianCalendar b = new GregorianCalendar(); b.setTime(patient.getDate01()); int years = n.get(Calendar.YEAR) - b.get(Calendar.YEAR); int days = n.get(Calendar.DAY_OF_YEAR) - b.get(Calendar.DAY_OF_YEAR); if (days < 0) { years--; days = days + 365; } if (years > 1) { calcAge = years; calcType = "year"; } else if (years == 0 && days < 30) { calcAge = days; calcType = "days"; } else { calcAge = years * 12 + (days / 30); calcType = "months"; } if (calcType.equals("year")) { ageD = calcAge * 365; ageM = calcAge * 12; ageY = calcAge; ageW = calcAge * 52; } if (calcType.equals("months")) { ageD = calcAge * 30; ageM = calcAge; ageY = 0; ageW = calcAge / 7; } if (calcType.equals("days")) { ageD = calcAge; ageM = 0; ageY = 0; ageW = calcAge / 7; } if ((lowRangeType.equals("year")) && (highRangeType.equals("year"))) { if ((ageY >= lowRange) && (ageY <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("year")) && (highRangeType.equals("month"))) { if ((ageY >= lowRange) && (ageM <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("year")) && (highRangeType.equals("week"))) { if ((ageY >= lowRange) && (ageW <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("year")) && (highRangeType.equals("day"))) { if ((ageY >= lowRange) && (ageD <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("month")) && (highRangeType.equals("year"))) { if ((ageM >= lowRange) && (ageY <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("month")) && (highRangeType.equals("month"))) { if ((ageM >= lowRange) && (ageM <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("month")) && (highRangeType.equals("week"))) { if ((ageM >= lowRange) && (ageW <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("month")) && (highRangeType.equals("day"))) { if ((ageM >= lowRange) && (ageD <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("week")) && (highRangeType.equals("year"))) { if ((ageW >= lowRange) && (ageY <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("week")) && (highRangeType.equals("month"))) { if ((ageW >= lowRange) && (ageM <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("week")) && (highRangeType.equals("week"))) { if ((ageW >= lowRange) && (ageW <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("week")) && (highRangeType.equals("day"))) { if ((ageW >= lowRange) && (ageD <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("day")) && (highRangeType.equals("year"))) { if ((ageD >= lowRange) && (ageY <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("day")) && (highRangeType.equals("month"))) { if ((ageD >= lowRange) && (ageM <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("day")) && (highRangeType.equals("week"))) { if ((ageD >= lowRange) && (ageW <= highRange)) { ageCondition = true; } } if ((lowRangeType.equals("day")) && (highRangeType.equals("day"))) { if ((ageD >= lowRange) && (ageD <= highRange)) { ageCondition = true; } } if (ageCondition == true) break; } } if (genderCondition == true && ageCondition == true) { return true; } else { return false; } }
From source file:org.sakaibrary.osid.repository.xserver.AssetIterator.java
private void setDateRetrieved() { java.util.GregorianCalendar now = new java.util.GregorianCalendar(); int month = now.get(java.util.Calendar.MONTH) + 1; int date = now.get(java.util.Calendar.DATE); String monthStr, dateStr;//www . j a v a 2 s . c om if (month < 10) { monthStr = "0" + month; } else { monthStr = String.valueOf(month); } if (date < 10) { dateStr = "0" + date; } else { dateStr = String.valueOf(date); } String dateRetrieved = now.get(java.util.Calendar.YEAR) + "-" + monthStr + "-" + dateStr; try { record.createPart(DateRetrievedPartStructure.getInstance().getId(), dateRetrieved); } catch (org.osid.repository.RepositoryException re) { LOG.warn("setDateRetrieved() failed " + "creating new dateRetrieved Part.", re); } }
From source file:clummy.classes.DataHandlingClass.java
/** * generate random date of birth/*from w ww . ja va 2s. c o m*/ * @return */ public String getDateOfBirth() { GregorianCalendar gc = new GregorianCalendar(); int year = randBetween(DataHandlingClass.minMaxDOB[0], DataHandlingClass.minMaxDOB[1] - 1); gc.set(gc.YEAR, year); int dayOfYear = randBetween(1, gc.getActualMaximum(gc.DAY_OF_YEAR)); gc.set(gc.DAY_OF_YEAR, dayOfYear); int month = gc.get(gc.MONTH); if (month == 0) month = 1; if (dateSeperator.equals(" ")) dateSeperator = ""; if (dateFormat.equalsIgnoreCase("DDMMYYYY")) return String.format("%02d", gc.get(gc.DAY_OF_MONTH)) + dateSeperator + String.format("%02d", month) + dateSeperator + gc.get(gc.YEAR); else if (dateFormat.equalsIgnoreCase("MMDDYYYY")) return String.format("%02d", month) + dateSeperator + String.format("%02d", gc.get(gc.DAY_OF_MONTH)) + dateSeperator + gc.get(gc.YEAR); else if (dateFormat.equalsIgnoreCase("YYYYDDMM")) return gc.get(gc.YEAR) + dateSeperator + String.format("%02d", gc.get(gc.DAY_OF_MONTH)) + dateSeperator + String.format("%02d", month); else return gc.get(gc.YEAR) + dateSeperator + String.format("%02d", month) + dateSeperator + String.format("%02d", gc.get(gc.DAY_OF_MONTH)); }
From source file:org.wso2.carbon.registry.eventing.RegistryEventDispatcher.java
public RegistryEventDispatcher() { digestQueues = new LinkedHashMap<String, Queue<DigestEntry>>(); for (String s : new String[] { "h", "d", "w", "f", "m", "y" }) { //TODO: Identify Queuing mechanisms. digestQueues.put(s, new ConcurrentLinkedQueue<DigestEntry>()); }//from ww w .ja v a2s . co m final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); executorService.scheduleAtFixedRate(new Runnable() { public void run() { GregorianCalendar utc = new GregorianCalendar(SimpleTimeZone.getTimeZone("UTC")); Map<String, List<DigestEntry>> digestEntries = new HashMap<String, List<DigestEntry>>(); try { addToDigestEntryQueue(digestEntries, "h"); if (utc.get(Calendar.HOUR_OF_DAY) == 0) { addToDigestEntryQueue(digestEntries, "d"); if (utc.get(Calendar.DAY_OF_WEEK) == 1) { addToDigestEntryQueue(digestEntries, "w"); if (utc.get(Calendar.WEEK_OF_YEAR) % 2 != 0) { addToDigestEntryQueue(digestEntries, "f"); } } if (utc.get(Calendar.DAY_OF_MONTH) == 1) { addToDigestEntryQueue(digestEntries, "m"); if (utc.get(Calendar.DAY_OF_YEAR) == 1) { addToDigestEntryQueue(digestEntries, "y"); } } } for (Map.Entry<String, List<DigestEntry>> e : digestEntries.entrySet()) { List<DigestEntry> value = e.getValue(); Collections.sort(value, new Comparator<DigestEntry>() { public int compare(DigestEntry o1, DigestEntry o2) { if (o1.getTime() > o2.getTime()) { return -1; } else if (o1.getTime() < o2.getTime()) { return 1; } return 0; } }); StringBuffer buffer = new StringBuffer(); for (DigestEntry entry : value) { buffer.append(entry.getMessage()).append("\n\n"); } RegistryEvent<String> re = new RegistryEvent<String>(buffer.toString()); re.setTopic(RegistryEvent.TOPIC_SEPARATOR + "DigestEvent"); DispatchEvent de = new DispatchEvent(re, e.getKey(), true); Subscription subscription = new Subscription(); subscription.setTopicName(re.getTopic()); publishEvent(de, subscription, e.getKey(), true); } } catch (RuntimeException ignored) { // Eat any runtime exceptions that occurred, we don't care if the message went // or not. } } }, System.currentTimeMillis() % (1000 * 60 * 60), 1000 * 60 * 60, TimeUnit.MILLISECONDS); try { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { executorService.shutdownNow(); } }); } catch (IllegalStateException e) { executorService.shutdownNow(); throw new IllegalStateException( "Unable to create registry event dispatcher during " + "shutdown process."); } }
From source file:net.naijatek.myalumni.framework.struts.MyAlumniValidator.java
/** * // w w w . j a va 2 s.c o m */ public boolean orgFirstYear(Object bean, ValidatorAction va, Field field, ActionMessages msgs, HttpServletRequest request) { SystemConfigForm sysConfigForm = (SystemConfigForm) bean; String orgYear = sysConfigForm.getOrgFirstYear(); int intOrgYear = Integer.parseInt(orgYear); GregorianCalendar ct = new GregorianCalendar(); int currentYear = ct.get(Calendar.YEAR); if (intOrgYear > currentYear) { msgs.add(field.getKey(), new ActionMessage("error.orgfirstyear", String.valueOf(currentYear))); saveErrors(request, msgs); } return msgs.isEmpty(); }
From source file:main.UIController.java
public void showDateOfToday(boolean useCacheSunset) { Preferences data = this.getCurrentPreferences(); String city = data.get(FIELD_CITY, DEF_CITY); String country = data.get(FIELD_COUNTRY, DEF_COUNTRY); TimeZone tz = TimeZone.getTimeZone(data.get(FIELD_TIMEZONE, DEF_TIMEZONE)); GregorianCalendar gcal = new GregorianCalendar(tz); Time time = this.calculateSunsetForActualLocation(gcal, useCacheSunset); ImladrisCalendar cal;/*from ww w . j a v a 2 s.com*/ String sunsetStr = ""; String locationInfo = ""; if (time == null) { cal = new ImladrisCalendar(gcal); } else { cal = new ImladrisCalendar(time, gcal); /*check if before sunset*/ String gtimeStr = gcal.get(GregorianCalendar.HOUR_OF_DAY) + ":" + gcal.get(GregorianCalendar.MINUTE) + ":" + gcal.get(GregorianCalendar.SECOND); Time gtime = Time.valueOf(gtimeStr); if (gtime.before(time)) { // before sunset sunsetStr = " - before sunset"; } else { // after/during sunset sunsetStr = " - after sunset"; } String sunsetTimeStr = time.toString(); sunsetTimeStr = sunsetTimeStr.substring(0, sunsetTimeStr.length() - 3); sunsetStr += " " + Lang.punctuation.parenthesis_open + "occurring at " + sunsetTimeStr + Lang.punctuation.parenthesis_close; locationInfo = this.makeLocationString(city, country); if (locationInfo.length() > 0) { locationInfo = Lang.punctuation.parenthesis_open + Lang.common.location_label + Lang.punctuation.double_dot + " " + locationInfo + " " + Lang.punctuation.pipe + " " + Lang.common.timezone_label + Lang.punctuation.double_dot + " " + data.get(FIELD_TIMEZONE, DEF_TIMEZONE) + Lang.punctuation.parenthesis_close; } } String gstr = this.gregorianToString(gcal); String istr = cal.toString(); UI ui = this.getUi(); ui.getTodayGregorian().setText(gstr + sunsetStr); ui.getTodayImladris().setText(istr); ui.getLocationInfo().setText(locationInfo); }
From source file:org.egov.egf.web.actions.budget.BaseBudgetDetailAction.java
public Date getPreviousYearFor(final Date date) { final GregorianCalendar previousYearToDate = new GregorianCalendar(); previousYearToDate.setTime(date);//w ww .j a v a 2 s.c o m final int prevYear = previousYearToDate.get(Calendar.YEAR) - 1; previousYearToDate.set(Calendar.YEAR, prevYear); return previousYearToDate.getTime(); }
From source file:com.opendesign.utils.Day.java
private void createDateFactorsByGivenMilliSecond(long milliSeconds, TimeZone timeZone) { if (timeZone == null) { this.timeZone = TimeZone.getDefault(); } else {// w ww .j a va 2s . c om this.timeZone = timeZone; } GregorianCalendar todaysDate = new GregorianCalendar(this.timeZone); if (milliSeconds != 0) { todaysDate.setTimeInMillis(milliSeconds); } this.year = todaysDate.get(Calendar.YEAR); this.month = todaysDate.get(Calendar.MONTH) + 1; this.day = todaysDate.get(Calendar.DAY_OF_MONTH); this.hour = todaysDate.get(Calendar.HOUR_OF_DAY); this.minute = todaysDate.get(Calendar.MINUTE); this.second = todaysDate.get(Calendar.SECOND); }