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.ow2.aspirerfid.queryandcapture.ui.MasterDataQueryAndCaptureGui.java
/** * Formats a <code>Calendar</code> value into an ISO8601-compliant date/time * string./*from ww w.j av 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.sqlite.date.FastDateParser.java
/** * Obtain a Strategy given a field from a SimpleDateFormat pattern * @param formatField A sub-sequence of the SimpleDateFormat pattern * @param definingCalendar The calendar to obtain the short and long values * @return The Strategy that will handle parsing for the field *///from w w w. j a v a 2 s . c om private Strategy getStrategy(final String formatField, final Calendar definingCalendar) { switch (formatField.charAt(0)) { case '\'': if (formatField.length() > 2) { return new CopyQuotedStrategy(formatField.substring(1, formatField.length() - 1)); } //$FALL-THROUGH$ default: return new CopyQuotedStrategy(formatField); case 'D': return DAY_OF_YEAR_STRATEGY; case 'E': return getLocaleSpecificStrategy(Calendar.DAY_OF_WEEK, definingCalendar); case 'F': return DAY_OF_WEEK_IN_MONTH_STRATEGY; case 'G': return getLocaleSpecificStrategy(Calendar.ERA, definingCalendar); case 'H': // Hour in day (0-23) return HOUR_OF_DAY_STRATEGY; case 'K': // Hour in am/pm (0-11) return HOUR_STRATEGY; case 'M': return formatField.length() >= 3 ? getLocaleSpecificStrategy(Calendar.MONTH, definingCalendar) : NUMBER_MONTH_STRATEGY; case 'S': return MILLISECOND_STRATEGY; case 'W': return WEEK_OF_MONTH_STRATEGY; case 'a': return getLocaleSpecificStrategy(Calendar.AM_PM, definingCalendar); case 'd': return DAY_OF_MONTH_STRATEGY; case 'h': // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0 return HOUR12_STRATEGY; case 'k': // Hour in day (1-24), i.e. midnight is 24, not 0 return HOUR24_OF_DAY_STRATEGY; case 'm': return MINUTE_STRATEGY; case 's': return SECOND_STRATEGY; case 'w': return WEEK_OF_YEAR_STRATEGY; case 'y': return formatField.length() > 2 ? LITERAL_YEAR_STRATEGY : ABBREVIATED_YEAR_STRATEGY; case 'X': return ISO8601TimeZoneStrategy.getStrategy(formatField.length()); case 'Z': if (formatField.equals("ZZ")) { return ISO_8601_STRATEGY; } //$FALL-THROUGH$ case 'z': return getLocaleSpecificStrategy(Calendar.ZONE_OFFSET, definingCalendar); } }
From source file:com.application.utils.FastDateParser.java
/** * Obtain a Strategy given a field from a SimpleDateFormat pattern * * @param formatField A sub-sequence of the SimpleDateFormat pattern * @param definingCalendar The calendar to obtain the short and long values * @return The Strategy that will handle parsing for the field *//*from w w w . jav a 2 s .c o m*/ private Strategy getStrategy(final String formatField, final Calendar definingCalendar) { switch (formatField.charAt(0)) { case '\'': if (formatField.length() > 2) { return new CopyQuotedStrategy(formatField.substring(1, formatField.length() - 1)); } //$FALL-THROUGH$ default: return new CopyQuotedStrategy(formatField); case 'D': return DAY_OF_YEAR_STRATEGY; case 'E': return getLocaleSpecificStrategy(Calendar.DAY_OF_WEEK, definingCalendar); case 'F': return DAY_OF_WEEK_IN_MONTH_STRATEGY; case 'G': return getLocaleSpecificStrategy(Calendar.ERA, definingCalendar); case 'H': return MODULO_HOUR_OF_DAY_STRATEGY; case 'K': return HOUR_STRATEGY; case 'M': return formatField.length() >= 3 ? getLocaleSpecificStrategy(Calendar.MONTH, definingCalendar) : NUMBER_MONTH_STRATEGY; case 'S': return MILLISECOND_STRATEGY; case 'W': return WEEK_OF_MONTH_STRATEGY; case 'a': return getLocaleSpecificStrategy(Calendar.AM_PM, definingCalendar); case 'd': return DAY_OF_MONTH_STRATEGY; case 'h': return MODULO_HOUR_STRATEGY; case 'k': return HOUR_OF_DAY_STRATEGY; case 'm': return MINUTE_STRATEGY; case 's': return SECOND_STRATEGY; case 'w': return WEEK_OF_YEAR_STRATEGY; case 'y': return formatField.length() > 2 ? LITERAL_YEAR_STRATEGY : ABBREVIATED_YEAR_STRATEGY; case 'Z': case 'z': return getLocaleSpecificStrategy(Calendar.ZONE_OFFSET, definingCalendar); } }
From source file:org.apache.axis2.databinding.utils.ConverterUtil.java
/** * Converts a given string into a date. Code from Axis1 DateDeserializer. * * @param source// ww w .j a v a 2 s .c o m * @return Returns Date. */ public static Date convertToDate(String source) { // the lexical form of the date is '-'? yyyy '-' mm '-' dd zzzzzz? if ((source == null) || source.trim().equals("")) { return null; } source = source.trim(); boolean bc = false; if (source.startsWith("-")) { source = source.substring(1); bc = true; } int year = 0; int month = 0; int day = 0; int timeZoneOffSet = TimeZone.getDefault().getRawOffset(); if (source.length() >= 10) { //first 10 numbers must give the year if ((source.charAt(4) != '-') || (source.charAt(7) != '-')) { throw new RuntimeException("invalid date format (" + source + ") with out - s at correct place "); } year = Integer.parseInt(source.substring(0, 4)); month = Integer.parseInt(source.substring(5, 7)); day = Integer.parseInt(source.substring(8, 10)); if (source.length() > 10) { String restpart = source.substring(10); if (restpart.startsWith("Z")) { // this is a gmt time zone value timeZoneOffSet = 0; } else if (restpart.startsWith("+") || restpart.startsWith("-")) { // this is a specific time format string if (restpart.charAt(3) != ':') { throw new RuntimeException( "invalid time zone format (" + source + ") without : at correct place"); } int hours = Integer.parseInt(restpart.substring(1, 3)); int minits = Integer.parseInt(restpart.substring(4, 6)); timeZoneOffSet = ((hours * 60) + minits) * 60000; if (restpart.startsWith("-")) { timeZoneOffSet = timeZoneOffSet * -1; } } else { throw new RuntimeException("In valid string sufix"); } } } else { throw new RuntimeException("In valid string to parse"); } Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.setLenient(false); calendar.set(Calendar.YEAR, year); //xml month stars from the 1 and calendar month is starts with 0 calendar.set(Calendar.MONTH, month - 1); calendar.set(Calendar.DAY_OF_MONTH, day); calendar.set(Calendar.ZONE_OFFSET, timeZoneOffSet); // set the day light off set only if time zone if (source.length() >= 10) { calendar.set(Calendar.DST_OFFSET, 0); } calendar.getTimeInMillis(); if (bc) { calendar.set(Calendar.ERA, GregorianCalendar.BC); } return calendar.getTime(); }
From source file:org.apache.logging.log4j.core.util.datetime.FastDateParser.java
/** * Obtain a Strategy given a field from a SimpleDateFormat pattern * @param formatField A sub-sequence of the SimpleDateFormat pattern * @param definingCalendar The calendar to obtain the short and long values * @return The Strategy that will handle parsing for the field *//*w w w. j ava2s. c o m*/ private Strategy getStrategy(final char f, final int width, final Calendar definingCalendar) { switch (f) { default: throw new IllegalArgumentException("Format '" + f + "' not supported"); case 'D': return DAY_OF_YEAR_STRATEGY; case 'E': return getLocaleSpecificStrategy(Calendar.DAY_OF_WEEK, definingCalendar); case 'F': return DAY_OF_WEEK_IN_MONTH_STRATEGY; case 'G': return getLocaleSpecificStrategy(Calendar.ERA, definingCalendar); case 'H': // Hour in day (0-23) return HOUR_OF_DAY_STRATEGY; case 'K': // Hour in am/pm (0-11) return HOUR_STRATEGY; case 'M': return width >= 3 ? getLocaleSpecificStrategy(Calendar.MONTH, definingCalendar) : NUMBER_MONTH_STRATEGY; case 'S': return MILLISECOND_STRATEGY; case 'W': return WEEK_OF_MONTH_STRATEGY; case 'a': return getLocaleSpecificStrategy(Calendar.AM_PM, definingCalendar); case 'd': return DAY_OF_MONTH_STRATEGY; case 'h': // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0 return HOUR12_STRATEGY; case 'k': // Hour in day (1-24), i.e. midnight is 24, not 0 return HOUR24_OF_DAY_STRATEGY; case 'm': return MINUTE_STRATEGY; case 's': return SECOND_STRATEGY; case 'u': return DAY_OF_WEEK_STRATEGY; case 'w': return WEEK_OF_YEAR_STRATEGY; case 'y': case 'Y': return width > 2 ? LITERAL_YEAR_STRATEGY : ABBREVIATED_YEAR_STRATEGY; case 'X': return ISO8601TimeZoneStrategy.getStrategy(width); case 'Z': if (width == 2) { return ISO8601TimeZoneStrategy.ISO_8601_3_STRATEGY; } //$FALL-THROUGH$ case 'z': return getLocaleSpecificStrategy(Calendar.ZONE_OFFSET, definingCalendar); } }
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 . co 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:com.hybridbpm.core.util.HybridbpmCoreUtil.java
public static boolean isSameDay(Calendar cal1, Calendar cal2) { if (cal1 == null || cal2 == null) { throw new IllegalArgumentException("The dates must not be null"); }/*from ww w.j av a2 s .c o 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)); }
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.//from w w w . ja v a 2 s. co m * <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.axis2.databinding.utils.ConverterUtil.java
/** * Code from Axis1 code base Note - We only follow the convention in the latest schema spec * * @param source/*from w w w . j a v a 2s.co m*/ * @return Returns Calendar. */ public static Calendar convertToDateTime(String source) { if ((source == null) || source.trim().equals("")) { return null; } source = source.trim(); // the lexical representation of the date time as follows // '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)? Date date = null; Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.setLenient(false); if (source.startsWith("-")) { source = source.substring(1); calendar.set(Calendar.ERA, GregorianCalendar.BC); } int year = 0; int month = 0; int day = 0; int hour = 0; int minite = 0; int second = 0; long miliSecond = 0; int timeZoneOffSet = TimeZone.getDefault().getRawOffset(); if ((source != null) && (source.length() >= 19)) { if ((source.charAt(4) != '-') || (source.charAt(7) != '-') || (source.charAt(10) != 'T') || (source.charAt(13) != ':') || (source.charAt(16) != ':')) { throw new RuntimeException("invalid date format (" + source + ") with out - s at correct place "); } year = Integer.parseInt(source.substring(0, 4)); month = Integer.parseInt(source.substring(5, 7)); day = Integer.parseInt(source.substring(8, 10)); hour = Integer.parseInt(source.substring(11, 13)); minite = Integer.parseInt(source.substring(14, 16)); second = Integer.parseInt(source.substring(17, 19)); int milliSecondPartLength = 0; if (source.length() > 19) { String rest = source.substring(19); if (rest.startsWith(".")) { // i.e this have the ('.'s+) part if (rest.endsWith("Z")) { // this is in gmt time zone timeZoneOffSet = 0; calendar.setTimeZone(TimeZone.getTimeZone("GMT")); miliSecond = Integer.parseInt(rest.substring(1, rest.lastIndexOf("Z"))); milliSecondPartLength = rest.substring(1, rest.lastIndexOf("Z")).trim().length(); } else if ((rest.lastIndexOf("+") > 0) || (rest.lastIndexOf("-") > 0)) { // this is given in a general time zione String timeOffSet = null; if (rest.lastIndexOf("+") > 0) { timeOffSet = rest.substring(rest.lastIndexOf("+") + 1); miliSecond = Integer.parseInt(rest.substring(1, rest.lastIndexOf("+"))); milliSecondPartLength = rest.substring(1, rest.lastIndexOf("+")).trim().length(); // we keep +1 or -1 to finally calculate the value timeZoneOffSet = 1; } else if (rest.lastIndexOf("-") > 0) { timeOffSet = rest.substring(rest.lastIndexOf("-") + 1); miliSecond = Integer.parseInt(rest.substring(1, rest.lastIndexOf("-"))); milliSecondPartLength = rest.substring(1, rest.lastIndexOf("-")).trim().length(); // we keep +1 or -1 to finally calculate the value timeZoneOffSet = -1; } if (timeOffSet.charAt(2) != ':') { throw new RuntimeException( "invalid time zone format (" + source + ") without : at correct place"); } int hours = Integer.parseInt(timeOffSet.substring(0, 2)); int minits = Integer.parseInt(timeOffSet.substring(3, 5)); timeZoneOffSet = ((hours * 60) + minits) * 60000 * timeZoneOffSet; } else { // i.e it does not have time zone miliSecond = Integer.parseInt(rest.substring(1)); milliSecondPartLength = rest.substring(1).trim().length(); } } else { if (rest.startsWith("Z")) { calendar.setTimeZone(TimeZone.getTimeZone("GMT")); // this is in gmt time zone timeZoneOffSet = 0; } else if (rest.startsWith("+") || rest.startsWith("-")) { // this is given in a general time zione if (rest.charAt(3) != ':') { throw new RuntimeException( "invalid time zone format (" + source + ") without : at correct place"); } int hours = Integer.parseInt(rest.substring(1, 3)); int minits = Integer.parseInt(rest.substring(4, 6)); timeZoneOffSet = ((hours * 60) + minits) * 60000; if (rest.startsWith("-")) { timeZoneOffSet = timeZoneOffSet * -1; } } else { throw new NumberFormatException("in valid time zone attribute"); } } } calendar.set(Calendar.YEAR, year); // xml month is started from 1 and calendar month is started from 0 calendar.set(Calendar.MONTH, month - 1); calendar.set(Calendar.DAY_OF_MONTH, day); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minite); calendar.set(Calendar.SECOND, second); if (milliSecondPartLength != 3) { // milisecond part represenst the fraction of the second so we have to // find the fraction and multiply it by 1000. So if milisecond part // has three digits nothing required miliSecond = miliSecond * 1000; for (int i = 0; i < milliSecondPartLength; i++) { miliSecond = miliSecond / 10; } } calendar.set(Calendar.MILLISECOND, (int) miliSecond); calendar.set(Calendar.ZONE_OFFSET, timeZoneOffSet); // set the day light offset only if the time zone is present if (source.length() > 19) { calendar.set(Calendar.DST_OFFSET, 0); } } else { throw new NumberFormatException("date string can not be less than 19 characters"); } return calendar; }
From source file:TypeConversionHelper.java
/** * Converts a string in JDBC timestamp escape format to a Timestamp object. * To be precise, we prefer to find a JDBC escape type sequence in the format * "yyyy-mm-dd hh:mm:ss.fffffffff", but this does accept other separators * of fields, so as long as the numbers are in the order * year, month, day, hour, minute, second then we accept it. * @param s Timestamp string//from ww w. j av a 2 s. c om * @param cal The Calendar to use for conversion * @return Corresponding <tt>java.sql.Timestamp</tt> value. * @exception java.lang.IllegalArgumentException Thrown if the format of the * String is invalid */ public static Timestamp stringToTimestamp(String s, Calendar cal) { int[] numbers = convertStringToIntArray(s); if (numbers == null || numbers.length < 6) { throw new IllegalArgumentException("string to time stamp" + s); } int year = numbers[0]; int month = numbers[1]; int day = numbers[2]; int hour = numbers[3]; int minute = numbers[4]; int second = numbers[5]; int nanos = 0; if (numbers.length > 6) { nanos = numbers[6]; } Calendar thecal = cal; if (cal == null) { thecal = new GregorianCalendar(); } thecal.set(Calendar.ERA, GregorianCalendar.AD); thecal.set(Calendar.YEAR, year); thecal.set(Calendar.MONTH, month - 1); thecal.set(Calendar.DATE, day); thecal.set(Calendar.HOUR_OF_DAY, hour); thecal.set(Calendar.MINUTE, minute); thecal.set(Calendar.SECOND, second); Timestamp ts = new Timestamp(thecal.getTime().getTime()); ts.setNanos(nanos); return ts; }