📄 utility.java
字号:
return null; } else { return new Date(parseDateTime(text)); } }//#!j2me{ /** * Deserialize timestamp from general dateTime text. Timestamp values are * represented in the same way as regular dates, but allow more precision in * the fractional second value (down to nanoseconds). This method follows * standard JiBX deserializer usage requirements by accepting a * <code>null</code> input. * * @param text text to be parsed (may be <code>null</code>) * @return converted timestamp, or <code>null</code> if passed * <code>null</code> input * @throws JiBXException on parse error */ public static Timestamp deserializeTimestamp(String text) throws JiBXException { if (text == null) { return null; } else { // check for fractional second value present int split = text.indexOf('.'); int nano = 0; if (split > 0) { // make sure there aren't multiple decimal points if (text.indexOf('.', split+1) > 0) { throw new JiBXException("Not a valid timestamp value"); } // scan through all digits following decimal point int limit = text.length(); int scan = split; while (++scan < limit) { char chr = text.charAt(scan); if (chr < '0' || chr > '9') { break; } } // parse digits following decimal point int length = scan - split - 1; if (length > 9) { length = 9; } nano = parseDigits(text, split+1, length); // convert to number of nanoseconds while (length < 9) { nano *= 10; length++; } // strip fractional second off text if (scan < limit) { text = text.substring(0, split) + text.substring(scan); } else { text = text.substring(0, split); } } // return timestamp value with nanoseconds Timestamp stamp = new Timestamp(parseDateTime(text)); stamp.setNanos(nano); return stamp; } } /** * Deserialize time from text. Time values obey the rules of the time * portion of a dataTime value. This method follows standard JiBX * deserializer usage requirements by accepting a <code>null</code> input. * * @param text text to be parsed (may be <code>null</code>) * @return converted time, or <code>null</code> if passed <code>null</code> * input * @throws JiBXException on parse error */ public static Time deserializeSqlTime(String text) throws JiBXException { if (text == null) { return null; } else { return new Time(parseTime(text, 0, text.length())); } }//#j2me} /** * Format year number consistent with W3C XML Schema definitions, using a * minimum of four digits padded with zeros if necessary. A leading minus * sign is included for years prior to 1 C.E. * * @param year number to be formatted * @param buff text formatting buffer */ protected static void formatYearNumber(long year, StringBuffer buff) { // start with minus sign for dates prior to 1 C.E. if (year <= 0) { buff.append('-'); year = -(year-1); } // add padding if needed to bring to length of four if (year < 1000) { buff.append('0'); if (year < 100) { buff.append('0'); if (year < 10) { buff.append('0'); } } } // finish by converting the actual year number buff.append(year); } /** * Format a positive number as two digits. This uses an optional leading * zero digit for values less than ten. * * @param value number to be formatted (<code>0</code> to <code>99</code>) * @param buff text formatting buffer */ protected static void formatTwoDigits(int value, StringBuffer buff) { if (value < 10) { buff.append('0'); } buff.append(value); } /** * Format time in milliseconds to year number. The resulting year number * format is consistent with W3C XML Schema definitions, using a minimum * of four digits padded with zeros if necessary. A leading minus sign is * included for years prior to 1 C.E. * * @param value time in milliseconds to be converted (from 1 C.E.) * @param buff text formatting buffer */ protected static void formatYear(long value, StringBuffer buff) { // find the actual year and month number; this uses a integer arithmetic // conversion based on Baum, first making the millisecond count // relative to March 1 of the year 0 C.E., then using simple arithmetic // operations to compute century, year, and month; it's slightly // different for pre-C.E. values because of Java's handling of divisions. long time = value + 306*LMSPERDAY + LMSPERDAY*3/4; long century = time / MSPERCENTURY; // count of centuries long adjusted = time + (century - (century/4)) * MSPERDAY; int year = (int)(adjusted / MSPERAVGYEAR); // year in March 1 terms if (adjusted < 0) { year--; } long yms = adjusted + LMSPERDAY/4 - (year * 365 + year/4) * LMSPERDAY; int yday = (int)(yms / LMSPERDAY); // day number in year int month = (5*yday + 456) / 153; // (biased) month number if (month > 12) { // convert start of year year++; } // format year to text formatYearNumber(year, buff); } /** * Format time in milliseconds to year number and month number. The * resulting year number format is consistent with W3C XML Schema * definitions, using a minimum of four digits for the year and exactly * two digits for the month. * * @param value time in milliseconds to be converted (from 1 C.E.) * @param buff text formatting buffer * @return number of milliseconds into month */ protected static long formatYearMonth(long value, StringBuffer buff) { // find the actual year and month number; this uses a integer arithmetic // conversion based on Baum, first making the millisecond count // relative to March 1 of the year 0 C.E., then using simple arithmetic // operations to compute century, year, and month; it's slightly // different for pre-C.E. values because of Java's handling of divisions. long time = value + 306*LMSPERDAY + LMSPERDAY*3/4; long century = time / MSPERCENTURY; // count of centuries long adjusted = time + (century - (century/4)) * MSPERDAY; int year = (int)(adjusted / MSPERAVGYEAR); // year in March 1 terms if (adjusted < 0) { year--; } long yms = adjusted + LMSPERDAY/4 - (year * 365 + year/4) * LMSPERDAY; int yday = (int)(yms / LMSPERDAY); // day number in year if (yday == 0) { // special for negative boolean bce = year < 0; if (bce) { year--; } int dcnt = year % 4 == 0 ? 366 : 365; if (!bce) { year--; } yms += dcnt * LMSPERDAY; yday += dcnt; } int month = (5*yday + 456) / 153; // (biased) month number long rem = yms - BIAS_MONTHMS[month] - LMSPERDAY; // ms into month if (month > 12) { // convert start of year year++; month -= 12; } // format year and month as text formatYearNumber(year, buff); buff.append('-'); formatTwoDigits(month, buff); // return extra milliseconds into month return rem; } /** * Format time in milliseconds to year number, month number, and day * number. The resulting year number format is consistent with W3C XML * Schema definitions, using a minimum of four digits for the year and * exactly two digits each for the month and day. * * @param value time in milliseconds to be converted (from 1 C.E.) * @param buff text formatting buffer * @return number of milliseconds into day */ protected static int formatYearMonthDay(long value, StringBuffer buff) { // convert year and month long extra = formatYearMonth(value, buff); // append the day of month int day = (int)(extra / MSPERDAY) + 1; buff.append('-'); formatTwoDigits(day, buff); // return excess of milliseconds into day return (int)(extra % MSPERDAY); } /** * Serialize time to general gYear text. Date values are formatted in * W3C XML Schema standard format as CCYY, with optional * leading sign included if necessary. * * @param time time to be converted, as milliseconds from January 1, 1970 * @return converted gYear text */ public static String serializeYear(long time) { StringBuffer buff = new StringBuffer(6); formatYear(time + TIME_BASE, buff); return buff.toString(); } /** * Serialize date to general gYear text. Date values are formatted in * W3C XML Schema standard format as CCYY, with optional * leading sign included if necessary. * * @param date date to be converted * @return converted gYear text */ public static String serializeYear(Date date) { return serializeYear(date.getTime()); } /** * Serialize time to general gYearMonth text. Date values are formatted in * W3C XML Schema standard format as CCYY-MM, with optional * leading sign included if necessary. * * @param time time to be converted, as milliseconds from January 1, 1970 * @return converted gYearMonth text */ public static String serializeYearMonth(long time) { StringBuffer buff = new StringBuffer(12); formatYearMonth(time + TIME_BASE, buff); return buff.toString(); } /** * Serialize date to general gYearMonth text. Date values are formatted in * W3C XML Schema standard format as CCYY-MM, with optional * leading sign included if necessary. * * @param date date to be converted * @return converted gYearMonth text */ public static String serializeYearMonth(Date date) { return serializeYearMonth(date.getTime()); } /** * Serialize time to general date text. Date values are formatted in * W3C XML Schema standard format as CCYY-MM-DD, with optional * leading sign included if necessary. * * @param time time to be converted, as milliseconds from January 1, 1970 * @return converted date text */ public static String serializeDate(long time) { StringBuffer buff = new StringBuffer(12); formatYearMonthDay(time + TIME_BASE, buff); return buff.toString(); } /** * Serialize date to general date text. Date values are formatted in * W3C XML Schema standard format as CCYY-MM-DD, with optional * leading sign included if necessary. * * @param date date to be converted * @return converted date text */ public static String serializeDate(Date date) { return serializeDate(date.getTime()); }//#!j2me{ /** * Serialize SQL date to general date text. Date values are formatted in * W3C XML Schema standard format as CCYY-MM-DD, with optional * leading sign included if necessary. * * @param date date to be converted * @return converted date text */ public static String serializeSqlDate(java.sql.Date date) { // values should be normalized to midnight in zone, but no guarantee // so convert using the lame calendar approach GregorianCalendar cal = new GregorianCalendar(); cal.setGregorianChange(BEGINNING_OF_TIME); cal.setTime(date); StringBuffer buff = new StringBuffer(12); int year = cal.get(Calendar.YEAR); if (date.getTime() < 0) { if (cal.get(Calendar.ERA) == GregorianCalendar.BC) { year = -year + 1; } } formatYearNumber(year, buff); buff.append('-'); formatTwoDigits(cal.get(Calendar.MONTH)+1, buff); buff.append('-'); formatTwoDigits(cal.get(Calendar.DAY_OF_MONTH), buff); return buff.toString(); }//#j2me} /** * Serialize time to general time text in buffer. Time values are formatted * in W3C XML Schema standard format as hh:mm:ss, with optional trailing * seconds decimal, as necessary. This form uses a supplied buffer to * support flexible use, including with dateTime combination values. * * @param time time to be converted, as milliseconds in day * @param buff buffer for appending time text */ public static void serializeTime(int time, StringBuffer buff) { // append the hour, minute, and second formatTwoDigits(time/MSPERHOUR, buff); time = time % MSPERHOUR; buff.append(':'); formatTwoDigits(time/MSPERMINUTE, buff); time = time % MSPERMINUTE; buff.append(':'); formatTwoDigits(time/1000, buff); time = time % 1000; // check if decimals needed on second if (time > 0) { buff.append('.'); buff.append(time / 100); time = time % 100; if (time > 0) { buff.append(time / 10); time = time % 10; if (time > 0) { buff.append(time); } } } } /** * Serialize time to general dateTime text. Date values are formatted in * W3C XML Schema standard format as CCYY-MM-DDThh:mm:ss, with optional * leading sign and trailing seconds decimal, as necessary. * * @param time time to be converted, as milliseconds from January 1, 1970 * @param zone flag for trailing 'Z' to be appended to indicate UTC * @return converted dateTime text */ public static String serializeDateTime(long time, boolean zone) { // start with the year, month, and day StringBuffer buff = new StringBuffer(25); int extra = formatYearMonthDay(time + TIME_BASE, buff); // append the time for full form buff.append('T'); serializeTime(extra, buff); // return full text with optional trailing zone indicator if (zone) { buff.append('Z'); } return buff.toString(); } /** * Serialize time to general dateTime text. This method is provided for * backward compatibility. It generates the dateTime text without the * trailing 'Z' to indicate UTC. * * @param time time to be converted, as milliseconds from January 1, 1970 * @return converted dateTime text */ public static String serializeDateTime(long time) { return serializeDateTime(time, false); } /** * Serialize date to general dateTime text. Date values are formatted in * W3C XML Schema standard format as CCYY-MM-DDThh:mm:ssZ, with optional * leading sign and trailing seconds decimal, as necessary. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -