List of usage examples for java.util Calendar DAY_OF_WEEK
int DAY_OF_WEEK
To view the source code for java.util Calendar DAY_OF_WEEK.
Click Source Link
get
and set
indicating the day of the week. From source file:DateChooserPanel.java
/** * Returns the first date that is visible in the grid. This should always * be in the month preceding the month of the selected date. * * @return the date./* w ww. j a va 2 s .com*/ */ private Calendar getFirstVisibleDate() { final Calendar c = Calendar.getInstance(); c.set(this.chosenDate.get(Calendar.YEAR), this.chosenDate.get(Calendar.MONTH), 1); c.add(Calendar.DATE, -1); while (c.get(Calendar.DAY_OF_WEEK) != getFirstDayOfWeek()) { c.add(Calendar.DATE, -1); } return c; }
From source file:com.netsteadfast.greenstep.util.SimpleUtils.java
public static int getDayOfWeek(final int year, final int month) { int dayofweek = 28; Calendar calendar = Calendar.getInstance(); if (year >= 1900 && year <= 3000) { calendar.set(Calendar.YEAR, year); } else {/* w ww . j a va2s. c o m*/ calendar.set(Calendar.YEAR, 1900); } if (month >= 1 && month <= 12) { calendar.set(Calendar.MONTH, (month - 1)); } else { calendar.set(Calendar.MONTH, 0); } calendar.set(Calendar.DATE, 1); dayofweek = calendar.get(Calendar.DAY_OF_WEEK); return dayofweek; }
From source file:com.hihframework.core.utils.DateUtils.java
/** * ??2004-07-28 ?/* ww w.j a v a 2 s. co m*/ * * @param date * @return ? */ public static String getDayWeek(java.sql.Date date) { String days[] = { "", "", "", "", "", "", "" }; Calendar cale = Calendar.getInstance(); cale.setTime(date); cale.setFirstDayOfWeek(Calendar.SUNDAY); return date.toString() + " " + days[cale.get(Calendar.DAY_OF_WEEK) - 1]; }
From source file:com.espertech.esper.schedule.ScheduleComputeHelper.java
private static int determineDayOfMonth(ScheduleSpec spec, Calendar after, ScheduleCalendar result) { SortedSet<Integer> daysOfMonthSet = spec.getUnitValues().get(ScheduleUnit.DAYS_OF_MONTH); SortedSet<Integer> daysOfWeekSet = spec.getUnitValues().get(ScheduleUnit.DAYS_OF_WEEK); SortedSet<Integer> secondsSet = spec.getUnitValues().get(ScheduleUnit.SECONDS); SortedSet<Integer> minutesSet = spec.getUnitValues().get(ScheduleUnit.MINUTES); SortedSet<Integer> hoursSet = spec.getUnitValues().get(ScheduleUnit.HOURS); int dayOfMonth; // If days of week is a wildcard, just go by days of month if (spec.getOptionalDayOfMonthOperator() != null || spec.getOptionalDayOfWeekOperator() != null) { boolean isWeek = false; CronParameter op = spec.getOptionalDayOfMonthOperator(); if (spec.getOptionalDayOfMonthOperator() == null) { op = spec.getOptionalDayOfWeekOperator(); isWeek = true;// w w w . ja va 2 s.c o m } // may return the current day or a future day in the same month, // and may advance the "after" date to the next month int currentYYMMDD = getTimeYYYYMMDD(after); increaseAfterDayOfMonthSpecialOp(op.getOperator(), op.getDay(), op.getMonth(), isWeek, after); int rolledYYMMDD = getTimeYYYYMMDD(after); // if rolled then reset time portion if (rolledYYMMDD > currentYYMMDD) { result.setSecond(nextValue(secondsSet, 0)); result.setMinute(nextValue(minutesSet, 0)); result.setHour(nextValue(hoursSet, 0)); return after.get(Calendar.DAY_OF_MONTH); } // rolling backwards is not allowed else if (rolledYYMMDD < currentYYMMDD) { throw new IllegalStateException( "Failed to evaluate special date op, rolled date less then current date"); } else { Calendar work = (Calendar) after.clone(); work.set(Calendar.SECOND, result.getSecond()); work.set(Calendar.MINUTE, result.getMinute()); work.set(Calendar.HOUR_OF_DAY, result.getHour()); if (!work.after(after)) { // new date is not after current date, so bump after.add(Calendar.DAY_OF_MONTH, 1); result.setSecond(nextValue(secondsSet, 0)); result.setMinute(nextValue(minutesSet, 0)); result.setHour(nextValue(hoursSet, 0)); increaseAfterDayOfMonthSpecialOp(op.getOperator(), op.getDay(), op.getMonth(), isWeek, after); } return after.get(Calendar.DAY_OF_MONTH); } } else if (daysOfWeekSet == null) { dayOfMonth = nextValue(daysOfMonthSet, after.get(Calendar.DAY_OF_MONTH)); if (dayOfMonth != after.get(Calendar.DAY_OF_MONTH)) { result.setSecond(nextValue(secondsSet, 0)); result.setMinute(nextValue(minutesSet, 0)); result.setHour(nextValue(hoursSet, 0)); } if (dayOfMonth == -1) { dayOfMonth = nextValue(daysOfMonthSet, 0); after.add(Calendar.MONTH, 1); } } // If days of weeks is not a wildcard and days of month is a wildcard, go by days of week only else if (daysOfMonthSet == null) { // Loop to find the next day of month that works for the specified day of week values while (true) { dayOfMonth = after.get(Calendar.DAY_OF_MONTH); int dayOfWeek = after.get(Calendar.DAY_OF_WEEK) - 1; // If the day matches neither the day of month nor the day of week if (!daysOfWeekSet.contains(dayOfWeek)) { result.setSecond(nextValue(secondsSet, 0)); result.setMinute(nextValue(minutesSet, 0)); result.setHour(nextValue(hoursSet, 0)); after.add(Calendar.DAY_OF_MONTH, 1); } else { break; } } } // Both days of weeks and days of month are not a wildcard else { // Loop to find the next day of month that works for either day of month OR day of week while (true) { dayOfMonth = after.get(Calendar.DAY_OF_MONTH); int dayOfWeek = after.get(Calendar.DAY_OF_WEEK) - 1; // If the day matches neither the day of month nor the day of week if ((!daysOfWeekSet.contains(dayOfWeek)) && (!daysOfMonthSet.contains(dayOfMonth))) { result.setSecond(nextValue(secondsSet, 0)); result.setMinute(nextValue(minutesSet, 0)); result.setHour(nextValue(hoursSet, 0)); after.add(Calendar.DAY_OF_MONTH, 1); } else { break; } } } return dayOfMonth; }
From source file:com.autentia.intra.bean.activity.ObjectiveBean.java
/** * Move a date to one of its surrounding fridays. * * @param d the reference date//from ww w . j a v a2s . c om * @param inFuture whether to move to future/previous friday * @return the requested friday */ private Date moveToFriday(Date d, boolean inFuture) { Calendar cal = Calendar.getInstance(); cal.setTime(d); switch (cal.get(Calendar.DAY_OF_WEEK)) { case Calendar.MONDAY: cal.add(Calendar.DAY_OF_WEEK, inFuture ? 4 : -3); break; case Calendar.TUESDAY: cal.add(Calendar.DAY_OF_WEEK, inFuture ? 3 : -4); break; case Calendar.WEDNESDAY: cal.add(Calendar.DAY_OF_WEEK, inFuture ? 2 : -5); break; case Calendar.THURSDAY: cal.add(Calendar.DAY_OF_WEEK, inFuture ? 1 : -6); break; case Calendar.FRIDAY: cal.add(Calendar.DAY_OF_WEEK, inFuture ? 0 : -7); break; case Calendar.SATURDAY: cal.add(Calendar.DAY_OF_WEEK, inFuture ? 6 : -1); break; case Calendar.SUNDAY: cal.add(Calendar.DAY_OF_WEEK, inFuture ? 5 : -2); break; } return cal.getTime(); }
From source file:CalendarUtils.java
/** * Parses strings the way that CVS supports it... very human readable *///from w ww .j av a 2 s . c o m public static Calendar parse(String original, Locale locale) { //Get the symbol names DateFormatSymbols symbols = new DateFormatSymbols(locale); //Prep the string to parse String value = original.toLowerCase().trim(); //Get the current date/time Calendar now = Calendar.getInstance(); if (value.endsWith(" ago")) { //If this was a date that was "ago" the current time... //Strip out the ' ago' part value = value.substring(0, value.length() - 4); //Split the value and unit int start = value.indexOf(" "); if (start < 0) { throw new RuntimeException("Could not find space in between value and unit"); } String unit = value.substring(start + 1); value = value.substring(0, start); //We support "a week", so we need to parse the value as "a" int val = 0; if (value.equals("a") || value.equals("an")) { val = 1; } else { val = Integer.parseInt(value); } //Determine the unit if (unit.equals("milliseconds") || unit.equals("millisecond")) { now.add(Calendar.MILLISECOND, -val); } else if (unit.equals("seconds") || unit.equals("second")) { now.add(Calendar.SECOND, -val); } else if (unit.equals("minutes") || unit.equals("minute")) { now.add(Calendar.MINUTE, -val); } else if (unit.equals("hours") || unit.equals("hour")) { now.add(Calendar.HOUR, -val); } else if (unit.equals("days") || unit.equals("day")) { now.add(Calendar.DATE, -val); } else if (unit.equals("weeks") || unit.equals("week")) { now.add(Calendar.DATE, -val * 7); } else if (unit.equals("fortnights") || unit.equals("fortnight")) { now.add(Calendar.DATE, -val * 14); } else if (unit.equals("months") || unit.equals("month")) { now.add(Calendar.MONTH, -val); } else if (unit.equals("years") || unit.equals("year")) { now.add(Calendar.YEAR, -val); } else { throw new RuntimeException("We do not understand that many units ago"); } return now; } else if (value.startsWith("last ")) { //If this was the last time a certain field was met //Strip out the 'last ' part value = value.substring(5); //Get the current date/time String[] strings = symbols.getWeekdays(); for (int i = 0; i < strings.length; i++) { if (value.equalsIgnoreCase(strings[i])) { //How many days after Sunday int daysAgo = now.get(Calendar.DAY_OF_WEEK) - i; if (daysAgo <= 0) { daysAgo += 7; } now.add(Calendar.DATE, -daysAgo); return now; } } strings = symbols.getMonths(); for (int i = 0; i < strings.length; i++) { if (value.equalsIgnoreCase(strings[i])) { //How many days after January int monthsAgo = now.get(Calendar.MONTH) - i; if (monthsAgo <= 0) { monthsAgo += 12; } now.add(Calendar.MONTH, -monthsAgo); return now; } } if (value.equals("week")) { now.add(Calendar.DATE, -7); return now; } } else if (value.equals("yesterday")) { now.add(Calendar.DATE, -1); return now; } else if (value.equals("tomorrow")) { now.add(Calendar.DATE, 1); return now; } //Try to parse the date a number of different ways for (int i = 0; i < dateFormats.length; i++) { try { Date datetime = dateFormats[i].parse(original); Calendar cal = Calendar.getInstance(); cal.setTime(datetime); return cal; } catch (ParseException pe) { //we ignore this and just keep trying } } throw new RuntimeException("Unable to parse '" + original + "'."); }
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 va 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:de.ribeiro.android.gso.core.UntisProvider.java
/** * Synchronisiert alle verfgbaren WeekDatas im Stupid * * @param logger//w ww . j av a 2 s . c o m * @param selectedStringDate * @param selectedElement * @param myType * @param htmlResponse * @param stupid * @return * @throws Exception */ public static List<ICalEvent> syncWeekData(GregorianCalendar gc, String selectedElement, Type myType, HtmlResponse htmlResponse, Stupid stupid) throws Exception { List<ICalEvent> result = new ArrayList<ICalEvent>(); int currentDayOfWeek = gc.get(Calendar.DAY_OF_WEEK); // den currentDay auf den folge Montag setzen if (currentDayOfWeek < 2) { // 1000*60*60*24 = 1 Tag! gc.setTimeInMillis(gc.getTimeInMillis() + (1000 * 60 * 60 * 24 * (2 - currentDayOfWeek))); } if (currentDayOfWeek > 6) { // 1000*60*60*24 = 1 Tag! gc.setTimeInMillis(gc.getTimeInMillis() + (1000 * 60 * 60 * 24 * 2)); } String date = ConvertToStupidDateString(gc); String selectedType = myType.type; String selectedClassIndex = getIndexOfSelectorValue(myType.elementList, selectedElement); if (selectedClassIndex == "-1" || selectedType.equalsIgnoreCase("")) { throw new Exception(selectedElement + " kann nicht synchronisiert werden! " + selectedElement + " wurde nicht in der Liste der verfgbaren Elemente gefunden!"); } while (selectedClassIndex.length() < 3) { selectedClassIndex = "0" + selectedClassIndex; } WeekData weekData = new WeekData(stupid); try { // URL setzten URL url = new URL( "https://webuntis.stadt-koeln.de/WebUntis/Ical.do?school=K175055&ajaxCommand=renderTimetable&rpt_sd=" + date + "&type=" + 1 + "&elemId=" + selectedClassIndex + "&elemType=" + selectedType); htmlResponse.dataReceived = false; htmlResponse.xmlContent = XmlOPs.readFromURL(url, Const.CONNECTIONTIMEOUT); if (htmlResponse.xmlContent.length() < 50) { // ende return null; } _logger.Info("Week downloaded!"); List<ICalEvent> events = Ical.Parse(htmlResponse.xmlContent); weekData.syncTime = new GregorianCalendar().getTimeInMillis(); weekData.addParameter("syncTime", String.valueOf(weekData.syncTime)); weekData.date = (Calendar) gc.clone(); weekData.elementId = selectedElement; weekData.addParameter("classId", selectedElement); weekData.events = events; weekData.weekId = String.valueOf(gc.get(Calendar.WEEK_OF_YEAR)); weekData.addParameter("weekId", weekData.weekId); weekData.typeId = myType.type; weekData.addParameter("typeId", weekData.typeId); weekData.weekDataVersion = "2"; weekData.addParameter("weekDataVersion", weekData.weekDataVersion); // stupid.stupidData.add(wd); } catch (Exception e) { throw e; } // prfen, ob bereits die Woche fr die Klasse und den typ vorliegt: WeekData existWeekData = null; // alle bestehden Wochen abrufen: for (int y = 0; y < stupid.stupidData.size(); y++) { existWeekData = stupid.stupidData.get(y); // prfen, ob das bestehende Element, dem neu hinzuzufgenden // entspricht(klasse,KW,Typ) if (existWeekData.elementId.equalsIgnoreCase(weekData.elementId) && existWeekData.weekId.equalsIgnoreCase(weekData.weekId) && existWeekData.typeId.equalsIgnoreCase(weekData.typeId)) { // ja,es ist eine gleiche Woche bereits vorhanden // jedes event der neuen woche prfen, ob dieses schon existiert for (int ev = 0; ev < weekData.events.size(); ev++) { ICalEvent newevent = weekData.events.get(ev); // die schulstunde vom neuen event heraussuchen int schulstunde = GetSchulstundeOfEvent(newevent); // damit die schulstunde vom bestenden stundeplan abrufen ICalEvent existingevent = GetSchulstunde(existWeekData, newevent.DTSTART.get(Calendar.DAY_OF_WEEK), schulstunde); if (existingevent != null) newevent.UID = existingevent.UID; // beide schulstunden vergleichen if (compareEvents(existingevent, newevent)) { // unterschiede gefunden newevent.UID = "diff"; if (existingevent == null) _logger.Info("Es wurden Unterschiede in den Events gefunden: Neues Event " + newevent.DESCRIPTION + " , " + newevent.SUMMARY + " , kein altes Event vorhanden!"); else { _logger.Info("Es wurden Unterschiede in den Events gefunden: Neues Event " + newevent.DESCRIPTION + " , " + newevent.SUMMARY + " , Altes Event " + existingevent.DESCRIPTION + " , " + existingevent.SUMMARY); } // ber unterschiede benachrichtigen result.add(newevent); } // das existierende event aus dem datensatz lschen existWeekData.events.remove(existingevent); } for (ICalEvent event : existWeekData.events) { event.UID = "deleted"; weekData.events.add(event); } // alle verbleibenden events aus dem existierenden datensatz // sind ausgefallene stunden // existiert schon...ersetzen weekData.isDirty = true; stupid.stupidData.set(y, weekData); return SummarizeChanges(result); } } stupid.stupidData.add(weekData); // fgt die geparste Woche den // // Hauptdaten // // hinzu stupid.sort(); return SummarizeChanges(result); }
From source file:de.micromata.genome.util.types.DateUtils.java
/** * prfen, ob das gegebene Datum in bestimmten tagen liegt. * * @param date the date/*from w w w. j a va 2 s. co m*/ * @param dayList Kann <code>null</code> sein (liefert dann false als Ergebnis). Konstanten analog Klasse * <code>Calendar</code> * @return <code>true</code>, wenn das Datum in einem der spez. Tage liegt. */ public static boolean isInDayList(Date date, int... dayList) { Validate.notNull(date, "date not set"); final Calendar cal = getCalendarInstance(); cal.setTime(date); final int dayIndex = cal.get(Calendar.DAY_OF_WEEK); for (int day : dayList) { if (day == dayIndex) { return true; } } return false; }
From source file:de.austinpadernale.holidays.Holiday.java
public HolidayResult calculate(int year, Locale locale) { HolidayResult result = new HolidayResult(); result.setName(getName(locale));// www. j a v a2 s .c o 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; }