📄 datetime.java
字号:
i = 100 * (n - 49) + i + l; this.year = (short)i; this.month = (short)j; this.day = (short)k; } } if (time == TIME_NOT_USED) { this.hour = 0; this.minute = 0; this.second = 0; } else { int hours = time / 1080000; time = time - hours * 1080000; int minutes = time / 18000; time = time - (minutes * 18000); int seconds = time / 300; time = time - seconds * 300; time = (int) Math.round(time * 1000 / 300f); this.hour = (short)hours; this.minute = (short)minutes; this.second = (short)seconds; this.millis = (short)time; } unpacked = true; } /** * Converts a calendar date into days since 1900 (Sybase epoch). * <p> * Algorithm from Fliegel, H F and van Flandern, T C (1968). * Communications of the ACM, Vol 11, No 10 (October, 1968). * * <pre> * INTEGER FUNCTION JD (YEAR,MONTH,DAY) * C * C---COMPUTES THE JULIAN DATE (JD) GIVEN A GREGORIAN CALENDAR * C DATE (YEAR,MONTH,DAY). * C * INTEGER YEAR,MONTH,DAY,I,J,K * C * I= YEAR * J= MONTH * K= DAY * C * JD= K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12) * 2 /12-3*((I+4900+(J-14)/12)/100)/4 * C * RETURN * END * </pre> * * @throws java.sql.SQLException if the date is outside the accepted range, 1753-9999 */ public void packDate() throws SQLException { if (year < 1753 || year > 9999) { throw new SQLException(Messages.get("error.datetime.range"), "22003"); } date = day - 32075 + 1461 * (year + 4800 + (month - 14) / 12) / 4 + 367 * (month - 2 - (month - 14) / 12 * 12) / 12 - 3 * ((year + 4900 + (month -14) / 12) / 100) / 4 - 2415021; } /** * Converts separate time components into a datetime time value. */ public void packTime() { time = hour * 1080000; time += minute * 18000; time += second * 300; time += Math.round(millis * 300f / 1000); if (time > 25919999) { // Time field has overflowed need to increment days // Sybase does not allow invalid time component time = 0; hour = 0; minute = 0; second = 0; millis = 0; if (date != DATE_NOT_USED) { GregorianCalendar cal = (GregorianCalendar)calendar.get(); cal.set(Calendar.YEAR, this.year); cal.set(Calendar.MONTH, this.month - 1); cal.set(Calendar.DAY_OF_MONTH, this.day); cal.add(Calendar.DATE, 1); year = (short)cal.get(Calendar.YEAR); month = (short)(cal.get(Calendar.MONTH) + 1); day = (short)cal.get(Calendar.DAY_OF_MONTH); date++; } } } /** * Retrieves the current datetime value as a Timestamp. * * @return the current datetime value as a <code>java.sql.Timestamp</code> */ public Timestamp toTimestamp() { if (tsValue == null) { if (!unpacked) { unpackDateTime(); } GregorianCalendar cal = (GregorianCalendar)calendar.get(); cal.set(Calendar.YEAR, this.year); cal.set(Calendar.MONTH, this.month - 1); cal.set(Calendar.DAY_OF_MONTH, this.day); cal.set(Calendar.HOUR_OF_DAY, this.hour); cal.set(Calendar.MINUTE, this.minute); cal.set(Calendar.SECOND, this.second); cal.set(Calendar.MILLISECOND, this.millis); tsValue = new Timestamp(cal.getTime().getTime()); } return tsValue; } /** * Retrieves the current datetime value as a Date. * * @return the current datetime value as a <code>java.sql.Date</code> */ public Date toDate() { if (dateValue == null) { if (!unpacked) { unpackDateTime(); } GregorianCalendar cal = (GregorianCalendar)calendar.get(); cal.set(Calendar.YEAR, this.year); cal.set(Calendar.MONTH, this.month - 1); cal.set(Calendar.DAY_OF_MONTH, this.day); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); dateValue = new Date(cal.getTime().getTime()); } return dateValue; } /** * Retrieves the current datetime value as a Time. * * @return the current datetime value as a <code>java.sql.Time</code> */ public Time toTime() { if (timeValue == null) { if (!unpacked) { unpackDateTime(); } GregorianCalendar cal = (GregorianCalendar)calendar.get(); cal.set(Calendar.YEAR, 1970); cal.set(Calendar.MONTH, 0); cal.set(Calendar.DAY_OF_MONTH, 1); cal.set(Calendar.HOUR_OF_DAY, this.hour); cal.set(Calendar.MINUTE, this.minute); cal.set(Calendar.SECOND, this.second); cal.set(Calendar.MILLISECOND, this.millis); timeValue = new Time(cal.getTime().getTime()); } return timeValue; } /** * Retrieves the current datetime value as a Time, Date or Timestamp. * * @return the current datetime value as an <code>java.lang.Object</code> */ public Object toObject() { if (date == DATE_NOT_USED) { return toTime(); } if (time == TIME_NOT_USED) { return toDate(); } return toTimestamp(); } /** * Retrieves the current datetime value as a String. * * @return the current datetime value as a <code>String</code> */ public String toString() { if (stringValue == null) { if (!unpacked) { unpackDateTime(); } char buf[] = new char[23]; int p = 0; if (date != DATE_NOT_USED) { p = 10; buf[--p] = (char)('0' + day % 10); day /= 10; buf[--p] = (char)('0' + day % 10); buf[--p] = '-'; buf[--p] = (char)('0' + month % 10); month /= 10; buf[--p] = (char)('0' + month % 10); buf[--p] = '-'; buf[--p] = (char)('0' + year % 10); year /= 10; buf[--p] = (char)('0' + year % 10); year /= 10; buf[--p] = (char)('0' + year % 10); year /= 10; buf[--p] = (char)('0' + year % 10); p += 10; if (time != -1) { buf[p++] = ' '; } } if (time != TIME_NOT_USED) { p += 12; buf[--p] = (char)('0' + millis % 10); millis /= 10; buf[--p] = (char)('0' + millis % 10); millis /= 10; buf[--p] = (char)('0' + millis % 10); buf[--p] = '.'; buf[--p] = (char)('0' + second % 10); second /= 10; buf[--p] = (char)('0' + second % 10); buf[--p] = ':'; buf[--p] = (char)('0' + minute % 10); minute /= 10; buf[--p] = (char)('0' + minute % 10); buf[--p] = ':'; buf[--p] = (char)('0' + hour % 10); hour /= 10; buf[--p] = (char)('0' + hour % 10); p += 12; if (buf[p-1] == '0') { p--; } if (buf[p-1] == '0') { p--; } } stringValue = String.valueOf(buf, 0, p); } return stringValue; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -