下面列出了java.util.Date#getTimezoneOffset ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void fastJsonDateChars( Date date, CharBuf buf ) {
int day = date.getDate ();
int month = date.getMonth () +1;
int year = date.getYear () + 1900;
int hour = date.getHours ();
int minute = date.getMinutes ();
int second = date.getSeconds ();
int offset = date.getTimezoneOffset ();
int mili = 1;
buf.add( '"' );
buf.add( year ).add( '-' );
buf.add( Str.zfill( month, 2 ) ).add( '-' );
buf.add( Str.zfill ( day, 2 ) ).add('T');
buf.add( Str.zfill( hour, 2 ) ).add( ':' );
buf.add( Str.zfill( minute, 2 ) ).add( ':' );
buf.add( Str.zfill( second, 2 ) ).add( "." );
buf.add( Str.zfill( mili, 3 ) ).add( "Z" );
buf.add( '"' );
}
/**
* This method uses deprecated methods from {@link java.util.Date}
* instead of {@link Calendar} so that this code can be used (more easily)
* on the mobile Java platform (which has Date but does not have Calendar).
*/
@SuppressWarnings("deprecation")
private void set_fields_from_millis(long millis)
{
if(millis < MINIMUM_TIMESTAMP_IN_MILLIS){
throw new IllegalArgumentException("year is less than 1");
}
Date date = new Date(millis);
// The Date getters return values in the Date's time zone (i.e. the system time zone).
// The components need to be converted to UTC before being validated for exact ranges, because the offset
// conversion can affect which values are considered valid. Simply verify that the values can fit in the
// destination type here. If they do not, they would be out of range no matter the offset.
_minute = requireByte(date.getMinutes(), "Minute");
_second = requireByte(date.getSeconds(), "Second");
_hour = requireByte(date.getHours(), "Hour");
_day = requireByte(date.getDate(), "Day");
_month = requireByte(date.getMonth() + 1, "Month");
// Date does not correctly handle year values that represent year 0 or earlier in the system time zone through
// getYear(). This case is detected and forced to zero.
int offset = -date.getTimezoneOffset();
if(offset < 0 && MINIMUM_TIMESTAMP_IN_MILLIS - offset > millis) {
_year = 0;
} else {
_year = requireShort(date.getYear() + 1900, "Year");
}
// Now apply the offset to convert the components to UTC.
apply_offset(offset);
// Now that all components are in UTC, they may be validated for exact ranges.
this._year = checkAndCastYear(_year);
this._month = checkAndCastMonth(_month);
this._day = checkAndCastDay(_day, _year, _month);
this._hour = checkAndCastHour(_hour);
this._minute = checkAndCastMinute(_minute);
this._second = checkAndCastSecond(_second);
}
/**
* java.util.Date#Date(int, int, int)
*/
public void test_ConstructorIII() {
// Test for method java.util.Date(int, int, int)
Date d1 = new Date(70, 0, 1); // the epoch + local time
// the epoch + local time
Date d2 = new Date(0 + d1.getTimezoneOffset() * 60 * 1000);
assertTrue("Created incorrect date", d1.equals(d2));
Date date = new Date(99, 5, 22);
Calendar cal = new GregorianCalendar(1999, Calendar.JUNE, 22);
assertTrue("Wrong time zone", date.equals(cal.getTime()));
}
/**
* java.util.Date#Date(int, int, int, int, int)
*/
public void test_ConstructorIIIII() {
// Test for method java.util.Date(int, int, int, int, int)
// the epoch + local time + (1 hour and 1 minute)
Date d1 = new Date(70, 0, 1, 1, 1);
// the epoch + local time + (1 hour and 1 minute)
Date d2 = new Date(0 + d1.getTimezoneOffset() * 60 * 1000 + 60 * 60
* 1000 + 60 * 1000);
assertTrue("Created incorrect date", d1.equals(d2));
}
/**
* java.util.Date#Date(int, int, int, int, int, int)
*/
public void test_ConstructorIIIIII() {
// Test for method java.util.Date(int, int, int, int, int, int)
// the epoch + local time + (1 hour and 1 minute + 1 second)
Date d1 = new Date(70, 0, 1, 1, 1, 1);
// the epoch + local time + (1 hour and 1 minute + 1 second)
Date d2 = new Date(0 + d1.getTimezoneOffset() * 60 * 1000 + 60 * 60
* 1000 + 60 * 1000 + 1000);
assertTrue("Created incorrect date", d1.equals(d2));
}
@SuppressWarnings("deprecation")
private static boolean isSameDay(Date date1, Date date2) {
return (date1.getTime() - date1.getTimezoneOffset() * ONE_MINUTE_MILLIS)
/ ONE_DAY_MILLIS == (date2.getTime()
- date2.getTimezoneOffset() * ONE_MINUTE_MILLIS) / ONE_DAY_MILLIS;
}
@SuppressWarnings("deprecation")
public static Date toLocalDate(Date serverDate) {
if (serverDate == null || sServerTimeZone == null) return serverDate;
return new Date(serverDate.getTime() + 60000 * (serverDate.getTimezoneOffset() - getOffset(serverDate)));
}
@SuppressWarnings("deprecation")
public static Date toServerDate(Date localDate) {
if (localDate == null || sServerTimeZone == null) return localDate;
return new Date(localDate.getTime() + 60000 * (getOffset(localDate) - localDate.getTimezoneOffset()));
}
/**
* Returns the timezone offset for the specified Date.
*/
@SuppressWarnings("deprecation")
public static final long timezoneOffsetMillis(Date date) {
return date.getTimezoneOffset()*60*1000;
}
/**
* Get the time zone offset to UTC of the passed Date in minutes to be used in
* {@link XMLGregorianCalendar}.
*
* @param aDate
* The date to use. May not be <code>null</code>.
* @return 0 for no offset to UTC, the minutes otherwise. Usually in 60minutes
* steps :)
*/
@SuppressWarnings ("deprecation")
public static int getTimezoneOffsetInMinutes (@Nonnull final Date aDate)
{
return -aDate.getTimezoneOffset ();
}