本文整理汇总了Java中java.util.TimeZone.getOffset方法的典型用法代码示例。如果您正苦于以下问题:Java TimeZone.getOffset方法的具体用法?Java TimeZone.getOffset怎么用?Java TimeZone.getOffset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.TimeZone
的用法示例。
在下文中一共展示了TimeZone.getOffset方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convert
import java.util.TimeZone; //导入方法依赖的package包/类
private static DateTimeValue convert(DateTimeValue time,
TimeZone zone,
int sense) {
if (zone == null ||
zone.hasSameRules(ZULU) ||
time.year() == 0) {
return time;
}
long timetMillis = 0;
if (sense > 0) {
// time is in UTC
timetMillis = timetMillisFromEpochSecs(secsSinceEpoch(time), ZULU);
} else {
// time is in local time; since zone.getOffset() expects millis
// in UTC, need to convert before we can get the offset (ironic)
timetMillis = timetMillisFromEpochSecs(secsSinceEpoch(time), zone);
}
int millisecondOffset = zone.getOffset(timetMillis);
int millisecondRound = millisecondOffset < 0 ? -500 : 500;
int secondOffset = (millisecondOffset + millisecondRound) / 1000;
return addSeconds(time, sense * secondOffset);
}
示例2: getSystemTimestamp
import java.util.TimeZone; //导入方法依赖的package包/类
synchronized TimestampData getSystemTimestamp(boolean withZone) {
long millis = System.currentTimeMillis();
long seconds = millis / 1000;
int nanos = (int) (millis % 1000) * 1000000;
TimeZone zone = TimeZone.getDefault();
int offset = zone.getOffset(millis) / 1000;
if (!withZone) {
seconds += offset;
offset = 0;
}
return new TimestampData(seconds, nanos, offset);
}
示例3: apply
import java.util.TimeZone; //导入方法依赖的package包/类
@Override
public void apply(DynamicRealmObject obj) {
// set timezone offset value
long date = obj.getLong("date");
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin"));
cal.set(2017, Calendar.JANUARY, 30, 0, 0, 0);
TimeZone timeZone; // the timezone during data aquisition was not saved before schema version 2, so we have to guess the correct value for old data
if (date < cal.getTime().getTime()) { // before 30.01.2017 the only existing app installation was exclusively used in germany...
timeZone = TimeZone.getTimeZone("Europe/Berlin");
} else {
timeZone = TimeZone.getDefault(); // after that, use the device's current timezone
}
int timezoneOffsetInMinutes = timeZone.getOffset(date) / 1000 / 60;
obj.set("timezoneOffsetInMinutes", timezoneOffsetInMinutes);
}
示例4: getLocalMidnightFromNormalizedUtcDate
import java.util.TimeZone; //导入方法依赖的package包/类
/**
* This method will return the local time midnight for the provided normalized UTC date.
*
* @param normalizedUtcDate UTC time at midnight for a given date. This number comes from the
* database
*
* @return The local date corresponding to the given normalized UTC date
*/
private static long getLocalMidnightFromNormalizedUtcDate(long normalizedUtcDate) {
/* The timeZone object will provide us the current user's time zone offset */
TimeZone timeZone = TimeZone.getDefault();
/*
* This offset, in milliseconds, when added to a UTC date time, will produce the local
* time.
*/
long gmtOffset = timeZone.getOffset(normalizedUtcDate);
long localMidnightMillis = normalizedUtcDate - gmtOffset;
return localMidnightMillis;
}
示例5: getCalendarDate
import java.util.TimeZone; //导入方法依赖的package包/类
public CalendarDate getCalendarDate(long millis, CalendarDate date) {
int ms = 0; // time of day
int zoneOffset = 0;
int saving = 0;
long days = 0; // fixed date
// adjust to local time if `date' has time zone.
TimeZone zi = date.getZone();
if (zi != null) {
int[] offsets = new int[2];
if (zi instanceof ZoneInfo) {
zoneOffset = ((ZoneInfo)zi).getOffsets(millis, offsets);
} else {
zoneOffset = zi.getOffset(millis);
offsets[0] = zi.getRawOffset();
offsets[1] = zoneOffset - offsets[0];
}
// We need to calculate the given millis and time zone
// offset separately for java.util.GregorianCalendar
// compatibility. (i.e., millis + zoneOffset could cause
// overflow or underflow, which must be avoided.) Usually
// days should be 0 and ms is in the range of -13:00 to
// +14:00. However, we need to deal with extreme cases.
days = zoneOffset / DAY_IN_MILLIS;
ms = zoneOffset % DAY_IN_MILLIS;
saving = offsets[1];
}
date.setZoneOffset(zoneOffset);
date.setDaylightSaving(saving);
days += millis / DAY_IN_MILLIS;
ms += (int) (millis % DAY_IN_MILLIS);
if (ms >= DAY_IN_MILLIS) {
// at most ms is (DAY_IN_MILLIS - 1) * 2.
ms -= DAY_IN_MILLIS;
++days;
} else {
// at most ms is (1 - DAY_IN_MILLIS) * 2. Adding one
// DAY_IN_MILLIS results in still negative.
while (ms < 0) {
ms += DAY_IN_MILLIS;
--days;
}
}
// convert to fixed date (offset from Jan. 1, 1 (Gregorian))
days += EPOCH_OFFSET;
// calculate date fields from the fixed date
getCalendarDateFromFixedDate(date, days);
// calculate time fields from the time of day
setTimeOfDay(date, ms);
date.setLeapYear(isLeapYear(date));
date.setNormalized(true);
return date;
}
示例6: onBindViewHolder
import java.util.TimeZone; //导入方法依赖的package包/类
@Override
public void onBindViewHolder(ForumViewHolder holder, int position) {
ForumView.Threads thread = mItems.get(position);
TimeZone tz = TimeZone.getDefault();
int offsetFromUtc = tz.getOffset(new Date().getTime());
long now = System.currentTimeMillis() - offsetFromUtc;
holder.lastPostBy.setText(holder.lastPostBy.getContext().getString(R.string.last_post_by, thread.lastAuthorName, DateUtils.getRelativeTimeSpanString(thread.lastTime.getTime(), now, DateUtils.FORMAT_ABBREV_ALL)));
holder.title.setText(mItems.get(position).title);
if (Build.VERSION.SDK_INT >= 24) {
holder.author.setText(Html.fromHtml(thread.authorName, Html.FROM_HTML_MODE_LEGACY));
holder.title.setText(Html.fromHtml(thread.title, Html.FROM_HTML_MODE_LEGACY));
} else {
holder.author.setText(Html.fromHtml(thread.authorName));
holder.title.setText(Html.fromHtml(thread.title));
}
if (thread.lastReadPage > 1){
holder.setLastReadPage(thread.lastReadPage);
} else {
holder.setLastReadPage(0);
}
if (thread.read){
holder.cardView.setCardBackgroundColor(ContextCompat.getColor(holder.cardView.getContext(), R.color.BackgroundAccentDark));
} else {
holder.cardView.setCardBackgroundColor(ContextCompat.getColor(holder.cardView.getContext(), R.color.Background));
}
if (thread.locked) {
holder.imgLocked.setVisibility(View.VISIBLE);
} else {
holder.imgLocked.setVisibility(View.INVISIBLE);
}
if (thread.sticky) {
holder.imgSticky.setVisibility(View.VISIBLE);
} else {
holder.imgSticky.setVisibility(View.INVISIBLE);
}
holder.setTopicId(thread.topicId);
}
示例7: _adjustTimeZone
import java.util.TimeZone; //导入方法依赖的package包/类
/**
* Adjust the specified date, which is in server timeZone to the timeZone
* found in RequestContext and return the new date long value.
*/
@SuppressWarnings("cast")
private static long _adjustTimeZone(
Date date)
{
// get the current date of the server
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
long dateValueInMs = calendar.getTimeInMillis();
// adjust the date; first, get this into GMT
long tzOffset = calendar.get(Calendar.ZONE_OFFSET) +
calendar.get(Calendar.DST_OFFSET);
// get the timeZone specified in trinidad-config, if any or the
// client timeZone and find out the difference in timeZone
TimeZone timeZone = RequestContext.getCurrentInstance().getTimeZone();
if(timeZone == null)
{
timeZone = TimeZone.getDefault();
}
// then, adjust for the "local" time zone (either the client's, as
// specified in RequestContext, or the server's if it wasn't specified
// in RequestContext)
tzOffset -= timeZone.getOffset(dateValueInMs);
// make sure that adjusting to correct timeZone doesn't take the
// long value out of the range. Calendar too doesn't handle this
// properly ie. MIN_VALUE < (longValue + tzOffset) < MAX_VALUE.
if (tzOffset < 0)
{
// Cast to (float) has a purpose
tzOffset = (long)Math.max((float)tzOffset,
(float)Long.MIN_VALUE - (float)dateValueInMs);
}
else
{
// Cast to (float) has a purpose
tzOffset = (long)Math.min((float)tzOffset,
(float)Long.MAX_VALUE - (float)dateValueInMs);
}
// adjust the date in ms to the adjusted time zone.
long adjusted = dateValueInMs + tzOffset;
return adjusted;
}
示例8: getNormalizedUtcDateForToday
import java.util.TimeZone; //导入方法依赖的package包/类
/**
* This method returns the number of milliseconds (UTC time) for today's date at midnight in
* the local time zone. For example, if you live in California and the day is September 20th,
* 2016 and it is 6:30 PM, it will return 1474329600000. Now, if you plug this number into an
* Epoch time converter, you may be confused that it tells you this time stamp represents 8:00
* PM on September 19th local time, rather than September 20th. We're concerned with the GMT
* date here though, which is correct, stating September 20th, 2016 at midnight.
*
* As another example, if you are in Hong Kong and the day is September 20th, 2016 and it is
* 6:30 PM, this method will return 1474329600000. Again, if you plug this number into an Epoch
* time converter, you won't get midnight for your local time zone. Just keep in mind that we
* are just looking at the GMT date here.
*
* This method will ALWAYS return the date at midnight (in GMT time) for the time zone you
* are currently in. In other words, the GMT date will always represent your date.
*
* Since UTC / GMT time are the standard for all time zones in the world, we use it to
* normalize our dates that are stored in the database. When we extract values from the
* database, we adjust for the current time zone using time zone offsets.
*
* @return The number of milliseconds (UTC / GMT) for today's date at midnight in the local
* time zone
*/
public static long getNormalizedUtcDateForToday() {
/*
* This number represents the number of milliseconds that have elapsed since January
* 1st, 1970 at midnight in the GMT time zone.
*/
long utcNowMillis = System.currentTimeMillis();
/*
* This TimeZone represents the device's current time zone. It provides us with a means
* of acquiring the offset for local time from a UTC time stamp.
*/
TimeZone currentTimeZone = TimeZone.getDefault();
/*
* The getOffset method returns the number of milliseconds to add to UTC time to get the
* elapsed time since the epoch for our current time zone. We pass the current UTC time
* into this method so it can determine changes to account for daylight savings time.
*/
long gmtOffsetMillis = currentTimeZone.getOffset(utcNowMillis);
/*
* UTC time is measured in milliseconds from January 1, 1970 at midnight from the GMT
* time zone. Depending on your time zone, the time since January 1, 1970 at midnight (GMT)
* will be greater or smaller. This variable represents the number of milliseconds since
* January 1, 1970 (GMT) time.
*/
long timeSinceEpochLocalTimeMillis = utcNowMillis + gmtOffsetMillis;
/* This method simply converts milliseconds to days, disregarding any fractional days */
long daysSinceEpochLocal = TimeUnit.MILLISECONDS.toDays(timeSinceEpochLocalTimeMillis);
/*
* Finally, we convert back to milliseconds. This time stamp represents today's date at
* midnight in GMT time. We will need to account for local time zone offsets when
* extracting this information from the database.
*/
long normalizedUtcMidnightMillis = TimeUnit.DAYS.toMillis(daysSinceEpochLocal);
return normalizedUtcMidnightMillis;
}
示例9: getDayNumber
import java.util.TimeZone; //导入方法依赖的package包/类
/**
* This method returns the number of days since the epoch (January 01, 1970, 12:00 Midnight UTC)
* in UTC time from the current date.
*
* @param date A date in milliseconds in local time.
*
* @return The number of days in UTC time from the epoch.
*/
public static long getDayNumber(long date) {
TimeZone tz = TimeZone.getDefault();
long gmtOffset = tz.getOffset(date);
return (date + gmtOffset) / DAY_IN_MILLIS;
}
示例10: getLocalDateFromUTC
import java.util.TimeZone; //导入方法依赖的package包/类
/**
* Since all dates from the database are in UTC, we must convert the given date
* (in UTC timezone) to the date in the local timezone. Ths function performs that conversion
* using the TimeZone offset.
*
* @param utcDate The UTC datetime to convert to a local datetime, in milliseconds.
* @return The local date (the UTC datetime - the TimeZone offset) in milliseconds.
*/
public static long getLocalDateFromUTC(long utcDate) {
TimeZone tz = TimeZone.getDefault();
long gmtOffset = tz.getOffset(utcDate);
return utcDate - gmtOffset;
}
示例11: getUTCDateFromLocal
import java.util.TimeZone; //导入方法依赖的package包/类
/**
* Since all dates from the database are in UTC, we must convert the local date to the date in
* UTC time. This function performs that conversion using the TimeZone offset.
*
* @param localDate The local datetime to convert to a UTC datetime, in milliseconds.
* @return The UTC date (the local datetime + the TimeZone offset) in milliseconds.
*/
public static long getUTCDateFromLocal(long localDate) {
TimeZone tz = TimeZone.getDefault();
long gmtOffset = tz.getOffset(localDate);
return localDate + gmtOffset;
}