List of usage examples for java.util Calendar ERA
int ERA
To view the source code for java.util Calendar ERA.
Click Source Link
get
and set
indicating the era, e.g., AD or BC in the Julian calendar. From source file:org.apache.hadoop.hive.ql.udf.UDFTRUNC.java
public Text evaluate(Text date) { if (date == null) return null; try {//from ww w. ja va2 s . c o m Date theDate = formatter.parse(date.toString()); calendar.setTime(theDate); if (calendar.get(Calendar.ERA) == GregorianCalendar.BC) return null; int xxh = calendar.get(Calendar.YEAR); if ((xxh < 1900) || (xxh > 9999)) return null; calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0); result.set(formatter.format(calendar.getTime())); return result; } catch (ParseException e) { return null; } }
From source file:org.apache.logging.log4j.core.util.datetime.FastDateParserTest.java
@Test public void testParseLongShort() throws ParseException { final Calendar cal = Calendar.getInstance(NEW_YORK, Locale.US); cal.clear();/* w w w .jav a2s.c om*/ cal.set(2003, Calendar.FEBRUARY, 10, 15, 33, 20); cal.set(Calendar.MILLISECOND, 989); cal.setTimeZone(NEW_YORK); DateParser fdf = getInstance("yyyy GGGG MMMM dddd aaaa EEEE HHHH mmmm ssss SSSS ZZZZ", NEW_YORK, Locale.US); assertEquals(cal.getTime(), fdf.parse("2003 AD February 0010 PM Monday 0015 0033 0020 0989 GMT-05:00")); cal.set(Calendar.ERA, GregorianCalendar.BC); final Date parse = fdf.parse("2003 BC February 0010 PM Saturday 0015 0033 0020 0989 GMT-05:00"); assertEquals(cal.getTime(), parse); fdf = getInstance("y G M d a E H m s S Z", NEW_YORK, Locale.US); assertEquals(cal.getTime(), fdf.parse("03 BC 2 10 PM Sat 15 33 20 989 -0500")); cal.set(Calendar.ERA, GregorianCalendar.AD); assertEquals(cal.getTime(), fdf.parse("03 AD 2 10 PM Saturday 15 33 20 989 -0500")); }
From source file:org.sakaiproject.tool.summarycalendar.ui.EventSummary.java
private boolean isSameDay(long startMs, long endMs) { Calendar s = Calendar.getInstance(); Calendar e = (Calendar) s.clone(); s.setTimeInMillis(startMs);/*from w w w .jav a2s.c om*/ e.setTimeInMillis(endMs); return (s.get(Calendar.ERA) == e.get(Calendar.ERA) && s.get(Calendar.YEAR) == e.get(Calendar.YEAR) && s.get(Calendar.DAY_OF_YEAR) == e.get(Calendar.DAY_OF_YEAR)); }
From source file:com.application.utils.FastDatePrinter.java
/** * <p>Returns a list of Rules given a pattern.</p> * * @return a {@code List} of Rule objects * @throws IllegalArgumentException if pattern is invalid *///w w w.ja v a 2s. c om protected List<Rule> parsePattern() { final DateFormatSymbols symbols = new DateFormatSymbols(mLocale); final List<Rule> rules = new ArrayList<Rule>(); final String[] ERAs = symbols.getEras(); final String[] months = symbols.getMonths(); final String[] shortMonths = symbols.getShortMonths(); final String[] weekdays = symbols.getWeekdays(); final String[] shortWeekdays = symbols.getShortWeekdays(); final String[] AmPmStrings = symbols.getAmPmStrings(); final int length = mPattern.length(); final int[] indexRef = new int[1]; for (int i = 0; i < length; i++) { indexRef[0] = i; final String token = parseToken(mPattern, indexRef); i = indexRef[0]; final int tokenLen = token.length(); if (tokenLen == 0) { break; } Rule rule; final char c = token.charAt(0); switch (c) { case 'G': // era designator (text) rule = new TextField(Calendar.ERA, ERAs); break; case 'y': // year (number) if (tokenLen == 2) { rule = TwoDigitYearField.INSTANCE; } else { rule = selectNumberRule(Calendar.YEAR, tokenLen < 4 ? 4 : tokenLen); } break; case 'M': // month in year (text and number) if (tokenLen >= 4) { rule = new TextField(Calendar.MONTH, months); } else if (tokenLen == 3) { rule = new TextField(Calendar.MONTH, shortMonths); } else if (tokenLen == 2) { rule = TwoDigitMonthField.INSTANCE; } else { rule = UnpaddedMonthField.INSTANCE; } break; case 'd': // day in month (number) rule = selectNumberRule(Calendar.DAY_OF_MONTH, tokenLen); break; case 'h': // hour in am/pm (number, 1..12) rule = new TwelveHourField(selectNumberRule(Calendar.HOUR, tokenLen)); break; case 'H': // hour in day (number, 0..23) rule = selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen); break; case 'm': // minute in hour (number) rule = selectNumberRule(Calendar.MINUTE, tokenLen); break; case 's': // second in minute (number) rule = selectNumberRule(Calendar.SECOND, tokenLen); break; case 'S': // millisecond (number) rule = selectNumberRule(Calendar.MILLISECOND, tokenLen); break; case 'E': // day in week (text) rule = new TextField(Calendar.DAY_OF_WEEK, tokenLen < 4 ? shortWeekdays : weekdays); break; case 'D': // day in year (number) rule = selectNumberRule(Calendar.DAY_OF_YEAR, tokenLen); break; case 'F': // day of week in month (number) rule = selectNumberRule(Calendar.DAY_OF_WEEK_IN_MONTH, tokenLen); break; case 'w': // week in year (number) rule = selectNumberRule(Calendar.WEEK_OF_YEAR, tokenLen); break; case 'W': // week in month (number) rule = selectNumberRule(Calendar.WEEK_OF_MONTH, tokenLen); break; case 'a': // am/pm marker (text) rule = new TextField(Calendar.AM_PM, AmPmStrings); break; case 'k': // hour in day (1..24) rule = new TwentyFourHourField(selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen)); break; case 'K': // hour in am/pm (0..11) rule = selectNumberRule(Calendar.HOUR, tokenLen); break; case 'z': // time zone (text) if (tokenLen >= 4) { rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.LONG); } else { rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.SHORT); } break; case 'Z': // time zone (value) if (tokenLen == 1) { rule = TimeZoneNumberRule.INSTANCE_NO_COLON; } else { rule = TimeZoneNumberRule.INSTANCE_COLON; } break; case '\'': // literal text final String sub = token.substring(1); if (sub.length() == 1) { rule = new CharacterLiteral(sub.charAt(0)); } else { rule = new StringLiteral(sub); } break; default: throw new IllegalArgumentException("Illegal pattern component: " + token); } rules.add(rule); } return rules; }
From source file:edu.harvard.iq.dataverse.DatasetFieldValueValidator.java
private boolean isValidDate(String dateString, String pattern) { boolean valid = true; Date date;//from w ww.ja va 2s . co m SimpleDateFormat sdf = new SimpleDateFormat(pattern); sdf.setLenient(false); try { dateString = dateString.trim(); date = sdf.parse(dateString); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); int era = calendar.get(Calendar.ERA); if (era == GregorianCalendar.AD) { if (year > 9999) { valid = false; } } } catch (ParseException e) { valid = false; } if (dateString.length() > pattern.length()) { valid = false; } return valid; }
From source file:org.sqlite.date.FastDatePrinter.java
/** * <p>Returns a list of Rules given a pattern.</p> * * @return a {@code List} of Rule objects * @throws IllegalArgumentException if pattern is invalid *///from w w w .j a v a 2 s . co m protected List<Rule> parsePattern() { final DateFormatSymbols symbols = new DateFormatSymbols(mLocale); final List<Rule> rules = new ArrayList<Rule>(); final String[] ERAs = symbols.getEras(); final String[] months = symbols.getMonths(); final String[] shortMonths = symbols.getShortMonths(); final String[] weekdays = symbols.getWeekdays(); final String[] shortWeekdays = symbols.getShortWeekdays(); final String[] AmPmStrings = symbols.getAmPmStrings(); final int length = mPattern.length(); final int[] indexRef = new int[1]; for (int i = 0; i < length; i++) { indexRef[0] = i; final String token = parseToken(mPattern, indexRef); i = indexRef[0]; final int tokenLen = token.length(); if (tokenLen == 0) { break; } Rule rule; final char c = token.charAt(0); switch (c) { case 'G': // era designator (text) rule = new TextField(Calendar.ERA, ERAs); break; case 'y': // year (number) if (tokenLen == 2) { rule = TwoDigitYearField.INSTANCE; } else { rule = selectNumberRule(Calendar.YEAR, tokenLen < 4 ? 4 : tokenLen); } break; case 'M': // month in year (text and number) if (tokenLen >= 4) { rule = new TextField(Calendar.MONTH, months); } else if (tokenLen == 3) { rule = new TextField(Calendar.MONTH, shortMonths); } else if (tokenLen == 2) { rule = TwoDigitMonthField.INSTANCE; } else { rule = UnpaddedMonthField.INSTANCE; } break; case 'd': // day in month (number) rule = selectNumberRule(Calendar.DAY_OF_MONTH, tokenLen); break; case 'h': // hour in am/pm (number, 1..12) rule = new TwelveHourField(selectNumberRule(Calendar.HOUR, tokenLen)); break; case 'H': // hour in day (number, 0..23) rule = selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen); break; case 'm': // minute in hour (number) rule = selectNumberRule(Calendar.MINUTE, tokenLen); break; case 's': // second in minute (number) rule = selectNumberRule(Calendar.SECOND, tokenLen); break; case 'S': // millisecond (number) rule = selectNumberRule(Calendar.MILLISECOND, tokenLen); break; case 'E': // day in week (text) rule = new TextField(Calendar.DAY_OF_WEEK, tokenLen < 4 ? shortWeekdays : weekdays); break; case 'D': // day in year (number) rule = selectNumberRule(Calendar.DAY_OF_YEAR, tokenLen); break; case 'F': // day of week in month (number) rule = selectNumberRule(Calendar.DAY_OF_WEEK_IN_MONTH, tokenLen); break; case 'w': // week in year (number) rule = selectNumberRule(Calendar.WEEK_OF_YEAR, tokenLen); break; case 'W': // week in month (number) rule = selectNumberRule(Calendar.WEEK_OF_MONTH, tokenLen); break; case 'a': // am/pm marker (text) rule = new TextField(Calendar.AM_PM, AmPmStrings); break; case 'k': // hour in day (1..24) rule = new TwentyFourHourField(selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen)); break; case 'K': // hour in am/pm (0..11) rule = selectNumberRule(Calendar.HOUR, tokenLen); break; case 'X': // ISO 8601 rule = Iso8601_Rule.getRule(tokenLen); break; case 'z': // time zone (text) if (tokenLen >= 4) { rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.LONG); } else { rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.SHORT); } break; case 'Z': // time zone (value) if (tokenLen == 1) { rule = TimeZoneNumberRule.INSTANCE_NO_COLON; } else if (tokenLen == 2) { rule = TimeZoneNumberRule.INSTANCE_ISO_8601; } else { rule = TimeZoneNumberRule.INSTANCE_COLON; } break; case '\'': // literal text final String sub = token.substring(1); if (sub.length() == 1) { rule = new CharacterLiteral(sub.charAt(0)); } else { rule = new StringLiteral(sub); } break; default: throw new IllegalArgumentException("Illegal pattern component: " + token); } rules.add(rule); } return rules; }
From source file:DateUtils.java
/** * Check if two dates equals regardless of the time. Two null dates are equal. * Null and not null dates are not equal. * /*from w w w . j a va 2 s . co m*/ * @param dtFirst - first date to compare, can be null * @param dtSecond - second date to compare, can be null * @return boolean - true if two dates equals regardless of what the time is */ public static boolean dateEquals(Date dtFirst, Date dtSecond) { boolean bReturn = false; // If they are the same object, they are equals bReturn = (dtFirst == dtSecond); if (!bReturn) { if (dtFirst == null) { // Two null dates are the same bReturn = (dtSecond == null); } else { if (dtSecond != null) { Calendar compCalendar; int iEra; int iYear; int iMonth; int iDay; compCalendar = Calendar.getInstance(); compCalendar.setTime(dtFirst); iEra = compCalendar.get(Calendar.ERA); iYear = compCalendar.get(Calendar.YEAR); iMonth = compCalendar.get(Calendar.MONTH); iDay = compCalendar.get(Calendar.DATE); compCalendar.setTime(dtSecond); bReturn = ((iEra == compCalendar.get(Calendar.ERA)) && (iYear == compCalendar.get(Calendar.YEAR)) && (iMonth == compCalendar.get(Calendar.MONTH)) && (iDay == compCalendar.get(Calendar.DATE))); } } } return bReturn; }
From source file:org.apache.logging.log4j.core.util.datetime.FastDatePrinter.java
/** * <p>Returns a list of Rules given a pattern.</p> * * @return a {@code List} of Rule objects * @throws IllegalArgumentException if pattern is invalid *//*w w w.jav a 2s .co m*/ protected List<Rule> parsePattern() { final DateFormatSymbols symbols = new DateFormatSymbols(mLocale); final List<Rule> rules = new ArrayList<>(); final String[] ERAs = symbols.getEras(); final String[] months = symbols.getMonths(); final String[] shortMonths = symbols.getShortMonths(); final String[] weekdays = symbols.getWeekdays(); final String[] shortWeekdays = symbols.getShortWeekdays(); final String[] AmPmStrings = symbols.getAmPmStrings(); final int length = mPattern.length(); final int[] indexRef = new int[1]; for (int i = 0; i < length; i++) { indexRef[0] = i; final String token = parseToken(mPattern, indexRef); i = indexRef[0]; final int tokenLen = token.length(); if (tokenLen == 0) { break; } Rule rule; final char c = token.charAt(0); switch (c) { case 'G': // era designator (text) rule = new TextField(Calendar.ERA, ERAs); break; case 'y': // year (number) case 'Y': // week year if (tokenLen == 2) { rule = TwoDigitYearField.INSTANCE; } else { rule = selectNumberRule(Calendar.YEAR, tokenLen < 4 ? 4 : tokenLen); } if (c == 'Y') { rule = new WeekYear((NumberRule) rule); } break; case 'M': // month in year (text and number) if (tokenLen >= 4) { rule = new TextField(Calendar.MONTH, months); } else if (tokenLen == 3) { rule = new TextField(Calendar.MONTH, shortMonths); } else if (tokenLen == 2) { rule = TwoDigitMonthField.INSTANCE; } else { rule = UnpaddedMonthField.INSTANCE; } break; case 'd': // day in month (number) rule = selectNumberRule(Calendar.DAY_OF_MONTH, tokenLen); break; case 'h': // hour in am/pm (number, 1..12) rule = new TwelveHourField(selectNumberRule(Calendar.HOUR, tokenLen)); break; case 'H': // hour in day (number, 0..23) rule = selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen); break; case 'm': // minute in hour (number) rule = selectNumberRule(Calendar.MINUTE, tokenLen); break; case 's': // second in minute (number) rule = selectNumberRule(Calendar.SECOND, tokenLen); break; case 'S': // millisecond (number) rule = selectNumberRule(Calendar.MILLISECOND, tokenLen); break; case 'E': // day in week (text) rule = new TextField(Calendar.DAY_OF_WEEK, tokenLen < 4 ? shortWeekdays : weekdays); break; case 'u': // day in week (number) rule = new DayInWeekField(selectNumberRule(Calendar.DAY_OF_WEEK, tokenLen)); break; case 'D': // day in year (number) rule = selectNumberRule(Calendar.DAY_OF_YEAR, tokenLen); break; case 'F': // day of week in month (number) rule = selectNumberRule(Calendar.DAY_OF_WEEK_IN_MONTH, tokenLen); break; case 'w': // week in year (number) rule = selectNumberRule(Calendar.WEEK_OF_YEAR, tokenLen); break; case 'W': // week in month (number) rule = selectNumberRule(Calendar.WEEK_OF_MONTH, tokenLen); break; case 'a': // am/pm marker (text) rule = new TextField(Calendar.AM_PM, AmPmStrings); break; case 'k': // hour in day (1..24) rule = new TwentyFourHourField(selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen)); break; case 'K': // hour in am/pm (0..11) rule = selectNumberRule(Calendar.HOUR, tokenLen); break; case 'X': // ISO 8601 rule = Iso8601_Rule.getRule(tokenLen); break; case 'z': // time zone (text) if (tokenLen >= 4) { rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.LONG); } else { rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.SHORT); } break; case 'Z': // time zone (value) if (tokenLen == 1) { rule = TimeZoneNumberRule.INSTANCE_NO_COLON; } else if (tokenLen == 2) { rule = Iso8601_Rule.ISO8601_HOURS_COLON_MINUTES; } else { rule = TimeZoneNumberRule.INSTANCE_COLON; } break; case '\'': // literal text final String sub = token.substring(1); if (sub.length() == 1) { rule = new CharacterLiteral(sub.charAt(0)); } else { rule = new StringLiteral(sub); } break; default: throw new IllegalArgumentException("Illegal pattern component: " + token); } rules.add(rule); } return rules; }
From source file:org.apache.logging.log4j.core.util.datetime.FastDateParserTest.java
private Calendar getEraStart(int year, final TimeZone zone, final Locale locale) { final Calendar cal = Calendar.getInstance(zone, locale); cal.clear();/* ww w . ja v a 2 s .c om*/ // http://docs.oracle.com/javase/6/docs/technotes/guides/intl/calendar.doc.html if (locale.equals(FastDateParser.JAPANESE_IMPERIAL)) { if (year < 1868) { cal.set(Calendar.ERA, 0); cal.set(Calendar.YEAR, 1868 - year); } } else { if (year < 0) { cal.set(Calendar.ERA, GregorianCalendar.BC); year = -year; } cal.set(Calendar.YEAR, year / 100 * 100); } return cal; }
From source file:com.xunlei.util.DateUtils.java
/** * <p>// ww w . j a v a 2s . co m * Checks if two calendar objects are on the same week ignoring time. * </p> * <p> * 28 Mar 2002 13:45 and 28 Mar 2002 06:01 would return true. 28 Mar 2002 13:45 and 12 Mar 2002 13:45 would return false. * </p> * * @param date1 the first date, not altered, not null * @param date2 the second date, not altered, not null * @return true if they represent the same day * @throws IllegalArgumentException if either date is <code>null</code> * @author Weapon Chung * @since 2011-4-2 10:48:50 */ public static boolean isSameWeek(Calendar cal1, Calendar cal2) { if (cal1 == null || cal2 == null) { throw new IllegalArgumentException("The date must not be null"); } return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)); }