本文整理汇总了Java中java.util.TimeZone.getDefault方法的典型用法代码示例。如果您正苦于以下问题:Java TimeZone.getDefault方法的具体用法?Java TimeZone.getDefault怎么用?Java TimeZone.getDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.TimeZone
的用法示例。
在下文中一共展示了TimeZone.getDefault方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCompareLocal
import java.util.TimeZone; //导入方法依赖的package包/类
@Test
public void testCompareLocal() throws Exception {
ComparisonService s = new TimestampComparisonService(TimeZone.getDefault());
final long timestamp = Calendar.getInstance(TimeZone.getDefault()).getTimeInMillis();
assertEquals(Comparison.local, s.compare(new PathAttributes() {
@Override
public long getModificationDate() {
final Calendar c = Calendar.getInstance(TimeZone.getDefault());
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTimeInMillis();
}
}, new LocalAttributes("/t") {
@Override
public long getModificationDate() {
return timestamp;
}
}
));
}
示例2: getTimeZone
import java.util.TimeZone; //导入方法依赖的package包/类
/**
* Returns the time zone for which this <code>CronExpression</code>
* will be resolved.
*/
public TimeZone getTimeZone() {
if (timeZone == null) {
timeZone = TimeZone.getDefault();
}
return timeZone;
}
示例3: test_timezone
import java.util.TimeZone; //导入方法依赖的package包/类
public void test_timezone() throws Exception {
TimeZone tz1 = TimeZone.getDefault();
String text = JSON.toJSONString(tz1);
Assert.assertEquals(JSON.toJSONString(tz1.getID()), text);
TimeZone tz2 = JSON.parseObject(text, TimeZone.class);
Assert.assertEquals(tz1.getID(), tz2.getID());
}
示例4: test_systemDefault_unableToConvert_unknownId
import java.util.TimeZone; //导入方法依赖的package包/类
@Test(expectedExceptions = ZoneRulesException.class)
public void test_systemDefault_unableToConvert_unknownId() {
TimeZone current = TimeZone.getDefault();
try {
TimeZone.setDefault(new SimpleTimeZone(127, "SomethingWeird"));
ZoneId.systemDefault();
} finally {
TimeZone.setDefault(current);
}
}
示例5: getTimeZone
import java.util.TimeZone; //导入方法依赖的package包/类
public static TimeZone getTimeZone(int index) {
if (index == SYSTEM_TIME_ZONE) {
return TimeZone.getDefault();
} else {
return TimeZone.getTimeZone(availableTimeZoneNames[index]);
}
}
示例6: NativeDate
import java.util.TimeZone; //导入方法依赖的package包/类
private NativeDate()
{
if (thisTimeZone == null) {
// j.u.TimeZone is synchronized, so setting class statics from it
// should be OK.
thisTimeZone = TimeZone.getDefault();
LocalTZA = thisTimeZone.getRawOffset();
}
}
示例7: main
import java.util.TimeZone; //导入方法依赖的package包/类
public static void main(String[] args) {
if (args.length == 1) {
duration = Math.max(10, Integer.parseInt(args[0]));
}
Locale savedLocale = Locale.getDefault();
TimeZone savedTimeZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("US/Pacific"));
Locale.setDefault(Locale.US);
masterSdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
// Once it is used, DecimalFormat becomes not thread-safe.
Date d = masterSdf.parse(TIME_STRING);
new Bug6335238();
} catch (Exception e) {
System.err.println(e);
err = true;
} finally {
TimeZone.setDefault(savedTimeZone);
Locale.setDefault(savedLocale);
if (err) {
throw new RuntimeException("Failed: Multiple DateFormat instances didn't work correctly.");
} else {
System.out.println("Passed.");
}
}
}
示例8: testFormatUsesDefaultTimezone
import java.util.TimeZone; //导入方法依赖的package包/类
@Test
public void testFormatUsesDefaultTimezone() throws Exception {
TimeZone defaultTimeZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.US);
try {
assertFormatted("Dec 31, 1969 4:00:00 PM", new DefaultDateTypeAdapter(Date.class));
assertParsed("Dec 31, 1969 4:00:00 PM", new DefaultDateTypeAdapter(Date.class));
} finally {
TimeZone.setDefault(defaultTimeZone);
Locale.setDefault(defaultLocale);
}
}
示例9: 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;
}
示例10: getCurrentTimeZone
import java.util.TimeZone; //导入方法依赖的package包/类
/**
* Gets the time zone of the current time zone
*
* @return time zone String GMT+05:45
*/
public static String getCurrentTimeZone() {
TimeZone tz = TimeZone.getDefault();
String strTz = tz.getDisplayName(false, TimeZone.SHORT);
System.out.println("getCurrentTimeZone: " + strTz);
return strTz;
}
示例11: run
import java.util.TimeZone; //导入方法依赖的package包/类
public <T> T run(Supplier<T> supplier) {
TimeZone previousTimeZone = TimeZone.getDefault();
TimeZone.setDefault(this.newTimeZone);
try {
return supplier.get();
} finally {
TimeZone.setDefault(previousTimeZone);
}
}
示例12: 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;
}
示例13: 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);
}
示例14: getTimeZone
import java.util.TimeZone; //导入方法依赖的package包/类
public synchronized TimeZone getTimeZone() {
if (logon != null) {
return CommonUtil.timeZoneFromString(logon.getUser().getTimeZone());
}
return TimeZone.getDefault(); // defaultTimeZone;
}
示例15: 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;
}