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.tsm.concharto.util.TimeRangeFormat.java
/** * evaluates whether the separation between the begin and end is equal to 'separation' parameter * @param calendarField calendar field (e.g. Calendar.MONTH) * @param separation number of places of separation * @param tr SimpleTimeRange/*from w w w . j a v a2 s . c om*/ * @return true if begin-end = separation for the given calendar field (e.g. Calendar.MONTH) */ private static boolean isSeparatedBy(int calendarField, int separation, SimpleTimeRange tr) { GregorianCalendar begin = getCalendar(tr.getBegin().getDate()); GregorianCalendar end = getCalendar(tr.getEnd().getDate()); //roll begin by the separation ammount (takes into account boundaries e.g. month 12 + 1 = month 1) if (calendarField == Calendar.YEAR) { if (end.get(Calendar.ERA) == GregorianCalendar.BC) { separation = -separation; } } begin.roll(calendarField, separation); int endField = end.get(calendarField); int beginField = begin.get(calendarField); return (0 == (endField - beginField)); }
From source file:com.xunlei.util.DateUtils.java
/** * <p>/*from w ww . j a v a2s . c o m*/ * Checks if two calendar objects are on the same month 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 April 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 isSameMonth(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.MONTH) == cal2.get(Calendar.MONTH)); }
From source file:org.tsm.concharto.util.TimeRangeFormat.java
/** * format a date//from w w w .ja va2s. c o m * @param date date * @param cp CalendarPrecision for getting one of the format strings * @return String formatted string */ private static String dateFormat(Date date, CalendarPrecision cp) { String format; GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date); //if the year is an AD date and it is pretty old (e.g. less than 1000AD), then append the era //always display the era for BC dates if ((cal.get(Calendar.ERA) == GregorianCalendar.BC) || (cal.get(Calendar.YEAR) < MAX_YEAR_TO_DISLPAY_ERA)) { format = cp.getFormatWithEra(); } else { format = cp.getFormat(); } String text = DateFormatUtils.format(date, format); return stripLeadingZeros(date, text); }
From source file:com.xunlei.util.DateUtils.java
/** * <p>//from w w w . ja v a 2s .co m * Checks if two calendar objects are on the same year 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 April 2003 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 isSameYear(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)); }
From source file:com.application.utils.FastDateParser.java
private static String[] getDisplayNameArray(int field, boolean isLong, Locale locale) { DateFormatSymbols dfs = new DateFormatSymbols(locale); switch (field) { case Calendar.AM_PM: return dfs.getAmPmStrings(); case Calendar.DAY_OF_WEEK: return isLong ? dfs.getWeekdays() : dfs.getShortWeekdays(); case Calendar.ERA: return dfs.getEras(); case Calendar.MONTH: return isLong ? dfs.getMonths() : dfs.getShortMonths(); }/*from w ww .ja va2s. c om*/ return null; }
From source file:org.apache.logging.log4j.core.util.datetime.FastDateParserTest.java
private void testLocales(final String format, final boolean eraBC) throws Exception { final Calendar cal = Calendar.getInstance(GMT); cal.clear();//w ww. j a v a2s.c o m cal.set(2003, Calendar.FEBRUARY, 10); if (eraBC) { cal.set(Calendar.ERA, GregorianCalendar.BC); } for (final Locale locale : Locale.getAvailableLocales()) { // ja_JP_JP cannot handle dates before 1868 properly if (eraBC && locale.equals(FastDateParser.JAPANESE_IMPERIAL)) { continue; } final SimpleDateFormat sdf = new SimpleDateFormat(format, locale); final DateParser fdf = getInstance(format, locale); try { checkParse(locale, cal, sdf, fdf); } catch (final ParseException ex) { Assert.fail("Locale " + locale + " failed with " + format + " era " + (eraBC ? "BC" : "AD") + "\n" + trimMessage(ex.toString())); } } }
From source file:com.microsoft.tfs.util.datetime.LenientDateTimeParser.java
/** * Parse a free-form date and time first with the default format for the * default locale, then by parsing a list of known formats until one * succeeds. If the date cannot be parsed, ParseException is thrown. * <p>// w ww . ja va 2s . c om * WARNING: GMT doesn't parse as a time zone in on my platform (JDK 1.5, * Linux). Use UTC in your input instead. * <p> * This method may be somewhat slow, although it is probably not an area of * concern for most programs. Because of the way SimpleDateFormat parses, * all of the longer, more precise patterns must be tried first (and these * are least likely to be used by the user). This means we must walk an * array for each search and common formats are necessarily near the end. In * reality, most searches can be done under a few (6) milliseconds on a 3 * GHz computer. * <p> * <b>Notes on Default Date and Time</b> * <p> * The defaultDate and defaultTime parameters control whether the returned * Calendar has its date and/or time components set to the current date * and/or time (instead of the Date class epoch) when the given date time * string matches a pattern that does not specify <b>both</b> date and time * components. These parameters do <b>not</b> affect the order in which * patterns are tested or the criteria for a pattern match. They simply * choose which date or time is returned in the date- or time-only pattern * match where one of the components was unspecified. When either * defaultDate or defaultTime is false the Date class's epoch (1970/01/01 * 00:00:00 UTC) is used for the default date or time. * <p> * Both defaultDate and defaultTime may be set to true, but as these * parameters do not affect the matching behavior, a given string missing * <b>both</b> date and time components will still match no patterns * (causing a ParseException to be thrown). * * @param dateTimeString * the date string to parse (not null). * @param defaultDate * if true and the given date time string matches a time-only * pattern, today's date is used in the returned Calendar. If false * and a time-only pattern is matched, 1970/01/01 is used for the * date. See the method Javadoc for more information. * @param defaultTime * if true and the given date time string matches a date-only * pattern, the current time is used in the returned Calendar. If * false and a date-only pattern is matched, 00:00:00 is used for the * date. See the method Javadoc for more information. * @return a {@link LenientDateTimeResult} containing the date/time that was * in the given dateTimeString and additional match information. * @throws ParseException * if the date time string could not be parsed. */ public LenientDateTimeResult parseExtended(String dateTimeString, final boolean defaultDate, final boolean defaultTime) throws ParseException { Check.notNull(dateTimeString, "dateTimeString"); //$NON-NLS-1$ final long start = System.currentTimeMillis(); int i = 0; Calendar calendar = Calendar.getInstance(timeZone, locale); calendar.clear(); // Java's SimpleDateFormat.parse() doesn't support some common time // zones, so we convert them before parsing. dateTimeString = convertUnsupportedTimeZones(dateTimeString); for (i = 0; i < expandedFormats.length; i++) { final LenientDateTimeFormat format = expandedFormats[i]; try { calendar.setTime(format.getDateFormat().parse(dateTimeString)); if (defaultDate && format.specifiesDate() == false) { /* * We matched a pattern that doesn't specify a date and the * user wants a default of today. Copy now's date info into * ret. */ final Calendar now = new GregorianCalendar(timeZone, locale); calendar.set(Calendar.ERA, now.get(Calendar.ERA)); calendar.set(Calendar.YEAR, now.get(Calendar.YEAR)); calendar.set(Calendar.MONTH, now.get(Calendar.MONTH)); calendar.set(Calendar.DATE, now.get(Calendar.DATE)); } if (defaultTime && format.specifiesTime() == false) { /* * We matched a pattern that doesn't specify a time and the * user wants a default of now. Copy the parsed Calendar's * date info into a new time. */ final Calendar newRet = new GregorianCalendar(timeZone, locale); newRet.set(Calendar.ERA, calendar.get(Calendar.ERA)); newRet.set(Calendar.YEAR, calendar.get(Calendar.YEAR)); newRet.set(Calendar.MONTH, calendar.get(Calendar.MONTH)); newRet.set(Calendar.DATE, calendar.get(Calendar.DATE)); calendar = newRet; } final String messageFormat = "matched at index {0} in {1} ms: {2}"; //$NON-NLS-1$ final String message = MessageFormat.format(messageFormat, i, (System.currentTimeMillis() - start), dateTimeString); log.trace(message); return new LenientDateTimeResult(calendar, format.getDateFormat(), format.specifiesDate(), format.specifiesTime()); } catch (final ParseException e) { // Ignore and keep looping. } } String messageFormat = "no match in {0} ms: {1}"; //$NON-NLS-1$ String message = MessageFormat.format(messageFormat, (System.currentTimeMillis() - start), dateTimeString); log.trace(message); messageFormat = Messages.getString("LenientDateTimeParser.UnknownDateFormat"); //$NON-NLS-1$ message = MessageFormat.format(messageFormat, dateTimeString); throw new ParseException(message, 0); }
From source file:org.apache.logging.log4j.core.util.datetime.FastDateParserTest.java
@Test public void testJpLocales() { final Calendar cal = Calendar.getInstance(GMT); cal.clear();/*from w w w .j a va 2 s . co m*/ cal.set(2003, Calendar.FEBRUARY, 10); cal.set(Calendar.ERA, GregorianCalendar.BC); final Locale locale = LocaleUtils.toLocale("zh"); { // ja_JP_JP cannot handle dates before 1868 properly final SimpleDateFormat sdf = new SimpleDateFormat(LONG_FORMAT, locale); final DateParser fdf = getInstance(LONG_FORMAT, locale); try { checkParse(locale, cal, sdf, fdf); } catch (final ParseException ex) { Assert.fail("Locale " + locale + " failed with " + LONG_FORMAT + "\n" + trimMessage(ex.toString())); } } }
From source file:com.xunlei.util.DateUtils.java
/** * <p>/*from w w w. j av a 2s . c o m*/ * Checks if two calendar objects are on the same hour. * </p> * <p> * 28 Mar 2002 13:45 and 28 Mar 2002 13:01 would return true. 28 Mar 2002 13:45 and 28 Mar 2002 14: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 isSameHour(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.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) && cal1.get(Calendar.HOUR_OF_DAY) == cal2.get(Calendar.HOUR_OF_DAY)); }
From source file:util.android.util.DateUtils.java
public static boolean isSameDay(final Calendar cal1, final Calendar cal2) { if (cal1 == null || cal2 == null) { throw new IllegalArgumentException("The date must not be null"); }/*from ww w . j a v a 2 s . co m*/ return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR); }