List of usage examples for java.util GregorianCalendar set
public void set(int field, int value)
From source file:org.eclipse.uomo.util.impl.Iso8601Date.java
/** * get the date specified as a Date.//from w w w . j a v a 2 s . c o m * * @param timezone * - REQUIRED, IF_PRESENT or IGNORE * @throws OHFException */ public Date getAsDate(int timezone) throws UOMoException { GregorianCalendar cal = new GregorianCalendar(); cal.clear(); if (year != -1) cal.set(Calendar.YEAR, year); if (month != -1) cal.set(Calendar.MONTH, month - 1); // Calendar month start with 0 if (day != -1) cal.set(Calendar.DAY_OF_MONTH, day); if (hour != -1) cal.set(Calendar.HOUR_OF_DAY, hour); if (minute != -1) cal.set(Calendar.MINUTE, minute); if (second != -1) cal.set(Calendar.SECOND, second); if (milli != -1) cal.set(Calendar.MILLISECOND, milli); if (timezone == REQUIRED || timezone == IF_PRESENT || timezone == OPTIONAL) { if (timezone == REQUIRED && tzNegative == null) throw new UOMoException(Messages.Iso8601Date_TZ_NOT_DEFINED); if (tzNegative != null) { TimeZone tzMsg = TimeZone.getTimeZone(Messages.Iso8601Date_GMT + prepTimezone()); cal.setTimeZone(tzMsg); } } return cal.getTime(); }
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);//from w w w.ja v a 2 s. co m final int prevYear = previousYearToDate.get(Calendar.YEAR) - 1; previousYearToDate.set(Calendar.YEAR, prevYear); return previousYearToDate.getTime(); }
From source file:org.webical.test.web.component.WeekViewPanelTest.java
public void testWeekUse() throws WebicalException { MockCalendarManager mockCalendarManager = (MockCalendarManager) annotApplicationContextMock .getBean("calendarManager"); Calendar calendar1 = mockCalendarManager.getCalendarById("1"); MockEventManager mockEventManager = new MockEventManager(); annotApplicationContextMock.putBean("eventManager", mockEventManager); SimpleDateFormat dateFormat = new SimpleDateFormat( WebicalSession.getWebicalSession().getUserSettings().getTimeFormat(), getTestSession().getLocale()); final GregorianCalendar currentDate = CalendarUtils.newTodayCalendar(getFirstDayOfWeek()); // All events for this week List<Event> allEvents = new ArrayList<Event>(); Date midWeek = CalendarUtils.getFirstDayOfWeek(currentDate.getTime(), getFirstDayOfWeek()); midWeek = CalendarUtils.addDays(midWeek, 3); /* CREATE EVENTS TO RENDER */ GregorianCalendar cal = CalendarUtils.duplicateCalendar(currentDate); // Add a normal event this week Event event = new Event(); event.setUid("e1"); event.setCalendar(calendar1);/* w ww. j a v a 2 s . co m*/ event.setSummary("Normal Event Description"); event.setLocation("Normal Event Location"); event.setDescription("Event e1"); cal.setTime(midWeek); cal.set(GregorianCalendar.HOUR_OF_DAY, 12); event.setDtStart(cal.getTime()); cal.add(GregorianCalendar.HOUR_OF_DAY, 2); event.setDtEnd(cal.getTime()); log.debug( "Adding event: " + event.getDescription() + " -> " + event.getDtStart() + " - " + event.getDtEnd()); allEvents.add(event); mockEventManager.storeEvent(event); // Add a short recurring event, starting Tuesday, ending Friday event = new Event(); event.setUid("e2"); event.setCalendar(calendar1); event.setSummary("Recurring Event Yesterday"); event.setLocation("Recurring Event Location"); event.setDescription("Event e2"); cal.setTime(midWeek); cal.set(GregorianCalendar.HOUR_OF_DAY, 15); cal.add(GregorianCalendar.DAY_OF_MONTH, -1); event.setDtStart(cal.getTime()); cal.add(GregorianCalendar.HOUR_OF_DAY, 3); cal.add(GregorianCalendar.DAY_OF_MONTH, 3); event.setDtEnd(cal.getTime()); RecurrenceUtil.setRecurrenceRule(event, new Recurrence(Recurrence.DAILY, 1, CalendarUtils.getEndOfDay(cal.getTime()))); log.debug( "Adding event: " + event.getDescription() + " -> " + event.getDtStart() + " - " + event.getDtEnd()); allEvents.add(event); mockEventManager.storeEvent(event); // Add a long recurring event, starting last month, ending next month event = new Event(); event.setUid("e3"); event.setCalendar(calendar1); event.setSummary("Recurring Event Last Month"); event.setLocation("Recurring Event Location"); event.setDescription("Event e3"); cal.setTime(midWeek); cal.set(GregorianCalendar.HOUR_OF_DAY, 9); cal.add(GregorianCalendar.WEEK_OF_YEAR, -1); event.setDtStart(cal.getTime()); cal.add(GregorianCalendar.HOUR_OF_DAY, 2); cal.add(GregorianCalendar.WEEK_OF_YEAR, 3); event.setDtEnd(cal.getTime()); RecurrenceUtil.setRecurrenceRule(event, new Recurrence(Recurrence.DAILY, 1, CalendarUtils.getEndOfDay(cal.getTime()))); log.debug( "Adding event: " + event.getDescription() + " -> " + event.getDtStart() + " - " + event.getDtEnd()); allEvents.add(event); mockEventManager.storeEvent(event); // Add a (pseudo) all day event, starting Monday midnight, ending Tuesday 00:00 hours event = new Event(); event.setUid("e4"); event.setCalendar(calendar1); event.setSummary("Pseudo All Day Event"); event.setLocation("All Day Event Location"); event.setDescription("Starting Monday 00:00 hours, ending Tuesday 00:00 hours"); cal.setTime(midWeek); cal.add(GregorianCalendar.DAY_OF_MONTH, -2); cal.set(GregorianCalendar.HOUR_OF_DAY, 0); cal.set(GregorianCalendar.MINUTE, 0); cal.set(GregorianCalendar.SECOND, 0); cal.set(GregorianCalendar.MILLISECOND, 0); event.setDtStart(cal.getTime()); cal.add(GregorianCalendar.DAY_OF_MONTH, 1); event.setDtEnd(cal.getTime()); log.debug( "Adding event: " + event.getDescription() + " -> " + event.getDtStart() + " - " + event.getDtEnd()); allEvents.add(event); mockEventManager.storeEvent(event); // Add a long event this week starting Friday 14.00 hours and ending Saturday 16.00 hours event = new Event(); event.setUid("e5"); event.setCalendar(calendar1); event.setSummary("Long Event Description"); event.setLocation("Long Event Location"); event.setDescription("Event e5"); cal.setTime(midWeek); cal.add(GregorianCalendar.DAY_OF_MONTH, 2); cal.set(GregorianCalendar.HOUR_OF_DAY, 14); cal.set(GregorianCalendar.MINUTE, 0); event.setDtStart(cal.getTime()); cal.add(GregorianCalendar.DAY_OF_MONTH, 1); cal.add(GregorianCalendar.HOUR_OF_DAY, 2); event.setDtEnd(cal.getTime()); log.debug( "Adding event: " + event.getDescription() + " -> " + event.getDtStart() + " - " + event.getDtEnd()); allEvents.add(event); mockEventManager.storeEvent(event); // Create test page with a WeekViewPanel wicketTester.startPage(new ITestPageSource() { private static final long serialVersionUID = 1L; public Page getTestPage() { return new PanelTestPage(new WeekViewPanel(PanelTestPage.PANEL_MARKUP_ID, 7, 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, WeekViewPanel.class); // Set the correct dates to find the first and last day of the week // Assert number of days rendered GregorianCalendar weekFirstDayCalendar = CalendarUtils.duplicateCalendar(currentDate); weekFirstDayCalendar.setTime(CalendarUtils.getFirstDayOfWeek(currentDate.getTime(), getFirstDayOfWeek())); // Assert the first day in the view wicketTester.assertComponent(PanelTestPage.PANEL_MARKUP_ID + ":weekColumnRepeater:day" + weekFirstDayCalendar.get(GregorianCalendar.DAY_OF_YEAR), WeekDayPanel.class); GregorianCalendar weekLastDayCalendar = CalendarUtils.duplicateCalendar(currentDate); weekLastDayCalendar.setTime(CalendarUtils.getLastDayOfWeek(currentDate.getTime(), getFirstDayOfWeek())); // Assert the last day in the view wicketTester.assertComponent(PanelTestPage.PANEL_MARKUP_ID + ":weekColumnRepeater:day" + weekLastDayCalendar.get(GregorianCalendar.DAY_OF_YEAR), WeekDayPanel.class); // Events for days in this week List<Event> dayEvents = new ArrayList<Event>(); // Assert weekday events GregorianCalendar weekCal = CalendarUtils.duplicateCalendar(currentDate); weekCal.setTime(CalendarUtils.getFirstDayOfWeek(currentDate.getTime(), getFirstDayOfWeek())); for (int i = 0; i < 7; ++i) { WeekDayPanel weekDayEventsListView = (WeekDayPanel) wicketTester.getLastRenderedPage() .get(PanelTestPage.PANEL_MARKUP_ID + ":weekColumnRepeater:day" + weekCal.get(GregorianCalendar.DAY_OF_YEAR)); int weekDay = getFirstDayOfWeek() + weekCal.get(GregorianCalendar.DAY_OF_WEEK) - 1; // First day of the week is 1 if (weekDay > 7) weekDay = 1; switch (weekDay) { case GregorianCalendar.SUNDAY: dayEvents.clear(); dayEvents.add(allEvents.get(2)); // e3 break; case GregorianCalendar.MONDAY: dayEvents.clear(); dayEvents.add(allEvents.get(2)); // e3 dayEvents.add(allEvents.get(3)); // e4 break; case GregorianCalendar.TUESDAY: dayEvents.clear(); dayEvents.add(allEvents.get(1)); // e2 dayEvents.add(allEvents.get(2)); // e3 break; case GregorianCalendar.WEDNESDAY: dayEvents.clear(); dayEvents.add(allEvents.get(0)); // e1 dayEvents.add(allEvents.get(1)); // e2 dayEvents.add(allEvents.get(2)); // e3 break; case GregorianCalendar.THURSDAY: dayEvents.clear(); dayEvents.add(allEvents.get(1)); // e2 dayEvents.add(allEvents.get(2)); // e3 break; case GregorianCalendar.FRIDAY: dayEvents.clear(); dayEvents.add(allEvents.get(1)); // e2 dayEvents.add(allEvents.get(2)); // e3 dayEvents.add(allEvents.get(4)); // e5 break; case GregorianCalendar.SATURDAY: dayEvents.clear(); dayEvents.add(allEvents.get(2)); // e3 dayEvents.add(allEvents.get(4)); // e5 break; } String path = weekDayEventsListView.getPageRelativePath() + ":eventItem"; wicketTester.assertListView(path, dayEvents); ListView listView = (ListView) wicketTester.getComponentFromLastRenderedPage(path); Iterator<?> lvIt = listView.iterator(); while (lvIt.hasNext()) { ListItem item = (ListItem) lvIt.next(); Event evt = (Event) item.getModelObject(); List<?> bhvs = item.getBehaviors(); if (evt.getUid().equals("e4")) assertEquals(1, bhvs.size()); // only e4 is an all day event else assertEquals(0, bhvs.size()); if (evt.getUid().equals("e5")) { // e5: Fri 14:00 to Sat 16:00 String timePath = item.getPageRelativePath() + ":eventLink:eventTime"; String timeLabelText = null; if (weekDay == GregorianCalendar.FRIDAY) timeLabelText = dateFormat.format(evt.getDtStart()); else timeLabelText = dateFormat.format(midWeek); // time 00:00 wicketTester.assertLabel(timePath, timeLabelText); } } weekCal.add(GregorianCalendar.DAY_OF_WEEK, 1); } }
From source file:org.alfresco.mobile.android.api.services.impl.onpremise.OnPremiseWorkflowServiceImpl.java
/** {@inheritDoc} */ @SuppressWarnings("unchecked") public PagingResult<Process> getProcesses(ListingContext listingContext) { List<Process> processes = new ArrayList<Process>(); Map<String, Object> json = new HashMap<String, Object>(0); int maxItems = -1; try {/* w ww. jav a 2 s. c om*/ String link = OnPremiseUrlRegistry.getProcessesUrl(session); UrlBuilder url = new UrlBuilder(link); if (listingContext != null) { if (listingContext.getFilter() != null) { ListingFilter lf = listingContext.getFilter(); // Assignee if (lf.hasFilterValue(FILTER_KEY_INITIATOR)) { if (lf.getFilterValue(FILTER_KEY_INITIATOR) instanceof String) { url.addParameter(OnPremiseConstant.INITIATOR_VALUE, lf.getFilterValue(FILTER_KEY_INITIATOR)); } else if (lf.getFilterValue(FILTER_KEY_ASSIGNEE) instanceof Integer) { switch ((Integer) lf.getFilterValue(FILTER_KEY_INITIATOR)) { case FILTER_ASSIGNEE_ME: url.addParameter(OnPremiseConstant.INITIATOR_VALUE, session.getPersonIdentifier()); break; case FILTER_ASSIGNEE_ALL: break; default: break; } } } else { url.addParameter(OnPremiseConstant.INITIATOR_VALUE, session.getPersonIdentifier()); } if (lf.hasFilterValue(FILTER_KEY_PRIORITY)) { url.addParameter(OnPremiseConstant.PRIORITY_VALUE, lf.getFilterValue(FILTER_KEY_PRIORITY)); } if (lf.hasFilterValue(FILTER_KEY_STATUS)) { switch ((Integer) lf.getFilterValue(FILTER_KEY_STATUS)) { case FILTER_STATUS_COMPLETE: url.addParameter(OnPremiseConstant.STATE_VALUE, OnPremiseConstant.COMPLETED_UPPERCASE_VALUE); break; case FILTER_STATUS_ACTIVE: url.addParameter(OnPremiseConstant.STATE_VALUE, OnPremiseConstant.ACTIVE_UPPERCASE_VALUE); break; default: break; } } if (lf.hasFilterValue(FILTER_KEY_DUE)) { GregorianCalendar calendar = new GregorianCalendar(); calendar.set(Calendar.HOUR, 11); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); switch ((Integer) lf.getFilterValue(FILTER_KEY_DUE)) { case FILTER_DUE_TODAY: url.addParameter(OnPremiseConstant.DUEBEFORE_VALUE, DateUtils.format(calendar)); calendar.add(Calendar.DAY_OF_MONTH, -1); url.addParameter(OnPremiseConstant.DUEAFTER_VALUE, DateUtils.format(calendar)); break; case FILTER_DUE_TOMORROW: url.addParameter(OnPremiseConstant.DUEAFTER_VALUE, DateUtils.format(calendar)); calendar.add(Calendar.DAY_OF_MONTH, 1); url.addParameter(OnPremiseConstant.DUEBEFORE_VALUE, DateUtils.format(calendar)); break; case FILTER_DUE_7DAYS: url.addParameter(OnPremiseConstant.DUEAFTER_VALUE, DateUtils.format(calendar)); calendar.add(Calendar.DAY_OF_MONTH, 7); url.addParameter(OnPremiseConstant.DUEBEFORE_VALUE, DateUtils.format(calendar)); break; case FILTER_DUE_OVERDUE: calendar.add(Calendar.DAY_OF_MONTH, -1); url.addParameter(OnPremiseConstant.DUEBEFORE_VALUE, DateUtils.format(calendar)); break; case FILTER_DUE_NODATE: url.addParameter(OnPremiseConstant.DUEBEFORE_VALUE, ""); break; default: break; } } } url.addParameter(OnPremiseConstant.MAX_ITEMS_VALUE, listingContext.getMaxItems()); maxItems = listingContext.getMaxItems(); url.addParameter(OnPremiseConstant.SKIP_COUNT_VALUE, listingContext.getSkipCount()); } else { url.addParameter(OnPremiseConstant.STATE_VALUE, OnPremiseConstant.IN_PROGRESS_UPPERCASE_VALUE); } // send and parse Response resp = read(url, ErrorCodeRegistry.WORKFLOW_GENERIC); json = JsonUtils.parseObject(resp.getStream(), resp.getCharset()); if (json != null) { List<Object> jo = (List<Object>) json.get(OnPremiseConstant.DATA_VALUE); for (Object obj : jo) { processes.add(ProcessImpl.parseJson((Map<String, Object>) obj)); } } } catch (Exception e) { convertException(e); } return new PagingResultImpl<Process>(processes, maxItems == -1, json.size()); }
From source file:org.apache.eagle.jpm.mr.history.MRHistoryJobDailyReporter.java
@Override protected void runOneIteration() throws Exception { GregorianCalendar calendar = new GregorianCalendar(timeZone); int currentHour = calendar.get(Calendar.HOUR_OF_DAY); long currentTimestamp = calendar.getTimeInMillis(); if (!isSentHour(currentHour)) { isDailySent = false;//www . j a v a 2 s . co m } else if (!isDailySent) { isDailySent = true; LOG.info("last job report time is {} %s", DateTimeUtil.millisecondsToHumanDateWithSeconds(lastSentTime), timeZone.getID()); try { Collection<String> sites = loadSites(APP_TYPE); if (sites == null || sites.isEmpty()) { LOG.warn("application MR_HISTORY_JOB_APP does not run on any sites!"); return; } for (String site : sites) { int reportHour = currentHour / dailySentPeriod * dailySentPeriod; calendar.set(Calendar.HOUR_OF_DAY, reportHour); long endTime = calendar.getTimeInMillis() / DateTimeUtil.ONEHOUR * DateTimeUtil.ONEHOUR; long startTime = endTime - DateTimeUtil.ONEHOUR * dailySentPeriod; String subject = buildAlertSubject(site, startTime, endTime); Map<String, Object> alertData = buildAlertData(site, startTime, endTime); sendByEmailWithSubject(alertData, subject); } } catch (Exception ex) { LOG.error("Fail to get job summery info due to {}", ex.getMessage(), ex); } lastSentTime = currentTimestamp; } }
From source file:org.alfresco.mobile.android.api.services.impl.onpremise.OnPremiseWorkflowServiceImpl.java
/** {@inheritDoc} */ @SuppressWarnings("unchecked") private PagingResult<Task> getTasks(String link, ListingContext listingContext) { List<Task> tasks = new ArrayList<Task>(); Map<String, Object> json = new HashMap<String, Object>(0); int maxItems = -1; int size = 0; try {/*from ww w .j a v a 2 s .co m*/ UrlBuilder url = new UrlBuilder(link); if (listingContext != null) { if (listingContext.getFilter() != null) { ListingFilter lf = listingContext.getFilter(); // Assignee if (lf.hasFilterValue(FILTER_KEY_ASSIGNEE)) { if (lf.getFilterValue(FILTER_KEY_ASSIGNEE) instanceof String) { url.addParameter(OnPremiseConstant.AUTHORITY_VALUE, lf.getFilterValue(FILTER_KEY_ASSIGNEE)); } else if (lf.getFilterValue(FILTER_KEY_ASSIGNEE) instanceof Integer) { switch ((Integer) lf.getFilterValue(FILTER_KEY_ASSIGNEE)) { case FILTER_ASSIGNEE_UNASSIGNED: url.addParameter(OnPremiseConstant.AUTHORITY_VALUE, session.getPersonIdentifier()); url.addParameter(OnPremiseConstant.POOLEDTASKS_VALUE, true); break; case FILTER_ASSIGNEE_ME: url.addParameter(OnPremiseConstant.AUTHORITY_VALUE, session.getPersonIdentifier()); url.addParameter(OnPremiseConstant.POOLEDTASKS_VALUE, false); break; case FILTER_ASSIGNEE_ALL: url.addParameter(OnPremiseConstant.AUTHORITY_VALUE, session.getPersonIdentifier()); break; case FILTER_NO_ASSIGNEE: break; default: break; } } } else { url.addParameter(OnPremiseConstant.AUTHORITY_VALUE, session.getPersonIdentifier()); } if (lf.hasFilterValue(FILTER_KEY_PRIORITY)) { url.addParameter(OnPremiseConstant.PRIORITY_VALUE, lf.getFilterValue(FILTER_KEY_PRIORITY)); } if (lf.hasFilterValue(FILTER_KEY_STATUS)) { switch ((Integer) lf.getFilterValue(FILTER_KEY_STATUS)) { case FILTER_STATUS_COMPLETE: url.addParameter(OnPremiseConstant.STATE_VALUE, OnPremiseConstant.COMPLETED_UPPERCASE_VALUE); break; case FILTER_STATUS_ACTIVE: url.addParameter(OnPremiseConstant.STATE_VALUE, OnPremiseConstant.IN_PROGRESS_UPPERCASE_VALUE); break; case FILTER_STATUS_ANY: url.addParameter(OnPremiseConstant.INCLUDETASKS_VALUE, "true"); break; default: break; } } if (lf.hasFilterValue(FILTER_KEY_DUE)) { GregorianCalendar calendar = new GregorianCalendar(); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); switch ((Integer) lf.getFilterValue(FILTER_KEY_DUE)) { case FILTER_DUE_TODAY: url.addParameter(OnPremiseConstant.DUEBEFORE_VALUE, DateUtils.format(calendar)); calendar.add(Calendar.DAY_OF_MONTH, -1); url.addParameter(OnPremiseConstant.DUEAFTER_VALUE, DateUtils.format(calendar)); break; case FILTER_DUE_TOMORROW: url.addParameter(OnPremiseConstant.DUEAFTER_VALUE, DateUtils.format(calendar)); calendar.add(Calendar.DAY_OF_MONTH, 1); url.addParameter(OnPremiseConstant.DUEBEFORE_VALUE, DateUtils.format(calendar)); break; case FILTER_DUE_7DAYS: url.addParameter(OnPremiseConstant.DUEAFTER_VALUE, DateUtils.format(calendar)); calendar.add(Calendar.DAY_OF_MONTH, 7); url.addParameter(OnPremiseConstant.DUEBEFORE_VALUE, DateUtils.format(calendar)); break; case FILTER_DUE_OVERDUE: calendar.add(Calendar.DAY_OF_MONTH, -1); url.addParameter(OnPremiseConstant.DUEBEFORE_VALUE, DateUtils.format(calendar)); break; case FILTER_DUE_NODATE: url.addParameter(OnPremiseConstant.DUEBEFORE_VALUE, ""); break; default: break; } } } url.addParameter(OnPremiseConstant.MAX_ITEMS_VALUE, listingContext.getMaxItems()); maxItems = listingContext.getMaxItems(); url.addParameter(OnPremiseConstant.SKIP_COUNT_VALUE, listingContext.getSkipCount()); } else { url.addParameter(OnPremiseConstant.STATE_VALUE, OnPremiseConstant.IN_PROGRESS_UPPERCASE_VALUE); } Log.d(TAG, url.toString()); // send and parse Response resp = read(url, ErrorCodeRegistry.WORKFLOW_GENERIC); json = JsonUtils.parseObject(resp.getStream(), resp.getCharset()); if (json != null) { List<Object> jo = null; if (json.get(OnPremiseConstant.DATA_VALUE) instanceof List) { jo = (List<Object>) json.get(OnPremiseConstant.DATA_VALUE); } else if (json.get(OnPremiseConstant.DATA_VALUE) instanceof Map) { Map<String, Object> jso = (Map<String, Object>) json.get(OnPremiseConstant.DATA_VALUE); jo = (List<Object>) jso.get(OnPremiseConstant.TASKS_VALUE); } size = jo.size(); for (Object obj : jo) { tasks.add(TaskImpl.parseJson((Map<String, Object>) obj)); } } } catch (Exception e) { convertException(e); } return new PagingResultImpl<Task>(tasks, maxItems != -1, size); }
From source file:org.sakaiproject.time.impl.BasicTimeService.java
/** * {@inheritDoc}// www . j a v a 2 s . c o m */ public GregorianCalendar getCalendar(TimeZone zone, int year, int month, int day, int hour, int min, int sec, int ms) { GregorianCalendar rv = new GregorianCalendar(year, month, day, hour, min, sec); rv.setTimeZone(zone); rv.set(GregorianCalendar.MILLISECOND, ms); return rv; }
From source file:op.tools.SYSCalendar.java
/** * erkennt Uhrzeiten im Format HH:MM und erstellt einen GregorianCalendar basierend auf ref *//*from w w w.j ava 2 s .c o m*/ public static GregorianCalendar parseTime(String input, GregorianCalendar gc) throws NumberFormatException { if (input == null || input.equals("")) { throw new NumberFormatException("leere Eingabe"); } StringTokenizer st = new StringTokenizer(input, ":,."); if (st.countTokens() > 3) { throw new NumberFormatException("falsches Format"); } String sStunde = "00"; String sMinute = "00"; String sSekunde = "00"; if (st.countTokens() == 1) { // maybe a simple hour sStunde = st.nextToken(); } else if (st.countTokens() == 2) { // maybe a simple hour sStunde = st.nextToken(); sMinute = st.nextToken(); } else { sStunde = st.nextToken(); sMinute = st.nextToken(); sSekunde = st.nextToken(); } int stunde, minute, sekunde; GregorianCalendar now = (GregorianCalendar) gc.clone(); try { stunde = Integer.parseInt(sStunde); } catch (NumberFormatException nfe) { throw new NumberFormatException("stunde"); } try { minute = Integer.parseInt(sMinute); } catch (NumberFormatException nfe) { throw new NumberFormatException("minute"); } try { sekunde = Integer.parseInt(sSekunde); } catch (NumberFormatException nfe) { throw new NumberFormatException("Sekunde"); } if (stunde < 0) { throw new NumberFormatException("stunde"); } if (stunde > 23) { throw new NumberFormatException("stunde"); } if (minute < 0 || minute > 59) { throw new NumberFormatException("minute"); } if (sekunde < 0 || sekunde > 59) { throw new NumberFormatException("Sekunde"); } now.set(GregorianCalendar.HOUR_OF_DAY, stunde); now.set(GregorianCalendar.MINUTE, minute); now.set(GregorianCalendar.SECOND, sekunde); return now; }
From source file:org.opensingular.internal.lib.commons.xml.TestMElement.java
License:asdf
/** * Testa os mtodos de leitura de null./* w w w . j av a 2 s. c om*/ */ @Test public void testGetFormatado() { GregorianCalendar agoraGc = new GregorianCalendar(2001, 2, 31, 23, 59, 49); agoraGc.set(GregorianCalendar.MILLISECOND, 123); //java.util.Date agoraDate = new // java.util.Date(agoraGc.getTimeInMillis()); MElement xml; //getDateFormatado xml = MElement.newInstance("T"); xml.addElement("V2", 12312); xml.addElement("V3", 12312.123); //System.out.println(xml.getFormatadoNumber("V2", 0)); //System.out.println(xml.getFormatadoNumber("V2", 2)); //System.out.println(xml.getFormatadoNumber("V3", 0)); //System.out.println(xml.getFormatadoNumber("V3", 2)); assertEquals(xml.formatNumber("V2", 0), "12.312"); assertEquals(xml.formatNumber("V2", 2), "12.312,00"); assertEquals(xml.formatNumber("V3", 0), "12.312"); assertEquals(xml.formatNumber("V3", 2), "12.312,12"); }
From source file:org.opensingular.internal.lib.commons.xml.TestMElement.java
License:asdf
/** * Verifica se os mtodos de set e get para java.util.Date e GregorianCalendar funcionam. *//*from w w w .jav a2 s .c om*/ @Test public void testSetGetDatas() { GregorianCalendar agoraGc = new GregorianCalendar(2001, 2, 31, 23, 59, 49); agoraGc.set(GregorianCalendar.MILLISECOND, 123); Date agoraDate = new Date(agoraGc.getTimeInMillis()); MElement xml; //Teste ler e escrever Calendar xml = MElement.newInstance("T"); xml.addElement("V2", agoraGc); assertEquals("string calendar errada", xml.getValor("V2"), "2001-03-31T23:59:49.123"); assertEquals("Calendar gravado lido", xml.getCalendar("V2"), agoraGc); //Teste ler e escrever java.util.Date xml = MElement.newInstance("T"); xml.addElement("V", agoraDate); assertEquals("string util.date errada", xml.getValor("V"), "2001-03-31T23:59:49.123"); assertEquals("util.date gravado lido", xml.getDate("V"), agoraDate); //Testa adicionar data como string xml.addDate("V3", "08/01/2003"); assertEquals("data errada", xml.getValor("V3"), "2003-01-08"); //Testa formatao assertEquals("formatao errada", xml.formatDate("V3"), "08/01/2003"); assertEquals("formatao errada", xml.formatDate("V3", "short"), "08/01/03"); assertEquals("formatao errada", xml.formatDate("V3", "medium"), "08/01/2003"); }