qdate.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,666 行 · 第 1/3 页
JAVA
1,666 行
default: return Long.MAX_VALUE; } } /** * Sets values based on a field. */ public long set(int field, long value) { switch (field) { case YEAR: setYear((int) value); break; case MONTH: setMonth((int) value); break; case DAY_OF_MONTH: setDayOfMonth((int) value); break; case HOUR: setHour((int) value); break; case MINUTE: setMinute((int) value); break; case SECOND: setSecond((int) value); break; case MILLISECOND: setMillisecond(value); break; default: throw new RuntimeException(); } return _localTimeOfEpoch; } /* * Mon, 17 Jan 1994 11:14:55 -0500 (EST) */ public String printDate() { if (_lastDate != null && _lastTime == _localTimeOfEpoch) return _lastDate; CharBuffer cb = new CharBuffer(); printDate(cb); _lastDate = cb.toString(); _lastTime = _localTimeOfEpoch; return _lastDate; } /* * Mon, 17 Jan 1994 11:14:55 -0500 (EST) */ public void printDate(CharBuffer cb) { cb.append(DAY_NAMES[(int) (_dayOfEpoch % 7 + 11) % 7]); cb.append(", "); cb.append((_dayOfMonth + 1) / 10); cb.append((_dayOfMonth + 1) % 10); cb.append(" "); cb.append(MONTH_NAMES[(int) _month]); cb.append(" "); cb.append(_year); cb.append(" "); cb.append((_timeOfDay / 36000000L) % 10); cb.append((_timeOfDay / 3600000L) % 10); cb.append(":"); cb.append((_timeOfDay / 600000L) % 6); cb.append((_timeOfDay / 60000L) % 10); cb.append(":"); cb.append((_timeOfDay / 10000L) % 6); cb.append((_timeOfDay / 1000L) % 10); if (_zoneName == null || _zoneName.equals("GMT")) { cb.append(" GMT"); return; } long offset = _zoneOffset; if (offset < 0) { cb.append(" -"); offset = - offset; } else cb.append(" +"); cb.append((offset / 36000000) % 10); cb.append((offset / 3600000) % 10); cb.append((offset / 600000) % 6); cb.append((offset / 60000) % 10); cb.append(" ("); cb.append(_zoneName); cb.append(")"); } /** * Prints the date to a stream. */ public void printDate(WriteStream os) throws IOException { os.print(DAY_NAMES[(int) (_dayOfEpoch % 7 + 11) % 7]); os.write(','); os.write(' '); os.print((_dayOfMonth + 1) / 10); os.print((_dayOfMonth + 1) % 10); os.write(' '); os.print(MONTH_NAMES[(int) _month]); os.write(' '); os.print(_year); os.write(' '); os.print((_timeOfDay / 36000000) % 10); os.print((_timeOfDay / 3600000) % 10); os.write(':'); os.print((_timeOfDay / 600000) % 6); os.print((_timeOfDay / 60000) % 10); os.write(':'); os.print((_timeOfDay / 10000) % 6); os.print((_timeOfDay / 1000) % 10); if (_zoneName == null) { os.print(" GMT"); return; } long offset = _zoneOffset; if (offset < 0) { os.write(' '); os.write('-'); offset = - offset; } else { os.write(' '); os.write('+'); } os.print((offset / 36000000) % 10); os.print((offset / 3600000) % 10); os.print((offset / 600000) % 6); os.print((offset / 60000) % 10); os.write(' '); os.write('('); os.print(_zoneName); os.write(')'); } /** * Prints the time in ISO 8601 */ public String printISO8601() { StringBuilder sb = new StringBuilder(); if (_year > 0) { sb.append((_year / 1000) % 10); sb.append((_year / 100) % 10); sb.append((_year / 10) % 10); sb.append(_year % 10); sb.append('-'); sb.append(((_month + 1) / 10) % 10); sb.append((_month + 1) % 10); sb.append('-'); sb.append(((_dayOfMonth + 1) / 10) % 10); sb.append((_dayOfMonth + 1) % 10); } long time = _timeOfDay / 1000; long ms = _timeOfDay % 1000; sb.append('T'); sb.append((time / 36000) % 10); sb.append((time / 3600) % 10); sb.append(':'); sb.append((time / 600) % 6); sb.append((time / 60) % 10); sb.append(':'); sb.append((time / 10) % 6); sb.append((time / 1) % 10); if (ms != 0) { sb.append('.'); sb.append((ms / 100) % 10); sb.append((ms / 10) % 10); sb.append(ms % 10); } if (_zoneName == null) { sb.append("Z"); return sb.toString(); } // server/1471 - XXX: was commented out long offset = _zoneOffset; if (offset < 0) { sb.append("-"); offset = - offset; } else sb.append("+"); sb.append((offset / 36000000) % 10); sb.append((offset / 3600000) % 10); sb.append(':'); sb.append((offset / 600000) % 6); sb.append((offset / 60000) % 10); return sb.toString(); } /** * Prints just the date component of ISO 8601 */ public String printISO8601Date() { CharBuffer cb = new CharBuffer(); if (_year > 0) { cb.append((_year / 1000) % 10); cb.append((_year / 100) % 10); cb.append((_year / 10) % 10); cb.append(_year % 10); cb.append('-'); cb.append(((_month + 1) / 10) % 10); cb.append((_month + 1) % 10); cb.append('-'); cb.append(((_dayOfMonth + 1) / 10) % 10); cb.append((_dayOfMonth + 1) % 10); } return cb.toString(); } /** * Formats a date. * * @param time the time to format * @param format the format string */ public synchronized static String formatGMT(long gmtTime, String format) { _gmtDate.setGMTTime(gmtTime); return _gmtDate.format(new CharBuffer(), format).toString(); } /** * Formats a date, using the default time format. * * @param time the time to format */ public synchronized static String formatGMT(long gmtTime) { _gmtDate.setGMTTime(gmtTime); return _gmtDate.printDate(); } /** * Formats a time in the local time zone. * * @param time in milliseconds, GMT, from the epoch. * @param format formatting string. */ public synchronized static String formatLocal(long gmtTime, String format) { _localDate.setGMTTime(gmtTime); return _localDate.format(new CharBuffer(), format).toString(); } /** * Formats a time in the local time zone, using the default format. * * @param time in milliseconds, GMT, from the epoch. */ public synchronized static String formatLocal(long gmtTime) { _localDate.setGMTTime(gmtTime); return _localDate.printDate(); } /** * Formats a time in the local time zone. * * @param time in milliseconds, GMT, from the epoch. * @param format formatting string. */ public synchronized static CharBuffer formatLocal(CharBuffer cb, long gmtTime, String format) { _localDate.setGMTTime(gmtTime); return _localDate.format(cb, format); } public synchronized static String formatISO8601(long gmtTime) { if (_gmtDate == null) _gmtDate = new QDate(); _gmtDate.setGMTTime(gmtTime); return _gmtDate.printISO8601(); } /** * Global date must be synchronized before you can do anything on it. */ public static QDate getGlobalDate() { return _localDate; } /** * Formats the current date. */ public String format(String format) { CharBuffer cb = new CharBuffer(); return format(cb, format).close(); } /** * Format the date using % escapes: * * <table> * <tr><td>%a<td>day of week (short) * <tr><td>%A<td>day of week (verbose) * <tr><td>%b<td>day of month (short) * <tr><td>%B<td>day of month (verbose) * <tr><td>%c<td>Java locale date * <tr><td>%d<td>day of month (two-digit) * <tr><td>%H<td>24-hour (two-digit) * <tr><td>%I<td>12-hour (two-digit) * <tr><td>%j<td>day of year (three-digit) * <tr><td>%m<td>month (two-digit) * <tr><td>%M<td>minutes * <tr><td>%p<td>am/pm * <tr><td>%S<td>seconds * <tr><td>%s<td>milliseconds * <tr><td>%x<td>Java locale short date * <tr><td>%X<td>Java locale short time * <tr><td>%W<td>week in year (three-digit) * <tr><td>%w<td>day of week (one-digit) * <tr><td>%y<td>year (two-digit) * <tr><td>%Y<td>year (four-digit) * <tr><td>%Z<td>time zone (name) * <tr><td>%z<td>time zone (+/-0800) * </table> */ public CharBuffer format(CharBuffer cb, String format) { int length = format.length(); for (int i = 0; i < length; i++) { char ch = format.charAt(i); if (ch != '%') { cb.append(ch); continue; } switch (format.charAt(++i)) { case 'a': cb.append(SHORT_WEEKDAY[getDayOfWeek() - 1]); break; case 'A': cb.append(LONG_WEEKDAY[getDayOfWeek() - 1]); break; case 'b': cb.append(SHORT_MONTH[(int) _month]); break; case 'B': cb.append(LONG_MONTH[(int) _month]); break; case 'c': cb.append(printLocaleDate()); break; case 'd': cb.append((_dayOfMonth + 1) / 10); cb.append((_dayOfMonth + 1) % 10); break; case 'H': int hour = (int) (_timeOfDay / 3600000) % 24; cb.append(hour / 10); cb.append(hour % 10); break; case 'I': hour = (int) (_timeOfDay / 3600000) % 12; if (hour == 0) hour = 12; cb.append(hour / 10); cb.append(hour % 10); break; case 'j': cb.append((_dayOfYear + 1) / 100); cb.append((_dayOfYear + 1) / 10 % 10); cb.append((_dayOfYear + 1) % 10); break; case 'm': cb.append((_month + 1) / 10); cb.append((_month + 1) % 10); break; case 'M': cb.append((_timeOfDay / 600000) % 6); cb.append((_timeOfDay / 60000) % 10); break; case 'p': hour = (int) (_timeOfDay / 3600000) % 24; if (hour < 12) cb.append("am"); else cb.append("pm"); break; case 'S': cb.append((_timeOfDay / 10000) % 6); cb.append((_timeOfDay / 1000) % 10); break; case 's': cb.append((_timeOfDay / 100) % 10); cb.append((_timeOfDay / 10) % 10); cb.append(_timeOfDay % 10); break; case 'W': int week = getWeek(); cb.append((week + 1) / 10); cb.append((week + 1) % 10); break; case 'w': cb.append(getDayOfWeek() - 1); break; case 'x': cb.append(printShortLocaleDate()); break; case 'X': cb.append(printShortLocaleTime()); break; case 'y': cb.append(_year / 10 % 10); cb.append(_year % 10); break; case 'Y': cb.append(_year / 1000 % 10); cb.append(_year / 100 % 10); cb.append(_year / 10 % 10); cb.append(_year % 10); break; case 'Z': if (_zoneName == null) cb.append("GMT"); else cb.append(_zoneName); break; case 'z': long offset = _zoneOffset; if (offset < 0) { cb.append("-"); offset = - offset; } else cb.append("+"); cb.append((offset / 36000000) % 10); cb.append((offset / 3600000) % 10); cb.append((offset / 600000) % 6); cb.append((offset / 60000) % 10); break; default: cb.append(format.charAt(i)); } } return cb; } /* * XXX: buggy (Because cal is buggy), may have to implement the sdf */ public String printLocaleDate() { _date.setTime(_localTimeOfEpoch); // SimpleDateFormat sdf = new SimpleDateFormat(); // System.out.println("" + sdf.toPattern()); if (_dateFormat == null) _dateFormat = DateFormat.getInstance(); return _dateFormat.format(_date); } /** * Returns a date in M/dd/yy format (i.e. 11/30/69 in US locale). */ public String printShortLocaleDate() { _date.setTime(_localTimeOfEpoch); if (_shortDateFormat == null) _shortDateFormat = DateFormat.getDateInstance(DateFormat.SHORT); return _shortDateFormat.format(_date); } /** * Returns a date in H:mm:ss PM format. */ public String printShortLocaleTime() { _date.setTime(_localTimeOfEpoch); if (_shortTimeFormat == null) _shortTimeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?