qdate.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,666 行 · 第 1/3 页
JAVA
1,666 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.util;import com.caucho.vfs.WriteStream;import java.io.IOException;import java.text.DateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.TimeZone;import java.util.logging.Level;import java.util.logging.Logger;/** * Resin Date object */public class QDate { private static final Logger log = Logger.getLogger(QDate.class.getName()); static final public int YEAR = 0; static final public int MONTH = YEAR + 1; static final public int DAY_OF_MONTH = MONTH + 1; static final public int DAY = DAY_OF_MONTH + 1; static final public int DAY_OF_WEEK = DAY + 1; static final public int HOUR = DAY_OF_WEEK + 1; static final public int MINUTE = HOUR + 1; static final public int SECOND = MINUTE + 1; static final public int MILLISECOND = SECOND + 1; static final public int TIME = MILLISECOND + 1; static final public int TIME_ZONE = TIME + 1; static final long MS_PER_DAY = 24 * 60 * 60 * 1000L; static final long MS_PER_EON = MS_PER_DAY * (365 * 400 + 100 - 3); static final int []DAYS_IN_MONTH = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; static final String []DAY_NAMES = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; static final String []MONTH_NAMES = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; private static final String []SHORT_WEEKDAY = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; private static final String []LONG_WEEKDAY = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; private static final String []SHORT_MONTH = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", }; private static final String []LONG_MONTH = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", }; private static TimeZone _localTimeZone = TimeZone.getDefault(); private static TimeZone _gmtTimeZone = TimeZone.getTimeZone("GMT"); private static String _localDstName = _localTimeZone.getDisplayName(true, TimeZone.SHORT); private static String _localStdName = _localTimeZone.getDisplayName(false, TimeZone.SHORT); private static String _gmtDstName = _gmtTimeZone.getDisplayName(true, TimeZone.SHORT); private static String _gmtStdName = _gmtTimeZone.getDisplayName(false, TimeZone.SHORT); // static dates for the static formatting private static QDate _gmtDate = new QDate(false); private static QDate _localDate = new QDate(true); private TimeZone _timeZone; private Calendar _calendar; private String _dstName; private String _stdName; private DateFormat _dateFormat; private DateFormat _shortDateFormat; private DateFormat _shortTimeFormat; private Date _date = new Date(); // All times are local private long _localTimeOfEpoch; private long _dayOfEpoch; private long _year; private int _dayOfYear; private long _month; private long _dayOfMonth; private long _hour; private long _minute; private long _second; private long _ms; private boolean _isLeapYear; private long _timeOfDay; private boolean _isDaylightTime; private long _zoneOffset; private String _zoneName; private long _lastTime; private String _lastDate; /** * Creates the date for GMT. */ public QDate() { this(_gmtTimeZone); } /** * Creates the date for GMT. */ public QDate(long time) { this(_localTimeZone); setGMTTime(time); } /** * Creates the date form local or GMT. */ public QDate(boolean isLocal) { this(isLocal ? _localTimeZone : _gmtTimeZone); } /** * Creates the date from local or GMT. */ public QDate(TimeZone zone) { _timeZone = zone; if (zone == _gmtTimeZone) { _stdName = _gmtStdName; _dstName = _gmtDstName; } else if (zone == _localTimeZone) { _stdName = _localStdName; _dstName = _localDstName; } else { _stdName = _timeZone.getDisplayName(false, TimeZone.SHORT); _dstName = _timeZone.getDisplayName(true, TimeZone.SHORT); } _calendar = new GregorianCalendar(_timeZone); setLocalTime(Alarm.getCurrentTime()); } /** * Creates the date for the local time zone. * * @see #setDate(long, long, long) */ public QDate(long year, long month, long dayOfMonth) { this(_localTimeZone); setDate(year, month, dayOfMonth); } /** * Creates a local calendar. */ public static QDate createLocal() { return new QDate(true); } /** * Sets the time in milliseconds since the epoch and calculate * the internal variables. */ public void setLocalTime(long time) { // If this is a local time zone date, just set the time if (_timeZone != _gmtTimeZone) { calculateSplit(time); } // If this is a GMT date, convert from local to GMT else { calculateSplit(time - _localTimeZone.getRawOffset()); try { long offset = _localTimeZone.getOffset(GregorianCalendar.AD, (int) _year, (int) _month, (int) _dayOfMonth + 1, getDayOfWeek(), (int) _timeOfDay); calculateSplit(time - offset); } catch (Exception e) { log.log(Level.FINE, e.toString(), e); } } } /** * Returns the time in milliseconds since the epoch. */ public long getLocalTime() { // If this is a local time zone date, just set the time if (_timeZone != _gmtTimeZone) { return _localTimeOfEpoch; } // If this is a GMT date, convert from local to GMT else { long offset = _localTimeZone.getOffset(GregorianCalendar.AD, (int) _year, (int) _month, (int) _dayOfMonth + 1, getDayOfWeek(), (int) _timeOfDay); return _localTimeOfEpoch + offset; } } /** * Return the current time as a java.util.Calendar. **/ public Calendar getCalendar() { return _calendar; } /** * Sets the time in milliseconds since the epoch and calculate * the internal variables. */ public void setGMTTime(long time) { calculateSplit(time + _timeZone.getRawOffset()); // need to recalculate for daylight time if (_isDaylightTime) calculateSplit(time + _zoneOffset); } /** * Returns the time in milliseconds since the epoch. */ public long getGMTTime() { return _localTimeOfEpoch - _zoneOffset; } /** * Returns the milliseconds since the beginning of the day. */ public long getTimeOfDay() { return _timeOfDay; } /** * Returns the year. */ public int getYear() { return (int) _year; } /** * Sets the year, recalculating the time since epoch. */ public void setYear(int year) { _year = year; calculateJoin(); calculateSplit(_localTimeOfEpoch); } /** * Returns the month in the year. */ public int getMonth() { return (int) _month; } /** * Sets the month in the year. */ public void setMonth(int month) { _month = month; calculateJoin(); calculateSplit(_localTimeOfEpoch); } /** * Returns the day of the month, based on 1 for the first of the month. */ public int getDayOfMonth() { return (int) _dayOfMonth + 1; } /** * sets the day of the month based on 1 for the first of the month. */ public void setDayOfMonth(int day) { _dayOfMonth = day - 1; calculateJoin(); calculateSplit(_localTimeOfEpoch); } /** * Returns the day of the month, based on 1 for the first of the month. */ public int getDaysInMonth() { if (_month == 1) return _isLeapYear ? 29 : 28; else return DAYS_IN_MONTH[(int) _month]; } /** * Returns the day of the week. */ public int getDayOfWeek() { return (int) ((_dayOfEpoch % 7) + 11) % 7 + 1; } /** * Returns the day of the year, based on 0 for January 1. */ public int getDayOfYear() { return (int) _dayOfYear; } /** * Returns the hour. */ public int getHour() { return (int) _hour; } /** * Sets the hour, recalculating the localTimeOfEpoch. */ public void setHour(int hour) { _hour = hour; calculateJoin(); calculateSplit(_localTimeOfEpoch); } /** * Returns the minute. */ public int getMinute() { return (int) _minute; } /** * Sets the minute, recalculating the localTimeOfEpoch. */ public void setMinute(int minute) { _minute = minute; calculateJoin(); calculateSplit(_localTimeOfEpoch); } /** * Returns the second. */ public int getSecond() { return (int) _second; } /** * Sets the second, recalculating the localTimeOfEpoch. */ public void setSecond(int second) { _second = second; calculateJoin(); calculateSplit(_localTimeOfEpoch); } /** * Returns the millisecond. */ public long getMillisecond() { return _ms; } /** * Sets the millisecond, recalculating the localTimeOfEpoch. */ public void setMillisecond(long millisecond) { _ms = millisecond; calculateJoin(); calculateSplit(_localTimeOfEpoch); } /** * Returns the time zone offset for that particular day. */ public long getZoneOffset() { return _zoneOffset; } /** * Returns the name of the timezone */ public String getZoneName() { return _zoneName; } /** * Returns true for DST */ public boolean isDST() { return _isDaylightTime; } /** * Returns the current time zone. */ public TimeZone getLocalTimeZone() { return _timeZone; } /** * Returns the week in the year. */ public int getWeek() { int dow4th = (int) ((_dayOfEpoch - _dayOfYear + 3) % 7 + 10) % 7; int ww1monday = 3 - dow4th; if (_dayOfYear < ww1monday) return 53; int week = (_dayOfYear - ww1monday) / 7 + 1; if (_dayOfYear >= 360) { int days = 365 + (_isLeapYear ? 1 : 0); long nextNewYear = (_dayOfEpoch - _dayOfYear + days); int dowNext4th = (int) ((nextNewYear + 3) % 7 + 10) % 7; int nextWw1Monday = 3 - dowNext4th; if (days <= _dayOfYear - nextWw1Monday) return 1; } return week; } /** * Gets values based on a field. */ public long get(int field) { switch (field) { case TIME: return getLocalTime(); case YEAR: return getYear(); case MONTH: return getMonth(); case DAY_OF_MONTH: return getDayOfMonth(); case DAY: return getDayOfWeek(); case DAY_OF_WEEK: return getDayOfWeek(); case HOUR: return getHour(); case MINUTE: return getMinute(); case SECOND: return getSecond(); case MILLISECOND: return getMillisecond(); case TIME_ZONE: return getZoneOffset() / 1000;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?