A way to get the day of the week, peacetime

I need to write a method to get the day of the week (peace time) for the 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(System.currentTimeMillis());
    return s_calendar.get(Calendar.DAY_OF_WEEK);
}

Thank.

+3
source share
2 answers

Use below:

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

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

0
source

Source: https://habr.com/ru/post/1757057/


All Articles