Java Code Examples for java.util.Calendar#getTimeZone()

The following examples show how to use java.util.Calendar#getTimeZone() . 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: UsageServiceImpl.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
private Date computeAdjustedTime(Date initialDate, TimeZone targetTZ) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(initialDate);
    TimeZone localTZ = cal.getTimeZone();
    int timezoneOffset = cal.get(Calendar.ZONE_OFFSET);
    if (localTZ.inDaylightTime(initialDate)) {
        timezoneOffset += (60 * 60 * 1000);
    }
    cal.add(Calendar.MILLISECOND, timezoneOffset);

    Date newTime = cal.getTime();

    Calendar calTS = Calendar.getInstance(targetTZ);
    calTS.setTime(newTime);
    timezoneOffset = calTS.get(Calendar.ZONE_OFFSET);
    if (targetTZ.inDaylightTime(initialDate)) {
        timezoneOffset += (60 * 60 * 1000);
    }

    calTS.add(Calendar.MILLISECOND, -1 * timezoneOffset);

    return calTS.getTime();
}
 
Example 2
Source File: WrapTime.java    From jphp with Apache License 2.0 6 votes vote down vote up
@Signature({
    @Arg(value = "timeZone", nativeType = WrapTimeZone.class, optional = @Optional("NULL")),
    @Arg(value = "locale", nativeType = WrapLocale.class, optional = @Optional("NULL"))
})
public static Memory today(Environment env, Memory... args) {
    Date date1 = new Date();
    Locale aLocale = args[1].isNull() ? Locale.ENGLISH : args[1].toObject(WrapLocale.class).getLocale();
    Calendar calendar = Calendar.getInstance(
            WrapTimeZone.getTimeZone(env, args[0]),
            aLocale
    );
    calendar.setTime(date1);

    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    return new ObjectMemory(new WrapTime(env, calendar.getTime(), calendar.getTimeZone(), aLocale));
}
 
Example 3
Source File: 1_FastDateFormat.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@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));
        }
    }
}
 
Example 4
Source File: Arja_0019_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * {@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));
        }
    }
}
 
Example 5
Source File: 1_FastDateFormat.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@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));
        }
    }
}
 
Example 6
Source File: TimestampUtils.java    From jdbc-cb with Apache License 2.0 6 votes vote down vote up
public Date applyCalendar(Calendar cal, Date date)
{
    // check to see if there is a calendar and that it is different than the one used to parse
    if ( !cal.getTimeZone().hasSameRules(df.getTimeZone()))
    {
        Calendar convertCal = Calendar.getInstance();
        convertCal.setTime(date);
        TimeZone toTimeZone     = cal.getTimeZone();
        TimeZone fromTimeZone   = df.getTimeZone();

        convertCal.setTimeZone(fromTimeZone);
        convertCal.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1);
        if (fromTimeZone.inDaylightTime(convertCal.getTime())) {
            convertCal.add(Calendar.MILLISECOND, convertCal.getTimeZone().getDSTSavings() * -1);
        }

        convertCal.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());
        if (toTimeZone.inDaylightTime(convertCal.getTime())) {
            convertCal.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());
        }

        return new Date(convertCal.getTime().getTime());
    }
    return date;
}
 
Example 7
Source File: Arja_00117_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * {@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));
        }
    }
}
 
Example 8
Source File: Arja_0071_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * {@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));
        }
    }
}
 
Example 9
Source File: Elixir_008_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * {@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));
        }
    }
}
 
Example 10
Source File: FastDatePrinter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@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));
    }
}
 
Example 11
Source File: DateFormat.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static String getTimeZoneString(Calendar inDate, int count) {
    TimeZone tz = inDate.getTimeZone();
    if (count < 2) { // FIXME: shouldn't this be <= 2 ?
        return formatZoneOffset(inDate.get(Calendar.DST_OFFSET) +
                                inDate.get(Calendar.ZONE_OFFSET),
                                count);
    } else {
        boolean dst = inDate.get(Calendar.DST_OFFSET) != 0;
        return tz.getDisplayName(dst, TimeZone.SHORT);
    }
}
 
