List of usage examples for java.util Calendar WEEK_OF_YEAR
int WEEK_OF_YEAR
To view the source code for java.util Calendar WEEK_OF_YEAR.
Click Source Link
get
and set
indicating the week number within the current year. From source file:DateUtils.java
/** * Parse date time value from given string resolving any functions or formulas * the string can contain. This method can be therefore used if the passed * string contains string representation date, time or timestamp or a formula * such as now + 3h - 1m + 4d. /*from w w w. j a v a2 s . c o m*/ * * @param strValue - string representation of date or date function * @param iDateType - date type code, one of the DATE_TYPE_XXX constants * @param stored - flag if Date should be parsed using format used for * storage or for display * @return Timestamp - parsed date or null if date was null * @throws OSSInvalidDataException - error during parsing */ public static Timestamp parseDateTime(String strValue, int iDateType, boolean stored) { Timestamp tsReturn = null; Calendar workCal = GregorianCalendar.getInstance(); if (strValue != null && strValue.length() > 0) { strValue = strValue.trim(); if (strValue.startsWith(CURRENT_DATE_CODE)) { strValue = strValue.replaceAll("[ ]", ""); // If the user specified "UseCurrent", then substitute the // current date/time in the value workCal.setTime(new Date()); // Log.getInstance().debug("Parsing current date " + strValue); // Parse the date math int iBeginIndex = CURRENT_DATE_CODE.length(); int iMaxLength = strValue.length(); int iSign = 1; int iNumberIndex; int iValue; char cChar = ' '; while (iBeginIndex < iMaxLength) { // This has to be sign if (strValue.charAt(iBeginIndex) == '+') { iSign = 1; } else if (strValue.charAt(iBeginIndex) == '-') { iSign = -1; } else { // Incorrect String throw new RuntimeException("Date function is in incorrect format: " + strValue + " at " + strValue.substring(iBeginIndex)); } iBeginIndex++; // Now we have to have number iNumberIndex = iBeginIndex; while (((iBeginIndex == iNumberIndex) || Character.isDigit(cChar)) && (iBeginIndex < iMaxLength)) { cChar = strValue.charAt(iBeginIndex++); } // We have to go one back because we should stop on modifier (e.g 1m) iBeginIndex--; try { iValue = Integer.parseInt(strValue.substring(iNumberIndex, iBeginIndex)); } catch (NumberFormatException nmeExc) { // Incorrect String throw new RuntimeException("Date function is in incorrect format: " + strValue + " at " + strValue.substring(iNumberIndex)); } // This has to be modifier: y - year, M - month, w - week, // d - day, h - hour, m - minute, s - second cChar = strValue.charAt(iBeginIndex); switch (cChar) { case (YEAR_CODE): { if (iDateType == DATE_TYPE_TIME) { throw new RuntimeException( "Date function is in incorrect format: " + "used YEAR modifier for TIME type"); } workCal.add(Calendar.YEAR, iSign * iValue); break; } case (MONTH_CODE): { if (iDateType == DATE_TYPE_TIME) { throw new RuntimeException( "Date function is in incorrect format: " + "used MONTH modifier for TIME type"); } workCal.add(Calendar.MONTH, iSign * iValue); break; } case (WEEK_CODE): { if (iDateType == DATE_TYPE_TIME) { throw new RuntimeException( "Date function is in incorrect format: " + "used WEEK modifier for TIME type"); } workCal.add(Calendar.WEEK_OF_YEAR, iSign * iValue); break; } case (DAY_CODE): { if (iDateType == DATE_TYPE_TIME) { throw new RuntimeException( "Date function is in incorrect format: " + "used DAY modifier for TIME type"); } workCal.add(Calendar.DATE, iSign * iValue); break; } case (HOUR_CODE): { if (iDateType == DATE_TYPE_DATE) { throw new RuntimeException( "Date function is in incorrect format: " + "used HOUR modifier for DATE type"); } workCal.add(Calendar.HOUR, iSign * iValue); break; } case (MINUTE_CODE): { if (iDateType == DATE_TYPE_DATE) { throw new RuntimeException("Date function is in incorrect format: " + "used MINUTE modifier for DATE type"); } workCal.add(Calendar.MINUTE, iSign * iValue); break; } case (SECOND_CODE): { if (iDateType == DATE_TYPE_DATE) { throw new RuntimeException("Date function is in incorrect format: " + "used SECOND modifier for DATE type"); } workCal.add(Calendar.SECOND, iSign * iValue); break; } default: { // Incorrect String throw new RuntimeException("Date function is in incorrect format: " + strValue + " at " + strValue.substring(iBeginIndex)); } } iBeginIndex++; } tsReturn = new Timestamp(workCal.getTimeInMillis()); } else { try { if (stored) { switch (iDateType) { case (DATE_TYPE_DATE): { tsReturn = new Timestamp(DATE_STORE_FORMAT.parse(strValue).getTime()); break; } case (DATE_TYPE_TIME): { tsReturn = new Timestamp(TIME_STORE_FORMAT.parse(strValue).getTime()); break; } case (DATE_TYPE_DATETIME): { tsReturn = new Timestamp(DATETIME_STORE_FORMAT.parse(strValue).getTime()); break; } default: { assert false : "Unknown date type " + iDateType; } } } else { switch (iDateType) { case (DATE_TYPE_DATE): { tsReturn = new Timestamp(DATE_FORMAT.parse(strValue).getTime()); break; } case (DATE_TYPE_TIME): { tsReturn = new Timestamp(TIME_FORMAT.parse(strValue).getTime()); break; } case (DATE_TYPE_DATETIME): { tsReturn = new Timestamp(DATETIME_FORMAT.parse(strValue).getTime()); break; } default: { assert false : "Unknown date type " + iDateType; } } } } catch (ParseException peExc) { throw new RuntimeException("Date is in incorrect format. Problems with parsing.", peExc); } } } return tsReturn; }
From source file:com.discovery.darchrow.date.DateUtil.java
/** * ? ({@link Calendar#WEEK_OF_YEAR}?,??),,. * //from w w w . j av a 2 s.c om * <pre> * Example 1: * addWeek(2012-06-29 00:45:18,5) * return 2012-08-03 00:45:18 * * Example 2: * addWeek(2012-06-29 00:45:18,-5) * return 2012-05-25 00:45:18 * </pre> * * @param date * * @param week * ??,<span style="color:red">?</span>,??<br> * @return ? * @see #operateDate(Date, int, int) * @see Calendar#WEEK_OF_YEAR */ public static Date addWeek(Date date, int week) { return operateDate(date, Calendar.WEEK_OF_YEAR, week); }
From source file:DateUtils.java
/** * Parse date time value from given string resolving any functions or formulas * the string can contain. This method can be therefore used if the passed * string contains string representation date, time or timestamp or a formula * such as now + 3h - 1m + 4d. // ww w . j a va 2s .co m * * @param strValue - string representation of date or date function * @param iDateType - date type code, one of the DATE_TYPE_XXX constants * @param stored - flag if Date should be parsed using format used for * storage or for display * @return Timestamp - parsed date or null if date was null * @throws OSSInvalidDataException - error during parsing */ public static Timestamp parseDateTime(String strValue, int iDateType, boolean stored) throws Exception { Timestamp tsReturn = null; Calendar workCal = GregorianCalendar.getInstance(); if (strValue != null && strValue.length() > 0) { strValue = strValue.trim(); if (strValue.startsWith(CURRENT_DATE_CODE)) { strValue = strValue.replaceAll("[ ]", ""); // If the user specified "UseCurrent", then substitute the // current date/time in the value workCal.setTime(new Date()); // Log.getInstance().debug("Parsing current date " + strValue); // Parse the date math int iBeginIndex = CURRENT_DATE_CODE.length(); int iMaxLength = strValue.length(); int iSign = 1; int iNumberIndex; int iValue; char cChar = ' '; while (iBeginIndex < iMaxLength) { // This has to be sign if (strValue.charAt(iBeginIndex) == '+') { iSign = 1; } else if (strValue.charAt(iBeginIndex) == '-') { iSign = -1; } else { // Incorrect String throw new Exception("Date function is in incorrect format: " + strValue + " at " + strValue.substring(iBeginIndex)); } iBeginIndex++; // Now we have to have number iNumberIndex = iBeginIndex; while (((iBeginIndex == iNumberIndex) || Character.isDigit(cChar)) && (iBeginIndex < iMaxLength)) { cChar = strValue.charAt(iBeginIndex++); } // We have to go one back because we should stop on modifier (e.g 1m) iBeginIndex--; try { iValue = Integer.parseInt(strValue.substring(iNumberIndex, iBeginIndex)); } catch (NumberFormatException nmeExc) { // Incorrect String throw new Exception("Date function is in incorrect format: " + strValue + " at " + strValue.substring(iNumberIndex)); } // This has to be modifier: y - year, M - month, w - week, // d - day, h - hour, m - minute, s - second cChar = strValue.charAt(iBeginIndex); switch (cChar) { case (YEAR_CODE): { if (iDateType == DATE_TYPE_TIME) { throw new Exception( "Date function is in incorrect format: " + "used YEAR modifier for TIME type"); } workCal.add(Calendar.YEAR, iSign * iValue); break; } case (MONTH_CODE): { if (iDateType == DATE_TYPE_TIME) { throw new Exception( "Date function is in incorrect format: " + "used MONTH modifier for TIME type"); } workCal.add(Calendar.MONTH, iSign * iValue); break; } case (WEEK_CODE): { if (iDateType == DATE_TYPE_TIME) { throw new Exception( "Date function is in incorrect format: " + "used WEEK modifier for TIME type"); } workCal.add(Calendar.WEEK_OF_YEAR, iSign * iValue); break; } case (DAY_CODE): { if (iDateType == DATE_TYPE_TIME) { throw new Exception( "Date function is in incorrect format: " + "used DAY modifier for TIME type"); } workCal.add(Calendar.DATE, iSign * iValue); break; } case (HOUR_CODE): { if (iDateType == DATE_TYPE_DATE) { throw new Exception( "Date function is in incorrect format: " + "used HOUR modifier for DATE type"); } workCal.add(Calendar.HOUR, iSign * iValue); break; } case (MINUTE_CODE): { if (iDateType == DATE_TYPE_DATE) { throw new Exception("Date function is in incorrect format: " + "used MINUTE modifier for DATE type"); } workCal.add(Calendar.MINUTE, iSign * iValue); break; } case (SECOND_CODE): { if (iDateType == DATE_TYPE_DATE) { throw new Exception("Date function is in incorrect format: " + "used SECOND modifier for DATE type"); } workCal.add(Calendar.SECOND, iSign * iValue); break; } default: { // Incorrect String throw new Exception("Date function is in incorrect format: " + strValue + " at " + strValue.substring(iBeginIndex)); } } iBeginIndex++; } tsReturn = new Timestamp(workCal.getTimeInMillis()); } else { try { if (stored) { switch (iDateType) { case (DATE_TYPE_DATE): { tsReturn = new Timestamp(DATE_STORE_FORMAT.parse(strValue).getTime()); break; } case (DATE_TYPE_TIME): { tsReturn = new Timestamp(TIME_STORE_FORMAT.parse(strValue).getTime()); break; } case (DATE_TYPE_DATETIME): { tsReturn = new Timestamp(DATETIME_STORE_FORMAT.parse(strValue).getTime()); break; } default: { assert false : "Unknown date type " + iDateType; } } } else { switch (iDateType) { case (DATE_TYPE_DATE): { tsReturn = new Timestamp(DATE_FORMAT.parse(strValue).getTime()); break; } case (DATE_TYPE_TIME): { tsReturn = new Timestamp(TIME_FORMAT.parse(strValue).getTime()); break; } case (DATE_TYPE_DATETIME): { tsReturn = new Timestamp(DATETIME_FORMAT.parse(strValue).getTime()); break; } default: { assert false : "Unknown date type " + iDateType; } } } } catch (ParseException peExc) { throw new Exception("Date is in incorrect format. Problems with parsing.", peExc); } } } return tsReturn; }
From source file:me.StevenLawson.TotalFreedomMod.TFM_Util.java
public static Date parseDateOffset(String time) { Pattern timePattern = Pattern.compile("(?:([0-9]+)\\s*y[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*mo[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*w[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*d[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*h[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*m[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*(?:s[a-z]*)?)?", Pattern.CASE_INSENSITIVE); Matcher m = timePattern.matcher(time); int years = 0; int months = 0; int weeks = 0; int days = 0; int hours = 0; int minutes = 0; int seconds = 0; boolean found = false; while (m.find()) { if (m.group() == null || m.group().isEmpty()) { continue; }/*from w w w.ja v a2s . c o m*/ for (int i = 0; i < m.groupCount(); i++) { if (m.group(i) != null && !m.group(i).isEmpty()) { found = true; break; } } if (found) { if (m.group(1) != null && !m.group(1).isEmpty()) { years = Integer.parseInt(m.group(1)); } if (m.group(2) != null && !m.group(2).isEmpty()) { months = Integer.parseInt(m.group(2)); } if (m.group(3) != null && !m.group(3).isEmpty()) { weeks = Integer.parseInt(m.group(3)); } if (m.group(4) != null && !m.group(4).isEmpty()) { days = Integer.parseInt(m.group(4)); } if (m.group(5) != null && !m.group(5).isEmpty()) { hours = Integer.parseInt(m.group(5)); } if (m.group(6) != null && !m.group(6).isEmpty()) { minutes = Integer.parseInt(m.group(6)); } if (m.group(7) != null && !m.group(7).isEmpty()) { seconds = Integer.parseInt(m.group(7)); } break; } } if (!found) { return null; } Calendar c = new GregorianCalendar(); if (years > 0) { c.add(Calendar.YEAR, years); } if (months > 0) { c.add(Calendar.MONTH, months); } if (weeks > 0) { c.add(Calendar.WEEK_OF_YEAR, weeks); } if (days > 0) { c.add(Calendar.DAY_OF_MONTH, days); } if (hours > 0) { c.add(Calendar.HOUR_OF_DAY, hours); } if (minutes > 0) { c.add(Calendar.MINUTE, minutes); } if (seconds > 0) { c.add(Calendar.SECOND, seconds); } return c.getTime(); }
From source file:DateUtil.java
/** * Advances the internal cursor <code>step</code> units of Period * <code>p</code> in time. Note that for MONTH and QUARTER, this works out to * 1 and 3 months respectively, as defined by the Calendar class and based on * the current cursor, not precisely MONTH_MS or QUARTER_MS milliseconds. * /* w ww.j av a 2s . c om*/ * @param p * The DateUtil.Period unit. * @param step * The number of Period units to advance. */ public void advance(Period p, int step) { copyDate(mCursor, mBase); switch (p) { case YEAR: mCal.setTimeInMillis(mCursor.mMillis); mCal.add(Calendar.YEAR, step); break; case QUARTER: mCal.setTimeInMillis(mCursor.mMillis); mCal.add(Calendar.MONTH, step * 3); break; case MONTH: mCal.setTimeInMillis(mCursor.mMillis); mCal.add(Calendar.MONTH, step); break; case WEEK: mCal.setTimeInMillis(mCursor.mMillis); mCal.add(Calendar.WEEK_OF_YEAR, step); break; case DAY: mCal.setTimeInMillis(mCursor.mMillis); mCal.add(Calendar.DAY_OF_MONTH, step); break; case HOUR: mCal.setTimeInMillis(mCursor.mMillis); mCal.add(Calendar.HOUR_OF_DAY, step); break; case MINUTE: default: mCal.setTimeInMillis(mCursor.mMillis); mCal.add(Calendar.MINUTE, step); break; } mCursor.mMillis = mCal.getTimeInMillis(); millisToComponent(mCursor); return; }
From source file:com.jungle.base.utils.MiscUtils.java
public static boolean isSameWeek(long timestamp) { Calendar calendar = Calendar.getInstance(); Calendar today = Calendar.getInstance(); calendar.setTimeInMillis(timestamp); today.setTimeInMillis(System.currentTimeMillis()); return calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) && calendar.get(Calendar.WEEK_OF_YEAR) == today.get(Calendar.WEEK_OF_YEAR); }
From source file:com.pr7.logging.CustomDailyRollingFileAppender.java
public Date getNextCheckDate(Date now) { this.setTime(now); switch (type) { case CustomDailyRollingFileAppender.TOP_OF_MINUTE: this.set(Calendar.SECOND, 0); this.set(Calendar.MILLISECOND, 0); this.add(Calendar.MINUTE, 1); break;/*from w ww . ja v a2s. com*/ case CustomDailyRollingFileAppender.TOP_OF_HOUR: this.set(Calendar.MINUTE, 0); this.set(Calendar.SECOND, 0); this.set(Calendar.MILLISECOND, 0); this.add(Calendar.HOUR_OF_DAY, 1); break; case CustomDailyRollingFileAppender.HALF_DAY: this.set(Calendar.MINUTE, 0); this.set(Calendar.SECOND, 0); this.set(Calendar.MILLISECOND, 0); int hour = get(Calendar.HOUR_OF_DAY); if (hour < 12) { this.set(Calendar.HOUR_OF_DAY, 12); } else { this.set(Calendar.HOUR_OF_DAY, 0); this.add(Calendar.DAY_OF_MONTH, 1); } break; case CustomDailyRollingFileAppender.TOP_OF_DAY: this.set(Calendar.HOUR_OF_DAY, 0); this.set(Calendar.MINUTE, 0); this.set(Calendar.SECOND, 0); this.set(Calendar.MILLISECOND, 0); this.add(Calendar.DATE, 1); break; case CustomDailyRollingFileAppender.TOP_OF_WEEK: this.set(Calendar.DAY_OF_WEEK, getFirstDayOfWeek()); this.set(Calendar.HOUR_OF_DAY, 0); this.set(Calendar.MINUTE, 0); this.set(Calendar.SECOND, 0); this.set(Calendar.MILLISECOND, 0); this.add(Calendar.WEEK_OF_YEAR, 1); break; case CustomDailyRollingFileAppender.TOP_OF_MONTH: this.set(Calendar.DATE, 1); this.set(Calendar.HOUR_OF_DAY, 0); this.set(Calendar.MINUTE, 0); this.set(Calendar.SECOND, 0); this.set(Calendar.MILLISECOND, 0); this.add(Calendar.MONTH, 1); break; default: throw new IllegalStateException("Unknown periodicity type."); } return getTime(); }
From source file:DateUtil.java
/** * Some external entities still use Calendar.* fields to do some of their own * date calculations, so this provides a mapping from DateUtil.*_MS to the * closest Calendar.* field. Note that if the milliseconds is not one of the * DateUtil constants, the smallest known field will be returned. * //from w w w . j a v a 2 s . c o m * @param millis * The DateUtil.*_MS field to map from. * @return The int representing the closest Calendar.* field. */ public static int mapLongToCal(long millis) { if (millis == YEAR_MS) return Calendar.YEAR; else if (millis == QUARTER_MS) return Calendar.MONTH; // There is no Calendar.QUARTER, return MONTH else if (millis == MONTH_MS) return Calendar.MONTH; else if (millis == WEEK_MS) return Calendar.WEEK_OF_YEAR; else if (millis == DAY_MS) return Calendar.DAY_OF_MONTH; else if (millis == AMPM_MS) return Calendar.AM_PM; else if (millis == HOUR_MS) return Calendar.HOUR_OF_DAY; return Calendar.MINUTE; }
From source file:com.feilong.commons.core.date.DateUtil.java
/** * ?, 1,52(365/7=52.14)<br>/*w ww.j a v a 2 s . co m*/ * {@link Calendar#WEEK_OF_YEAR} * <p> * ?:<br> * 20141-1 1-2 1-3 1-4 WEEK_OF_YEAR 1; <br> * 201412-28 12-29 12-30 12-31 WEEK_OF_YEAR 1 * * <pre> * * Example 1: * 2014-06-03 * return 23 * * Example 2: * 2014-01-01 * return 1 * * Example 3: * 2014-12-29 * return 23 * * Example 4: * 2014-12-20 * return 51 * * Example 5: * 2014-12-26 * return 52 * </pre> * * {@link Calendar#setMinimalDaysInFirstWeek(int)} ???,7? * * <pre> * * Example 1: * 2014-01-01 * return 52 * * Example 3: * 2014-12-31 * return 52 * </pre> * * ?,11??,?7,11? * * @param date * the date * @return ? * @see CalendarUtil#getCalendarFieldValue(Date, int) * @see Calendar#WEEK_OF_YEAR * @see Calendar#getFirstDayOfWeek() * @see Calendar#getMinimalDaysInFirstWeek() * @see Calendar#setMinimalDaysInFirstWeek(int) * @since 1.0.7 */ public static final int getWeekOfYear(Date date) { // Calendar calendar = DateUtil.toCalendar(date); // calendar.setMinimalDaysInFirstWeek(7); // return calendar.get(Calendar.WEEK_OF_YEAR); return CalendarUtil.getCalendarFieldValue(date, Calendar.WEEK_OF_YEAR); }
From source file:com.frey.repo.DateUtil.java
/***************************************** * @ ??//from w w w. ja v a 2s . c o m ****************************************/ public static String getYearWeekFirstDay(Date curDate) { Calendar cal = Calendar.getInstance(); cal.setTime(curDate); if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { cal.set(Calendar.WEEK_OF_YEAR, cal.get(Calendar.WEEK_OF_YEAR) - 1); } else { cal.set(Calendar.WEEK_OF_YEAR, cal.get(Calendar.WEEK_OF_YEAR)); } cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); String tempYear = Integer.toString(cal.get(Calendar.YEAR)); String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1); String tempDay = Integer.toString(cal.get(Calendar.DATE)); String tempDate = tempYear + "-" + tempMonth + "-" + tempDay; return setDateFormat(tempDate, "yyyy-MM-dd"); }