开发者

The way to get day of week, pacific time

开发者 https://www.devze.com 2023-01-09 02:30 出处:网络
I need to write a method to get day of week (pacific time) for current time. Is the following code correct?

I need to write a method to get day of week (pacific time) for current time. Is the following code correct?

static Calendar s_calendar = Calendar.getInstance(Locale.US);
static {
    s_calendar.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
}

public static int getDayOfWeek() {
    s_calendar.setTimeInMillis(S开发者_开发知识库ystem.currentTimeMillis());
    return s_calendar.get(Calendar.DAY_OF_WEEK);
}

Thanks.


Use below:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"),
    Locale.US);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);


System.currentTimeMillis() returns the millis in UTC. Convert it before you call get(Calendar.DAY_OF_WEEK)

0

精彩评论

暂无评论...
验证码 换一张
取 消