List of usage examples for java.util GregorianCalendar BC
int BC
To view the source code for java.util GregorianCalendar BC.
Click Source Link
ERA
field indicating the period before the common era (before Christ), also known as BCE. From source file:org.apache.jackrabbit.core.persistence.util.BundleReader.java
/** * Deserializes a specially encoded date written using bundle * serialization version 3.//from w w w. j a v a 2 s .c o m * * @return deserialized date * @throws IOException if an I/O error occurs */ private Calendar readDate() throws IOException { long ts = readVarLong(); TimeZone tz; if ((ts & 1) == 0) { tz = COMMON_TIMEZONES[0]; ts >>= 1; } else if ((ts & 2) == 0) { tz = COMMON_TIMEZONES[((int) ts >> 2) & 0x1f]; // 5 bits; ts >>= 7; } else { int m = ((int) ts << 19) >> 21; // 11 bits, sign-extended int h = m / 60; String s; if (m < 0) { s = String.format("GMT-%02d:%02d", -h, h * 60 - m); } else { s = String.format("GMT+%02d:%02d", h, m - h * 60); } tz = TimeZone.getTimeZone(s); ts >>= 13; } int u = 0; int s = 0; int m = 0; int h = 0; int type = (int) ts & 3; ts >>= 2; switch (type) { case 3: u = (int) ts & 0x3fffffff; // 30 bits s = u / 1000; m = s / 60; h = m / 60; m -= h * 60; s -= (h * 60 + m) * 60; u -= ((h * 60 + m) * 60 + s) * 1000; ts >>= 30; break; case 2: m = (int) ts & 0x07ff; // 11 bits h = m / 60; m -= h * 60; ts >>= 11; break; case 1: h = (int) ts & 0x1f; // 5 bits ts >>= 5; break; } int d = (int) ts & 0x01ff; // 9 bits; ts >>= 9; int y = (int) (ts + 2010); Calendar value = Calendar.getInstance(tz); if (y <= 0) { value.set(Calendar.YEAR, 1 - y); value.set(Calendar.ERA, GregorianCalendar.BC); } else { value.set(Calendar.YEAR, y); value.set(Calendar.ERA, GregorianCalendar.AD); } value.set(Calendar.DAY_OF_YEAR, d); value.set(Calendar.HOUR_OF_DAY, h); value.set(Calendar.MINUTE, m); value.set(Calendar.SECOND, s); value.set(Calendar.MILLISECOND, u); return value; }
From source file:org.apache.jackrabbit.core.persistence.util.BundleWriter.java
/** * Serializes a JCR date value using the {@link #writeVarLong(long)} * serialization on a special 64-bit date encoding. This encoding maps * the <code>sYYYY-MM-DDThh:mm:ss.sssTZD</code> date format used by * JCR to an as small 64 bit integer (positive or negative) as possible, * while preserving full accuracy (including time zone offsets) and * favouring common levels of accuracy (per minute, hour and day) over * full millisecond level detail.// w w w.j av a 2 s .c om * <p> * Each date value is mapped to separate timestamp and timezone fields, * both of whose lenghts are variable: * <pre> * +----- ... ------- ... --+ * | timestamp | timezone | * +----- ... ------- ... --+ * </pre> * <p> * The type and length of the timezone field can be determined by looking * at the two least significant bits of the value: * <dl> * <dt><code>?0</code></dt> * <dd> * UTC time. The length of the timezone field is just one bit, * i.e. the second bit is already a part of the timestamp field. * </dd> * <dt><code>01</code></dt> * <dd> * The offset is counted as hours from UTC, and stored as the number * of hours (positive or negative) in the next 5 bits (range from * -16 to +15 hours), making the timezone field 7 bits long in total. * </dd> * <dt><code>11</code></dt> * <dd> * The offset is counted as hours and minutes from UTC, and stored * as the total minute offset (positive or negative) in the next * 11 bits (range from -17 to +17 hours), making the timezone field * 13 bits long in total. * </dd> * </dl> * <p> * The remaining 51-63 bits of the encoded value make up the timestamp * field that also uses the two least significant bits to indicate the * type and length of the field: * <dl> * <dt><code>00</code></dt> * <dd> * <code>sYYYY-MM-DDT00:00:00.000</code>, i.e. midnight of the * specified date. The next 9 bits encode the day within the year * (starting from 1, maximum value 366) and the remaining bits are * used for the year, stored as an offset from year 2010. * </dd> * <dt><code>01</code></dt> * <dd> * <code>sYYYY-MM-DDThh:00:00.000</code>, i.e. at the hour. The * next 5 bits encode the hour within the day (starting from 0, * maximum value 23) and the remaining bits are used as described * above for the date. * </dd> * <dt><code>10</code></dt> * <dd> * <code>sYYYY-MM-DDThh:mm:00.000</code>, i.e. at the minute. The * next 11 bits encode the minute within the day (starting from 0, * maximum value 1439) and the remaining bits are used as described * above for the date. * </dd> * <dt><code>11</code></dt> * <dd> * <code>sYYYY-MM-DDThh:mm:ss.sss</code>, i.e. full millisecond * accuracy. The next 30 bits encode the millisecond within the * day (starting from 0, maximum value 87839999) and the remaining * bits are used as described above for the date. * </dd> * </dl> * <p> * With full timezone and millisecond accuracies, this encoding leaves * 10 bits (64 - 9 - 30 - 2 - 11 - 2) for the date offset, which allows * for representation of all timestamps between years 1498 and 2521. * Timestamps outside this range and with a minute-level timezone offset * are automatically truncated to minute-level accuracy to support the * full range of years -9999 to 9999 specified in JCR. * <p> * Note that the year, day of year, and time of day values are stored * as separate bit sequences to avoid problems with changing leap second * or leap year definitions. Bit fields are used for better encoding and * decoding performance than what would be possible with the slightly more * space efficient mechanism of using multiplication and modulo divisions * to separate the different timestamp fields. * * @param value date value * @throws IOException if an I/O error occurs */ private void writeDate(Calendar value) throws IOException { int y = value.get(Calendar.YEAR); if (value.isSet(Calendar.ERA) && value.get(Calendar.ERA) == GregorianCalendar.BC) { y = 1 - y; // convert to an astronomical year } y -= 2010; // use a recent offset NOTE: do not change this! int d = value.get(Calendar.DAY_OF_YEAR); int h = value.get(Calendar.HOUR_OF_DAY); int m = value.get(Calendar.MINUTE); int s = value.get(Calendar.SECOND); int u = value.get(Calendar.MILLISECOND); int z = value.getTimeZone().getOffset(value.getTimeInMillis()) / (60 * 1000); int zh = z / 60; int zm = z - zh * 60; long ts = y << 9 | d & 0x01ff; if ((u != 0 || s != 0) && ((-512 <= y && y < 512) || zm == 0)) { ts <<= 30; ts |= (((h * 60 + m) * 60 + s) * 1000 + u) & 0x3fffffff; // 30 bits ts <<= 2; ts |= 3; } else if (m != 0) { ts <<= 11; ts |= (h * 60 + m) & 0x07ff; // 11 bits ts <<= 2; ts |= 2; } else if (h != 0) { ts <<= 5; ts |= h & 0x1f; // 5 bits ts <<= 2; ts |= 1; } else { ts <<= 2; } if (zm != 0) { ts <<= 11; ts |= z & 0x07ff; // 11 bits writeVarLong(ts << 2 | 3); } else if (zh != 0) { ts <<= 5; ts |= zh & 0x1f; // 5 bits writeVarLong(ts << 2 | 1); } else { writeVarLong(ts << 1); } }
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();/*from w w w. j ava2s.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.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();//from w w w . j a 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: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();//from w ww . j a v a 2 s.c om 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: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.java 2 s . com 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:org.ow2.aspirerfid.queryandcapture.ui.MasterDataQueryAndCaptureGui.java
/** * Formats a <code>Calendar</code> value into an ISO8601-compliant date/time * string./* ww w . ja v a2 s . c o m*/ * * @param cal * The time value to be formatted into a date/time string. * @return The formatted date/time string. */ private static String format(final Calendar cal) { if (cal == null) { throw new IllegalArgumentException("argument can not be null"); } // determine era and adjust year if necessary int year = cal.get(Calendar.YEAR); if (cal.isSet(Calendar.ERA) && cal.get(Calendar.ERA) == GregorianCalendar.BC) { /** * calculate year using astronomical system: year n BCE => * astronomical year -n + 1 */ year = 0 - year + 1; } /** * the format of the date/time string is: YYYY-MM-DDThh:mm:ss.SSSTZD * note that we cannot use java.text.SimpleDateFormat for formatting * because it can't handle years <= 0 and TZD's */ StringBuilder buf = new StringBuilder(); // year ([-]YYYY) buf.append(XXXX_FORMAT.format(year)); buf.append('-'); // month (MM) buf.append(XX_FORMAT.format(cal.get(Calendar.MONTH) + 1)); buf.append('-'); // day (DD) buf.append(XX_FORMAT.format(cal.get(Calendar.DAY_OF_MONTH))); buf.append('T'); // hour (hh) buf.append(XX_FORMAT.format(cal.get(Calendar.HOUR_OF_DAY))); buf.append(':'); // minute (mm) buf.append(XX_FORMAT.format(cal.get(Calendar.MINUTE))); buf.append(':'); // second (ss) buf.append(XX_FORMAT.format(cal.get(Calendar.SECOND))); buf.append('.'); // millisecond (SSS) buf.append(XXX_FORMAT.format(cal.get(Calendar.MILLISECOND))); // time zone designator (+/-hh:mm) buf.append(getTimeZone(cal)); return buf.toString(); }
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 ww.ja 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:org.tsm.concharto.util.TimeRangeFormat.java
/** * format a date//from w w w . j a v a 2s . 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); }