📄 simpledate.java
字号:
/*
* Copyright (C) 1999-2005 <a href="mailto:paschke@in.tum.de">Adrian Paschke</a>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.mandarax.lib.date;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Locale;
/**
* A <code>SimpleDate</code> implementation which extends the <code>java.util.GregorianCalendar</code>.
* A <code>SimpleDate</code> can be in synchronized mode and not synchronized (absolute) mode.
* In the not synchronized mode the absolute time values independent from the time zone are used for
* comparison of calendar objects (e.g. a SimpleDate with time value 19:00 p.m. in Germany Berlin is
* the same as SimpleDate object with time value 19:00 p.m. in USA Los Angeles).
* In the synchronized mode two SimpleDate objects are equal if they have the same real time value
* (e.g. a SimpleDate with time value 19:00 p.m. in Germany Berlin is the same as the SimpleDate object with
* time value 10:00 a.m. in USA Los Angeles (- 8 hours)). The time zone offset is given by the java.util.Timezone.
*
*
* @author <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
* @version 3.4 <7 March 05>
* @since 3.3.1
*/
public class SimpleDate extends GregorianCalendar {
private boolean syn = true;
///////////////
// Constructors
///////////////
/**
* Constructs a default, synchronized <code>SimpleDate</code> using the current time
* in the default time zone with the default locale.
*/
public SimpleDate() {
super();
this.syn=true;
}
/**
* Constructs a default<code>SimpleDate</code> using the current time
* in the default time zone with the default locale and synchronized flag.
*/
public SimpleDate(boolean syn) {
super();
this.syn=syn;
}
/**
* Constructs a <code>SimpleDate</code> based on the current time
* in the given time zone with the default locale.
*
* @param zone the given time zone.
* @param syn synchronized flag
*/
public SimpleDate(TimeZone zone, boolean syn) {
super(zone);
this.syn=syn;
}
/**
* Constructs a <code>SimpleDate</code> based on the current time
* in the default time zone with the given locale and synchronized flag.
*
* @param aLocale the given locale.
* @param syn synchronized flag
*/
public SimpleDate(Locale aLocale, boolean syn) {
super(aLocale);
this.syn=syn;
}
/**
* Constructs a <code>SimpleDate</code> based on the current time
* in the given time zone with the given locale and synchronized flag.
*
* @param zone the given time zone.
* @param aLocale the given locale.
* @param syn synchronized flag
*/
public SimpleDate(TimeZone zone, Locale aLocale, boolean syn) {
super(zone, aLocale);
this.syn=syn;
}
/**
* Constructs a <code>SimpleDate</code> with the given date set
* in the default time zone with the default locale and synchronized flag.
*
* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
* Month value is 0-based. e.g., 0 for January.
* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
* @param syn synchronized flag
*/
public SimpleDate(int year, int month, int dayOfMonth, boolean syn) {
super(year, month, dayOfMonth);
this.syn=syn;
}
/**
* Constructs a <code>SimpleDate</code> with the given date
* and time set for the default time zone with the default locale and synchronized flag.
*
* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
* Month value is 0-based. e.g., 0 for January.
* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
* in the calendar.
* @param minute the value used to set the <code>MINUTE</code> calendar field
* in the calendar.
*/
public SimpleDate(int year, int month, int dayOfMonth, int hourOfDay,
int minute, boolean syn) {
super(year, month, dayOfMonth, hourOfDay, minute);
this.syn=syn;
}
/**
* Constructs a SimpleDate with the given date
* and time set for the default time zone with the default locale and synchronized flag.
*
* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
* Month value is 0-based. e.g., 0 for January.
* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
* in the calendar.
* @param minute the value used to set the <code>MINUTE</code> calendar field
* in the calendar.
* @param second the value used to set the <code>SECOND</code> calendar field
* in the calendar.
*/
public SimpleDate(int year, int month, int dayOfMonth, int hourOfDay,
int minute, int second, boolean syn) {
super(year, month, dayOfMonth, hourOfDay, minute, second);
this.syn=syn;
}
/**
* Constructs a synchronized <code>SimpleDate</code> based on the current time
* in the given time zone with the default locale.
*
* @param zone the given time zone.
*/
public SimpleDate(TimeZone zone) {
super(zone);
this.syn=true;
}
/**
* Constructs a synchronized <code>SimpleDate</code> based on the current time
* in the default time zone with the given locale.
*
* @param aLocale the given locale.
*/
public SimpleDate(Locale aLocale) {
super(aLocale);
this.syn=true;
}
/**
* Constructs a synchronized<code>SimpleDate</code> based on the current time
* in the given time zone with the given locale.
*
* @param zone the given time zone.
* @param aLocale the given locale.
*/
public SimpleDate(TimeZone zone, Locale aLocale) {
super(zone, aLocale);
this.syn=true;
}
/**
* Constructs a synchronized<code>SimpleDate</code> with the given date set
* in the default time zone with the default locale.
*
* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
* Month value is 0-based. e.g., 0 for January.
* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
*/
public SimpleDate(int year, int month, int dayOfMonth) {
super(year, month, dayOfMonth);
this.syn=true;
}
/**
* Constructs a synchronized<code>SimpleDate</code> with the given date
* and time set for the default time zone with the default locale.
*
* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
* Month value is 0-based. e.g., 0 for January.
* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
* in the calendar.
* @param minute the value used to set the <code>MINUTE</code> calendar field
* in the calendar.
*/
public SimpleDate(int year, int month, int dayOfMonth, int hourOfDay,
int minute) {
super(year, month, dayOfMonth, hourOfDay, minute);
this.syn=true;
}
/**
* Constructs a synchronized SimpleDate with the given date
* and time set for the default time zone with the default locale.
*
* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
* Month value is 0-based. e.g., 0 for January.
* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
* in the calendar.
* @param minute the value used to set the <code>MINUTE</code> calendar field
* in the calendar.
* @param second the value used to set the <code>SECOND</code> calendar field
* in the calendar.
*/
public SimpleDate(int year, int month, int dayOfMonth, int hourOfDay,
int minute, int second) {
super(year, month, dayOfMonth, hourOfDay, minute, second);
this.syn=true;
}
/////////////////
// Public methods
/////////////////
/**
* Indicated wether this SimpleDate is in synchronized mode or not
* @return boolean true:synchronized - false: not synchronize
*/
public boolean isSynchronized() {
return syn;
}
/**
* Set the synchronized mode
* @param syn synchronized flag
*/
public void setSynchronized(boolean syn) {
this.syn=syn;
}
/**
* Compares this <code>SimpleDate</code> to the specified
* <code>Object</code>. The result is <code>true</code> if and
* only if the argument is a instance of <code>Calendar</code>
* that represents the same time value (millisecond offset from
* the <a href="Calendar.html#Epoch">Epoch</a>) in the synchronized
* mode or the time value + the time zone offsets (in milliseconds) as
* this object (~ the absolute times are compared).
*
* @param obj the object to compare with.
* @return <code>true</code> if this object is equal to <code>obj</code>;
* <code>false</code> otherwise.
*/
public boolean equals(Object obj) {
if ( (obj instanceof SimpleDate) || (obj instanceof Calendar) ) {
// compare absolute times (GMT time in milliseconds + zone offset in milliseconds)
if (!this.syn) return this.getTimeInMillis()+this.get(Calendar.ZONE_OFFSET)==((Calendar) obj).getTimeInMillis()+((Calendar)obj).get(Calendar.ZONE_OFFSET);
if (obj instanceof SimpleDate) if (!((SimpleDate)obj).isSynchronized()) return this.getTimeInMillis()+this.get(Calendar.ZONE_OFFSET)==((Calendar) obj).getTimeInMillis()+((Calendar)obj).get(Calendar.ZONE_OFFSET);
// compare synchronized times (GMT time in milliseconds)
return this.getTimeInMillis()==((Calendar)obj).getTimeInMillis();
}
return false;
}
/**
* Returns whether this <code>SimpleDate</code> represents a time
* before the time represented by the specified
* <code>Object</code>, if and only if <code>obj</code> is a <code>Calendar</code>
* instance. Otherwise, the method returns <code>false</code>.
* In synchronized mode only the time values are compared, in not synchronized mode
* the time zone offsets are added (~ the absolute times are compared).
*
* @param obj the <code>Object</code> to be compared
* @return <code>true</code> if the time of this
* <code>Calendar</code> is before the time represented by
* <code>obj</code>; <code>false</code> otherwise.
*/
public boolean before(Object obj) {
if ( (obj instanceof SimpleDate) || (obj instanceof Calendar) ) {
// compare absolute times (GMT time in milliseconds + zone offset in milliseconds)
if (!this.syn) return this.getTimeInMillis()+this.get(Calendar.ZONE_OFFSET)<((Calendar) obj).getTimeInMillis()+((Calendar)obj).get(Calendar.ZONE_OFFSET);
if (obj instanceof SimpleDate) if (!((SimpleDate)obj).isSynchronized()) return this.getTimeInMillis()+this.get(Calendar.ZONE_OFFSET)<((Calendar) obj).getTimeInMillis()+((Calendar)obj).get(Calendar.ZONE_OFFSET);
// compare synchronized times (GMT time in milliseconds)
return this.getTimeInMillis()<((Calendar)obj).getTimeInMillis();
}
return false;
}
/**
* Returns whether this <code>SimpleDate</code> represents a time
* after the time represented by the specified
* <code>Object</code>, if and only if <code>obj</code> is a <code>Calendar</code>
* instance. Otherwise, the method returns <code>false</code>.
* In synchronized mode only the time values are compared, in not synchronized mode
* the time zone offsets are added (~the absolute times are compared).
*
* @param obj the <code>Object</code> to be compared
* @return <code>true</code> if the time of this <code>Calendar</code> is
* after the time represented by <code>when</code>; <code>false</code>
* otherwise.
* @see #compareTo(Calendar)
*/
public boolean after(Object obj) {
if ( (obj instanceof SimpleDate) || (obj instanceof Calendar) ) {
// compare absolute times (GMT time in milliseconds + zone offset in milliseconds)
if (!this.syn) return this.getTimeInMillis()+this.get(Calendar.ZONE_OFFSET)>((Calendar) obj).getTimeInMillis()+((Calendar)obj).get(Calendar.ZONE_OFFSET);
if (obj instanceof SimpleDate) if (!((SimpleDate)obj).isSynchronized()) return this.getTimeInMillis()+this.get(Calendar.ZONE_OFFSET)>((Calendar) obj).getTimeInMillis()+((Calendar)obj).get(Calendar.ZONE_OFFSET);
// compare synchronized times (GMT time in milliseconds)
return this.getTimeInMillis()>((Calendar)obj).getTimeInMillis();
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -