Java Code Examples for java.util.Calendar#YEAR
The following examples show how to use
java.util.Calendar#YEAR .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateMatch.java From OsmGo with MIT License | 6 votes |
/** * Postpone trigger if first schedule matches the past */ private long postponeTriggerIfNeeded(Calendar current, Calendar next) { int currentYear = current.get(Calendar.YEAR); if (matchesUnit(Calendar.YEAR, current, next)) { next.set(Calendar.YEAR, currentYear + 1); this.unit = Calendar.YEAR; } else if (matchesUnit(Calendar.MONTH, current, next)) { next.set(Calendar.YEAR, currentYear + 1); this.unit = Calendar.MONTH; } else if (matchesUnit(Calendar.DAY_OF_MONTH, current, next)) { next.set(Calendar.MONTH, current.get(Calendar.MONTH) + 1); this.unit = Calendar.DAY_OF_MONTH; } else if (matchesUnit(Calendar.HOUR_OF_DAY, current, next)) { next.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH) + 1); this.unit = Calendar.DAY_OF_MONTH; } else if (matchesUnit(Calendar.MINUTE, current, next)) { next.set(Calendar.HOUR_OF_DAY, current.get(Calendar.HOUR_OF_DAY) + 1); this.unit = Calendar.MINUTE; } return next.getTimeInMillis(); }
Example 2
Source File: SearchDateConversion.java From alfresco-data-model with GNU Lesser General Public License v3.0 | 6 votes |
/** * @param dateAndResolution * @return String date */ public static String getDateStart(Pair<Date, Integer> dateAndResolution) { Calendar cal = Calendar.getInstance(I18NUtil.getLocale()); cal.setTime(dateAndResolution.getFirst()); switch (dateAndResolution.getSecond()) { case Calendar.YEAR: cal.set(Calendar.MONTH, cal.getActualMinimum(Calendar.MONTH)); case Calendar.MONTH: cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH)); case Calendar.DAY_OF_MONTH: cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY)); case Calendar.HOUR_OF_DAY: cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE)); case Calendar.MINUTE: cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND)); case Calendar.SECOND: cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND)); case Calendar.MILLISECOND: default: } SimpleDateFormat formatter = CachingDateFormat.getSolrDatetimeFormat(); formatter.setTimeZone(UTC_TIMEZONE); return formatter.format(cal.getTime()); }
Example 3
Source File: Cal.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Adds the given amount of time to the given calendar field. * * @param field the calendar field. * @param amount the amount of time. */ public Cal add( int field, int amount ) { switch ( field ) { case Calendar.YEAR: dateTimeUnit = getCalendar().plusYears( dateTimeUnit, amount ); case Calendar.MONTH: dateTimeUnit = getCalendar().plusMonths( dateTimeUnit, amount ); case Calendar.DAY_OF_MONTH: case Calendar.DAY_OF_YEAR: dateTimeUnit = getCalendar().plusDays( dateTimeUnit, amount ); break; default: throw new UnsupportedOperationException(); } return this; }
Example 4
Source File: DateUtilsRoundingTest.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Tests DateUtils.round()-method with Calendar.Year * * @throws Exception * @since 3.0 */ public void testRoundYear() throws Exception { final int calendarField = Calendar.YEAR; Date roundedUpDate = dateTimeParser.parse("January 1, 2008 0:00:00.000"); Date roundedDownDate = targetYearDate; Date lastRoundedDownDate = dateTimeParser.parse("June 30, 2007 23:59:59.999"); baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); }
Example 5
Source File: DateUtilsRoundingTest.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Tests DateUtils.round()-method with Calendar.Year * * @throws Exception * @since 3.0 */ @Test public void testRoundYear() throws Exception { final int calendarField = Calendar.YEAR; Date roundedUpDate = dateTimeParser.parse("January 1, 2008 0:00:00.000"); Date roundedDownDate = targetYearDate; Date lastRoundedDownDate = dateTimeParser.parse("June 30, 2007 23:59:59.999"); baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); }
Example 6
Source File: DateUtilsRoundingTest.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Test DateUtils.truncate()-method with Calendar.YEAR * * @throws Exception * @since 3.0 */ @Test public void testTruncateYear() throws Exception { final int calendarField = Calendar.YEAR; final Date lastTruncateDate = dateTimeParser.parse("December 31, 2007 23:59:59.999"); baseTruncateTest(targetYearDate, lastTruncateDate, calendarField); }
Example 7
Source File: DateTimeUtils.java From Deadline with GNU General Public License v3.0 | 5 votes |
public static String getCurrentDateTimeName(int field) { Calendar calendar = Calendar.getInstance(); if (field == Calendar.YEAR) { return String.valueOf(calendar.get(Calendar.YEAR)); } else { return calendar.getDisplayName(field, Calendar.SHORT, Locale.getDefault()); } }
Example 8
Source File: AlarmHandler.java From openScale with GNU General Public License v3.0 | 5 votes |
private static boolean isSameDate(Calendar c1, Calendar c2) { int[] dateFields = {Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH}; for (int dateField : dateFields) { if (c1.get(dateField) != c2.get(dateField)) return false; } return true; }
Example 9
Source File: Display.java From joinery with GNU General Public License v3.0 | 5 votes |
private static final String dateFormat(final List<Object> xdata) { final int[] fields = new int[] { Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND }; final String[] formats = new String[] { " yyy", "-MMM", "-d", " H", ":mm", ":ss" }; final Calendar c1 = Calendar.getInstance(), c2 = Calendar.getInstance(); if (!xdata.isEmpty() && xdata.get(0) instanceof Date) { String format = ""; int first = 0, last = 0; c1.setTime(Date.class.cast(xdata.get(0))); // iterate over all x-axis values comparing dates for (int i = 1; i < xdata.size(); i++) { // early exit for non-date elements if (!(xdata.get(i) instanceof Date)) return formats[0].substring(1); c2.setTime(Date.class.cast(xdata.get(i))); // check which components differ, those are the fields to output for (int j = 1; j < fields.length; j++) { if (c1.get(fields[j]) != c2.get(fields[j])) { first = Math.max(j - 1, first); last = Math.max(j, last); } } } // construct a format string for the fields that differ for (int i = first; i <= last && i < formats.length; i++) { format += format.isEmpty() ? formats[i].substring(1) : formats[i]; } return format; } return formats[0].substring(1); }
Example 10
Source File: TimeUtils.java From zstack with Apache License 2.0 | 5 votes |
public static boolean equalApproximately(Calendar c1, Calendar c2, int roundOffTimeUnit) { for (int i = roundOffTimeUnit; i >= Calendar.YEAR; i--) { if (c1.get(i) != c2.get(i)) { return false; } } return true; }
Example 11
Source File: DateUtilsRoundingTest.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Tests DateUtils.round()-method with Calendar.Year * * @throws Exception * @since 3.0 */ @Test public void testRoundYear() throws Exception { final int calendarField = Calendar.YEAR; Date roundedUpDate = dateTimeParser.parse("January 1, 2008 0:00:00.000"); Date roundedDownDate = targetYearDate; Date lastRoundedDownDate = dateTimeParser.parse("June 30, 2007 23:59:59.999"); baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); }
Example 12
Source File: DateBuilder.java From lams with GNU General Public License v2.0 | 5 votes |
private static int translate(IntervalUnit unit) { switch(unit) { case DAY : return Calendar.DAY_OF_YEAR; case HOUR : return Calendar.HOUR_OF_DAY; case MINUTE : return Calendar.MINUTE; case MONTH : return Calendar.MONTH; case SECOND : return Calendar.SECOND; case MILLISECOND : return Calendar.MILLISECOND; case WEEK : return Calendar.WEEK_OF_YEAR; case YEAR : return Calendar.YEAR; default : throw new IllegalArgumentException("Unknown IntervalUnit"); } }
Example 13
Source File: Footer.java From nebula with Eclipse Public License 2.0 | 4 votes |
public static Footer Today() { return new Footer(TODAY, Calendar.YEAR, Calendar.MONTH, Calendar.DATE); }
Example 14
Source File: TimeManagerTest.java From drftpd with GNU General Public License v2.0 | 4 votes |
public void resetYear(Date d) { _lastReset = Calendar.YEAR; }
Example 15
Source File: DateTimeFormatter.java From nebula with Eclipse Public License 2.0 | 4 votes |
/** * Compiles a given edit pattern, initializing <code>inputMask</code> and * <code>inputCache</code>. The content of the edit pattern is analyzed char * by char and the array of field descriptors is initialized. * Pattern chars allowed are : y, M, d, H, h, s, S, a. * The presence of other pattern chars defined in <code>SimpleDateFormat</code> * will raised an <code>IllegalArgumentException</code>. * * @param editPattern edit pattern * @throws IllegalArgumentException pattern is invalid */ private void compile(String editPattern) { inputMask = new StringBuffer(); inputCache = new StringBuffer(); fields = new FieldDesc[10]; int fi = 0; int length = editPattern.length(); for (int i = 0; i < length; i++) { char c = editPattern.charAt(i); int l = 1; while ( i < length - 1 && editPattern.charAt(i + 1) == c ) { i++; l++; } isValidCharPattern(c); switch ( c ) { case 'y' : // Year fields[fi] = new FieldDesc(); fields[fi].field = Calendar.YEAR; fields[fi].minLen = fields[fi].maxLen = l <= 2 ? 2 : 4; if ( fields[fi].maxLen == 2 ) { yearStart = -1; } break; case 'M' : // Month fields[fi] = new FieldDesc(); fields[fi].field = Calendar.MONTH; fields[fi].minLen = Math.min(l, 2); fields[fi].maxLen = 2; break; case 'd' : // Day in month fields[fi] = new FieldDesc(); fields[fi].field = Calendar.DAY_OF_MONTH; fields[fi].minLen = Math.min(l, 2); fields[fi].maxLen = 2; break; case 'H' : // Hour (0-23) fields[fi] = new FieldDesc(); fields[fi].field = Calendar.HOUR_OF_DAY; fields[fi].minLen = Math.min(l, 2); fields[fi].maxLen = 2; break; case 'h' : // Hour (1-12 AM-PM) fields[fi] = new FieldDesc(); fields[fi].field = Calendar.HOUR; fields[fi].minLen = Math.min(l, 2); fields[fi].maxLen = 2; break; case 'm' : // Minutes fields[fi] = new FieldDesc(); fields[fi].field = Calendar.MINUTE; fields[fi].minLen = Math.min(l, 2); fields[fi].maxLen = 2; break; case 's' : // Seconds fields[fi] = new FieldDesc(); fields[fi].field = Calendar.SECOND; fields[fi].minLen = Math.min(l, 2); fields[fi].maxLen = 2; break; case 'S' : // Milliseconds fields[fi] = new FieldDesc(); fields[fi].field = Calendar.MILLISECOND; fields[fi].minLen = Math.min(l, 3); fields[fi].maxLen = 3; break; case 'a' : // AM-PM marker fields[fi] = new FieldDesc(); fields[fi].field = Calendar.AM_PM; fields[fi].minLen = fields[fi].maxLen = 2; break; default : for (int j = 0; j < l; j++) { inputMask.append('*'); inputCache.append(c); } continue; } fields[fi].empty = true; fields[fi].valid = false; calendar.clear(fields[fi].field); char k = (char) ('0' + fi); for (int j = 0; j < fields[fi].minLen; j++) { inputMask.append(k); inputCache.append(SPACE); } fields[fi].index = k; fi++; } fieldCount = fi; }
Example 16
Source File: IndexNameHelper.java From phoebus with Eclipse Public License 1.0 | 4 votes |
private void setDateSpanStartAndEnd(Instant time) { Calendar calendar = new GregorianCalendar(); calendar.setTimeInMillis(time.toEpochMilli()); calendar.setFirstDayOfWeek(Calendar.SUNDAY); Integer dateSpanField = -1; if (dateSpanUnit.equals(YEAR)) { // Roll the year back to the beginning (midnight of the first of the year). dateSpanField = Calendar.YEAR; calendar.set(Calendar.MONTH, Calendar.JANUARY); calendar.set(Calendar.WEEK_OF_MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 1); } if (dateSpanUnit.equals(MONTH)) { // Roll the month back to the beginning (midnight of the first of the month). dateSpanField = Calendar.MONTH; calendar.set(Calendar.WEEK_OF_MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 1); } if (dateSpanUnit.equals(WEEK)) { // Roll the week back to the beginning (midnight Sunday). dateSpanField = Calendar.WEEK_OF_YEAR; calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); } if (dateSpanUnit.equals(DAY)) dateSpanField = Calendar.DATE; // Roll the day back to midnight calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); spanStart = calendar.toInstant(); calendar.add(dateSpanField, dateSpanValue); spanEnd = calendar.toInstant(); }
Example 17
Source File: Lang_21_DateUtils_t.java From coming with MIT License | 4 votes |
/** * Calendar-version for fragment-calculation in any unit * * @param calendar the calendar to work with, not null * @param fragment the Calendar field part of calendar to calculate * @param unit Calendar field defining the unit * @return number of units within the fragment of the calendar * @throws IllegalArgumentException if the date is <code>null</code> or * fragment is not supported * @since 2.4 */ private static long getFragment(Calendar calendar, int fragment, int unit) { if(calendar == null) { throw new IllegalArgumentException("The date must not be null"); } long millisPerUnit = getMillisPerUnit(unit); long result = 0; // Fragments bigger than a day require a breakdown to days switch (fragment) { case Calendar.YEAR: result += (calendar.get(Calendar.DAY_OF_YEAR) * MILLIS_PER_DAY) / millisPerUnit; break; case Calendar.MONTH: result += (calendar.get(Calendar.DAY_OF_MONTH) * MILLIS_PER_DAY) / millisPerUnit; break; } switch (fragment) { // Number of days already calculated for these cases case Calendar.YEAR: case Calendar.MONTH: // The rest of the valid cases case Calendar.DAY_OF_YEAR: case Calendar.DATE: result += (calendar.get(Calendar.HOUR_OF_DAY) * MILLIS_PER_HOUR) / millisPerUnit; //$FALL-THROUGH$ case Calendar.HOUR_OF_DAY: result += (calendar.get(Calendar.MINUTE) * MILLIS_PER_MINUTE) / millisPerUnit; //$FALL-THROUGH$ case Calendar.MINUTE: result += (calendar.get(Calendar.SECOND) * MILLIS_PER_SECOND) / millisPerUnit; //$FALL-THROUGH$ case Calendar.SECOND: result += (calendar.get(Calendar.MILLISECOND) * 1) / millisPerUnit; break; case Calendar.MILLISECOND: break;//never useful default: throw new IllegalArgumentException("The fragment " + fragment + " is not supported"); } return result; }
Example 18
Source File: FinancialOctPeriodGeneratorShould.java From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License | 4 votes |
public FinancialOctPeriodGeneratorShould() { super(PeriodType.FinancialOct, Calendar.YEAR); }
Example 19
Source File: DateUtils.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Gets a Calendar fragment for any unit. * * @param calendar the calendar to work with, not null * @param fragment the Calendar field part of calendar to calculate * @param unit the {@code Calendar} field defining the unit * @return number of units within the fragment of the calendar * @throws IllegalArgumentException if the date is <code>null</code> or * fragment is not supported * @since 2.4 */ private static long getFragment(Calendar calendar, int fragment, int unit) { if(calendar == null) { throw new IllegalArgumentException("The date must not be null"); } long millisPerUnit = getMillisPerUnit(unit); long result = 0; // Fragments bigger than a day require a breakdown to days switch (fragment) { case Calendar.YEAR: result += (calendar.get(Calendar.DAY_OF_YEAR) * MILLIS_PER_DAY) / millisPerUnit; break; case Calendar.MONTH: result += (calendar.get(Calendar.DAY_OF_MONTH) * MILLIS_PER_DAY) / millisPerUnit; break; } switch (fragment) { // Number of days already calculated for these cases case Calendar.YEAR: case Calendar.MONTH: // The rest of the valid cases case Calendar.DAY_OF_YEAR: case Calendar.DATE: result += (calendar.get(Calendar.HOUR_OF_DAY) * MILLIS_PER_HOUR) / millisPerUnit; //$FALL-THROUGH$ case Calendar.HOUR_OF_DAY: result += (calendar.get(Calendar.MINUTE) * MILLIS_PER_MINUTE) / millisPerUnit; //$FALL-THROUGH$ case Calendar.MINUTE: result += (calendar.get(Calendar.SECOND) * MILLIS_PER_SECOND) / millisPerUnit; //$FALL-THROUGH$ case Calendar.SECOND: result += (calendar.get(Calendar.MILLISECOND) * 1) / millisPerUnit; break; case Calendar.MILLISECOND: break;//never useful default: throw new IllegalArgumentException("The fragment " + fragment + " is not supported"); } return result; }
Example 20
Source File: Body.java From nebula with Eclipse Public License 2.0 | 4 votes |
public static Body Years() { return new Body(YEARS, Calendar.YEAR); }