Java Code Examples for java.util.Date#getTimezoneOffset()

The following examples show how to use java.util.Date#getTimezoneOffset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Dates.java    From boon with Apache License 2.0 6 votes vote down vote up
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( '"' );

    }
 
Example 2
Source File: Timestamp.java    From ion-java with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: DateTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
}
 
Example 4
Source File: DateTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
}
 
Example 5
Source File: DateTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
}
 
Example 6
Source File: TestRange.java    From javamelody with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example 7
Source File: ServerDateTimeFormat.java    From unitime with Apache License 2.0 4 votes vote down vote up
@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)));
}
 
Example 8
Source File: ServerDateTimeFormat.java    From unitime with Apache License 2.0 4 votes vote down vote up
@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()));
}
 
Example 9
Source File: UTCDateBox.java    From gwt-traction with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the timezone offset for the specified Date.
 */
@SuppressWarnings("deprecation")
public static final long timezoneOffsetMillis(Date date) {
    return date.getTimezoneOffset()*60*1000;        
}
 
Example 10
Source File: PDTFactory.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * 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 ();
}