Example 12
Source File: ResultSetImpl.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
    checkRowPos();
    checkColumnBounds(columnIndex);

    TimeZone tz = cal != null ? cal.getTimeZone() : this.session.getServerSession().getDefaultTimeZone();
    if (this.customTsVf != null && tz == this.lastTsCustomTz) {
        return getDateOrTimestampValueFromRow(columnIndex, this.customTsVf);
    }
    ValueFactory<Timestamp> vf = decorateDateTimeValueFactory(new SqlTimestampValueFactory(cal, tz), this.zeroDateTimeBehavior);
    this.lastTsCustomTz = tz;
    this.customTsVf = vf;
    return getDateOrTimestampValueFromRow(columnIndex, vf);
}
 
Example 13
Source File: Lang_8_FastDatePrinter_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * {@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));
    }
}
 
Example 14
Source File: GetCurrentTimeZone.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {

        //get Calendar instance
        Calendar now = Calendar.getInstance();

        //get current TimeZone using getTimeZone method of Calendar class
        TimeZone timeZone = now.getTimeZone();

        //display current TimeZone using getDisplayName() method of TimeZone class
        System.out.println("Current TimeZone is : " + timeZone.getDisplayName());
    }
 
Example 15
Source File: DatatypeConverterImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/** formats time zone specifier. */
private static void formatTimeZone(Calendar cal, StringBuilder buf) {
    TimeZone tz = cal.getTimeZone();

    if (tz == null) {
        return;
    }

    // otherwise print out normally.
    int offset = tz.getOffset(cal.getTime().getTime());

    if (offset == 0) {
        buf.append('Z');
        return;
    }

    if (offset >= 0) {
        buf.append('+');
    } else {
        buf.append('-');
        offset *= -1;
    }

    offset /= 60 * 1000; // offset is in milli-seconds

    formatTwoDigits(offset / 60, buf);
    buf.append(':');
    formatTwoDigits(offset % 60, buf);
}
 
Example 16
Source File: SetTimeTx.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public SetTimeTx() {

        final Calendar mCalendar = new GregorianCalendar();
        final TimeZone mTimeZone = mCalendar.getTimeZone();
        this.tzOffset = (mTimeZone.getRawOffset() + (int) getActualDSTOffset(mTimeZone)) / 1000;

        init(OPCODE_SET_TIME, 8);
        this.timestamp = JoH.tsl();
        long time = (timestamp / 1000) - Y2K_EPOCH;
        data.putInt((int) (time & 0xFFFFFFFF));
        data.putInt(tzOffset);
    }
 
Example 17
Source File: ISO8601Date.java    From XACML with MIT License 5 votes vote down vote up
public static ISO8601Date fromCalendar(Calendar calendar) {
	int year;
	if (calendar.get(Calendar.ERA) == GregorianCalendar.BC) {
		year	= 1 - calendar.get(Calendar.YEAR);
	} else {
		year	= calendar.get(Calendar.YEAR);
	}
	return new ISO8601Date(calendar.getTimeZone(), year, calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH));
}
 
Example 18
Source File: InstantType.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new DateTimeDt
 */
public InstantType(Calendar theCalendar) {
	super(theCalendar.getTime(), DEFAULT_PRECISION, theCalendar.getTimeZone());
}
 
Example 19
Source File: ISO8601Time.java    From XACML with MIT License 4 votes vote down vote up
public static ISO8601Time fromCalendar(Calendar calendar) {
	return new ISO8601Time(calendar.getTimeZone(), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND), calendar.get(Calendar.MILLISECOND));
}
 
Example 20
Source File: ResultSetImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public Date getDate(int columnIndex, Calendar cal) throws SQLException {
    checkRowPos();
    checkColumnBounds(columnIndex);
    ValueFactory<Date> vf = new SqlDateValueFactory(cal != null ? cal.getTimeZone() : this.session.getServerSession().getDefaultTimeZone(), this);
    return getDateOrTimestampValueFromRow(columnIndex, decorateDateTimeValueFactory(vf, this.zeroDateTimeBehavior));
}