下面列出了java.util.TimeZone#useDaylightTime ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* {@inheritDoc}
*/
public void appendTo(StringBuffer buffer, Calendar calendar) {
if (mTimeZoneForced) {
if (mTimeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(mDaylight);
} else {
buffer.append(mStandard);
}
} else {
TimeZone timeZone = calendar.getTimeZone();
if (timeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(getTimeZoneDisplay(timeZone, true, mStyle, mLocale));
} else {
buffer.append(getTimeZoneDisplay(timeZone, false, mStyle, mLocale));
}
}
}
/**
* {@inheritDoc}
*/
public void appendTo(StringBuffer buffer, Calendar calendar) {
if (mTimeZoneForced) {
if (mTimeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(mDaylight);
} else {
buffer.append(mStandard);
}
} else {
TimeZone timeZone = calendar.getTimeZone();
if (timeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(getTimeZoneDisplay(timeZone, true, mStyle, mLocale));
} else {
buffer.append(getTimeZoneDisplay(timeZone, false, mStyle, mLocale));
}
}
}
/**
* Returns true if this zone has the same raw GMT offset value and
* transition table as another zone info. If the specified
* TimeZone object is not a ZoneInfo instance, this method returns
* true if the specified TimeZone object has the same raw GMT
* offset value with no daylight saving time.
*
* @param other the ZoneInfo object to be compared with
* @return true if the given <code>TimeZone</code> has the same
* GMT offset and transition information; false, otherwise.
*/
public boolean hasSameRules(TimeZone other) {
if (this == other) {
return true;
}
if (other == null) {
return false;
}
if (!(other instanceof ZoneInfo)) {
if (getRawOffset() != other.getRawOffset()) {
return false;
}
// if both have the same raw offset and neither observes
// DST, they have the same rule.
if ((transitions == null)
&& (useDaylightTime() == false)
&& (other.useDaylightTime() == false)) {
return true;
}
return false;
}
if (getLastRawOffset() != ((ZoneInfo)other).getLastRawOffset()) {
return false;
}
return (checksum == ((ZoneInfo)other).checksum);
}
/**
* {@inheritDoc}
*/
public void appendTo(StringBuffer buffer, Calendar calendar) {
if (mTimeZoneForced) {
if (mTimeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(mDaylight);
} else {
buffer.append(mStandard);
}
} else {
TimeZone timeZone = calendar.getTimeZone();
if (timeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(getTimeZoneDisplay(timeZone, true, mStyle, mLocale));
} else {
buffer.append(getTimeZoneDisplay(timeZone, false, mStyle, mLocale));
}
}
}
/**
* {@inheritDoc}
*/
public void appendTo(StringBuffer buffer, Calendar calendar) {
if (mTimeZoneForced) {
if (mTimeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(mDaylight);
} else {
buffer.append(mStandard);
}
} else {
TimeZone timeZone = calendar.getTimeZone();
if (timeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(getTimeZoneDisplay(timeZone, true, mStyle, mLocale));
} else {
buffer.append(getTimeZoneDisplay(timeZone, false, mStyle, mLocale));
}
}
}
/**
* Returns true if this zone has the same raw GMT offset value and
* transition table as another zone info. If the specified
* TimeZone object is not a ZoneInfo instance, this method returns
* true if the specified TimeZone object has the same raw GMT
* offset value with no daylight saving time.
*
* @param other the ZoneInfo object to be compared with
* @return true if the given <code>TimeZone</code> has the same
* GMT offset and transition information; false, otherwise.
*/
public boolean hasSameRules(TimeZone other) {
if (this == other) {
return true;
}
if (other == null) {
return false;
}
if (!(other instanceof ZoneInfo)) {
if (getRawOffset() != other.getRawOffset()) {
return false;
}
// if both have the same raw offset and neither observes
// DST, they have the same rule.
if ((transitions == null)
&& (useDaylightTime() == false)
&& (other.useDaylightTime() == false)) {
return true;
}
return false;
}
if (getLastRawOffset() != ((ZoneInfo)other).getLastRawOffset()) {
return false;
}
return (checksum == ((ZoneInfo)other).checksum);
}
/**
* {@inheritDoc}
*/
@Override
public void appendTo(CharBuffer buffer, Calendar calendar) {
if (mTimeZoneForced) {
if (mTimeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(mDaylight);
} else {
buffer.append(mStandard);
}
} else {
TimeZone timeZone = calendar.getTimeZone();
if (timeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(getTimeZoneDisplay(timeZone, true, mStyle, mLocale));
} else {
buffer.append(getTimeZoneDisplay(timeZone, false, mStyle, mLocale));
}
}
}
/**
* {@inheritDoc}
*/
public void appendTo(StringBuffer buffer, Calendar calendar) {
if (mTimeZoneForced) {
if (mTimeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(mDaylight);
} else {
buffer.append(mStandard);
}
} else {
TimeZone timeZone = calendar.getTimeZone();
if (timeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(getTimeZoneDisplay(timeZone, true, mStyle, mLocale));
} else {
buffer.append(getTimeZoneDisplay(timeZone, false, mStyle, mLocale));
}
}
}
/**
* Returns true if this zone has the same raw GMT offset value and
* transition table as another zone info. If the specified
* TimeZone object is not a ZoneInfoOld instance, this method returns
* true if the specified TimeZone object has the same raw GMT
* offset value with no daylight saving time.
*
* @param other the ZoneInfoOld object to be compared with
* @return true if the given <code>TimeZone</code> has the same
* GMT offset and transition information; false, otherwise.
*/
public boolean hasSameRules(TimeZone other) {
if (this == other) {
return true;
}
if (other == null) {
return false;
}
if (!(other instanceof ZoneInfoOld)) {
if (getRawOffset() != other.getRawOffset()) {
return false;
}
// if both have the same raw offset and neither observes
// DST, they have the same rule.
if ((transitions == null)
&& (useDaylightTime() == false)
&& (other.useDaylightTime() == false)) {
return true;
}
return false;
}
if (getLastRawOffset() != ((ZoneInfoOld)other).getLastRawOffset()) {
return false;
}
return (checksum == ((ZoneInfoOld)other).checksum);
}
/**
* Returns true if this zone has the same raw GMT offset value and
* transition table as another zone info. If the specified
* TimeZone object is not a ZoneInfoOld instance, this method returns
* true if the specified TimeZone object has the same raw GMT
* offset value with no daylight saving time.
*
* @param other the ZoneInfoOld object to be compared with
* @return true if the given <code>TimeZone</code> has the same
* GMT offset and transition information; false, otherwise.
*/
public boolean hasSameRules(TimeZone other) {
if (this == other) {
return true;
}
if (other == null) {
return false;
}
if (!(other instanceof ZoneInfoOld)) {
if (getRawOffset() != other.getRawOffset()) {
return false;
}
// if both have the same raw offset and neither observes
// DST, they have the same rule.
if ((transitions == null)
&& (useDaylightTime() == false)
&& (other.useDaylightTime() == false)) {
return true;
}
return false;
}
if (getLastRawOffset() != ((ZoneInfoOld)other).getLastRawOffset()) {
return false;
}
return (checksum == ((ZoneInfoOld)other).checksum);
}
/**
* {@inheritDoc}
*/
@Override
public void appendTo(final StringBuffer buffer, final Calendar calendar) {
final TimeZone zone = calendar.getTimeZone();
if (zone.useDaylightTime()
&& calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(getTimeZoneDisplay(zone, true, mStyle, mLocale));
} else {
buffer.append(getTimeZoneDisplay(zone, false, mStyle, mLocale));
}
}
/**
* {@inheritDoc}
*/
@Override
public void appendTo(final StringBuffer buffer, final Calendar calendar) {
final TimeZone zone = calendar.getTimeZone();
if (zone.useDaylightTime()
&& calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(getTimeZoneDisplay(zone, true, mStyle, mLocale));
} else {
buffer.append(getTimeZoneDisplay(zone, false, mStyle, mLocale));
}
}
private String calculateGMTOffset()
{
String sign = "+";
TimeZone timeZone = TimeZone.getDefault();
int offset = timeZone.getRawOffset();
if (offset < 0)
{
sign = "-";
offset = -offset;
}
int hours = offset / (60 * 60 * 1000);
int minutes = (offset - (hours * 60 * 60 * 1000)) / (60 * 1000);
try
{
if (timeZone.useDaylightTime() && timeZone.inDaylightTime(this.getDate()))
{
hours += sign.equals("+") ? 1 : -1;
}
}
catch (ParseException e)
{
// we'll do our best and ignore daylight savings
}
return "GMT" + sign + convert(hours) + ":" + convert(minutes);
}
/**
* {@inheritDoc}
*/
@Override
public void appendTo(final StringBuffer buffer, final Calendar calendar) {
final TimeZone zone = calendar.getTimeZone();
if (zone.useDaylightTime()
&& calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(getTimeZoneDisplay(zone, true, mStyle, mLocale));
} else {
buffer.append(getTimeZoneDisplay(zone, false, mStyle, mLocale));
}
}
/**
* {@inheritDoc}
*/
@Override
public void appendTo(StringBuffer buffer, Calendar calendar) {
TimeZone zone = calendar.getTimeZone();
if (zone.useDaylightTime()
&& calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(getTimeZoneDisplay(zone, true, mStyle, mLocale));
} else {
buffer.append(getTimeZoneDisplay(zone, false, mStyle, mLocale));
}
}
private String calculateGMTOffset()
{
String sign = "+";
TimeZone timeZone = TimeZone.getDefault();
int offset = timeZone.getRawOffset();
if (offset < 0)
{
sign = "-";
offset = -offset;
}
int hours = offset / (60 * 60 * 1000);
int minutes = (offset - (hours * 60 * 60 * 1000)) / (60 * 1000);
try
{
if (timeZone.useDaylightTime() && timeZone.inDaylightTime(this.getDate()))
{
hours += sign.equals("+") ? 1 : -1;
}
}
catch (ParseException e)
{
// we'll do our best and ignore daylight savings
}
return "GMT" + sign + convert(hours) + ":" + convert(minutes);
}
public static void main(String[] args) {
TimeZone defaultTimeZone = TimeZone.getDefault();
int errors = 0;
Calendar cal = new GregorianCalendar(BEGIN_YEAR, MARCH, 1);
String[] tzids = TimeZone.getAvailableIDs();
try {
for (String id : tzids) {
TimeZone tz = TimeZone.getTimeZone(id);
if (!tz.useDaylightTime()) {
continue;
}
TimeZone.setDefault(tz);
dateloop:
// Use future dates because sun.util.calendar.ZoneInfo
// delegates offset transition calculations to a SimpleTimeZone
// (after 2038 as of JDK7).
for (int year = BEGIN_YEAR; year < END_YEAR; year++) {
for (int month = MARCH; month <= NOVEMBER; month++) {
cal.set(year, month, 1, 15, 0, 0);
int maxDom = cal.getActualMaximum(DAY_OF_MONTH);
for (int dom = 1; dom <= maxDom; dom++) {
Date date = new Date(year - 1900, month, dom);
if (date.getYear()+1900 != year
|| date.getMonth() != month
|| date.getDate() != dom) {
System.err.printf("%s: got %04d-%02d-%02d, expected %04d-%02d-%02d%n",
id,
date.getYear() + 1900,
date.getMonth() + 1,
date.getDate(),
year,
month + 1,
dom);
errors++;
break dateloop;
}
}
}
}
}
} finally {
// Restore the default TimeZone.
TimeZone.setDefault(defaultTimeZone);
}
if (errors > 0) {
throw new RuntimeException("Transition test failed");
}
}
/**
* Method to convert a {@link Calendar} object to an innard array. This implementation
* uses the C API.
*
* @param cal calendar
* @param hasDate true to create an innard with a date part
* @param hasTime true to create an innard with a time part
* @return innard array
*/
public static int[] encodeInnardsWithCAPI(Calendar cal, boolean hasDate, boolean hasTime) {
DisposableMemory m = new DisposableMemory(NotesConstants.timeSize);
NotesTimeStruct time = NotesTimeStruct.newInstance(m);
boolean isNZDTConversionNeeded = false;
if (!hasDate) {
//for time only items, use local time, since there is no timezone information to tell Domino we're using UTC
Calendar calNow = Calendar.getInstance();
time.dst=0;
time.zone=0;
time.hour = cal.get(Calendar.HOUR_OF_DAY);
time.minute = cal.get(Calendar.MINUTE);
time.second = cal.get(Calendar.SECOND);
time.hundredth = (int) ((cal.get(Calendar.MILLISECOND) / 10) & 0xffffffff);
time.day = calNow.get(Calendar.DAY_OF_MONTH);
time.month = calNow.get(Calendar.MONTH)+1;
time.year = calNow.get(Calendar.YEAR);
}
else {
TimeZone tz = cal.getTimeZone();
int tzRawOffset = tz.getRawOffset();
int tzOffsetHours = (int)(tzRawOffset / 3600000);
time.hour = cal.get(Calendar.HOUR_OF_DAY);
time.minute = cal.get(Calendar.MINUTE);
time.second = cal.get(Calendar.SECOND);
time.hundredth = (int) ((cal.get(Calendar.MILLISECOND) / 10) & 0xffffffff);
time.dst=tz.useDaylightTime() ? 1 : 0;
//valid values for time.zone: -11 - +11
if (tzOffsetHours == 12) {
//special case for e.g. NZDT (New Zealand) which is UTC+13, raw offset 12 hours; switch to -11 and add one day
isNZDTConversionNeeded = true;
time.zone = 11;
Calendar calNextDay = (Calendar) cal.clone();
calNextDay.add(Calendar.DAY_OF_MONTH, -1);
time.day = calNextDay.get(Calendar.DAY_OF_MONTH);
time.month = calNextDay.get(Calendar.MONTH)+1;
time.year = calNextDay.get(Calendar.YEAR);
}
else {
time.zone=-1*tzOffsetHours;
time.day = cal.get(Calendar.DAY_OF_MONTH);
time.month = cal.get(Calendar.MONTH)+1;
time.year = cal.get(Calendar.YEAR);
}
}
time.write();
//convert day, month, year etc. to GM NotesTimeDate
boolean convRet = NotesNativeAPI.get().TimeLocalToGM(m);
if (convRet) {
String msg = "Error converting calendar value to GM: "+cal.getTime();
throw new NotesError(0, msg);
}
time.read();
int[] innards = time.GM.Innards.clone();
m.dispose();
if (isNZDTConversionNeeded) {
//overwrite the wrong timezone info with the New Zealand timeout offset
//Bit 30 (0x40000000) is set if the time zone is east of Greenwich mean time.
innards[1] |= 0x40000000;
//Bits 27-24 contain the number of hours difference between the time zone and Greenwich mean time
innards[1] &= ~0xF000000;
innards[1] |= ((long)12) << 24;
}
if (!hasDate) {
innards[1] = NotesConstants.ANYDAY;
}
if (!hasTime) {
innards[0] = NotesConstants.ALLDAY;
}
return innards;
}
/**
* Method to convert a {@link Calendar} object to an innard array. This implementation
* uses pure Java functions for the conversion, which is faster than using JNA.
*
* @param cal calendar
* @param hasDate true to create an innard with a date part
* @param hasTime true to create an innard with a time part
* @return innard array
*/
public static int[] encodeInnards(Calendar cal, boolean hasDate, boolean hasTime) {
if (!hasDate && !hasTime) {
return new int[] {NotesConstants.ALLDAY, NotesConstants.ANYDAY};
}
int[] innards = new int[2];
//The first DWORD, Innards[0], contains the number of hundredths of seconds since midnight,
if (hasTime) {
if (hasDate) {
long dtTimeMillisSince1970 = cal.getTimeInMillis();
long dtTimeDaysSince1970 = dtTimeMillisSince1970 / (24*60*60*1000);
long dtTimeMillisSince1970StartOfDay = dtTimeDaysSince1970 * 24*60*60*1000;
innards[0] = (int) (((dtTimeMillisSince1970-dtTimeMillisSince1970StartOfDay) / 10) & 0xffffffff);
}
else {
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
int millis = cal.get(Calendar.MILLISECOND);
int hundredth = millis / 10;
innards[0] = ((hour*60*60*100 + minute*60*100 + second*100 + hundredth)) & 0xffffffff;
}
}
else {
innards[0] = NotesConstants.ALLDAY;
Calendar calUTC = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calUTC.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal = calUTC;
}
if (!hasDate) {
innards[1] = NotesConstants.ANYDAY;
return innards;
}
//The 24 low-order bits contain the Julian Day, the number of days since January 1, 4713 BC
Date dtTime = cal.getTime();
long julianDay = toJulianDay(dtTime);
long zoneMask = 0;
TimeZone tz = cal.getTimeZone();
//The high-order bit, bit 31 (0x80000000), is set if Daylight Savings Time is observed
if (tz.useDaylightTime()) {
zoneMask |= 1l << 31;
}
//Bit 30 (0x40000000) is set if the time zone is east of Greenwich mean time.
int tzOffsetSeconds = (int)(tz.getRawOffset() / 1000);
if (tzOffsetSeconds>0) {
zoneMask |= 1l << 30;
}
int tzOffsetHours = Math.abs(tzOffsetSeconds / (60*60));
//Bits 27-24 contain the number of hours difference between the time zone and Greenwich mean time
zoneMask |= ((long)tzOffsetHours) << 24;
//bits 29-28 contain the number of 15-minute intervals in the difference
int tzOffsetFractionSeconds = tzOffsetSeconds - tzOffsetHours*60*60; // tzOffset % 60;
int tzOffsetFractionMinutes = tzOffsetFractionSeconds % 60;
int tzOffsetFraction15MinuteIntervalls = tzOffsetFractionMinutes / 15;
zoneMask |= ((long)tzOffsetFraction15MinuteIntervalls) << 28;
long resultLong = julianDay | zoneMask;
innards[1] = (int) (resultLong & 0xffffffff);
return innards;
}
/**
* <p>
* Equivalent of TimeZone.getDSTSavings() in JDK 1.4, but Quartz is trying
* to support JDK 1.3.
* </p>
*
* @param tz the target time-zone
* @return the amount of saving time in milliseconds
* @deprecated use TimeZone.getDSTSavings()
*/
public static int getDSTSavings(TimeZone tz) {
if (tz.useDaylightTime()) {
return 3600000;
}
return 0;
}