如何使Java日历从星期一开始工作日? [英] How to make java calendar to start weekday from Monday?

查看:420
本文介绍了如何使Java日历从星期一开始工作日?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了代码,该代码使用Java日历并显示时间戳中的DAY_OF_WEEK。但是默认日历从星期日(1)开始。我想从星期一开始。它应该在星期一返回1。这是我的代码:

  Calender c = Calender.getInstance(TimeZone.getInstance( Australia / Sydney)); 
c.setTimeInMillis(1413831601032L);
c.setFirstDayOfWeek(Calender.Monday);
System.out.println(c.get(c.DAY_OF_WEEK));

setFirstDayOfWeek()在这种情况下无济于事。



Tueday的输出应该为2,但显示给我3。

解决方案

一周的第一天从当前语言环境派生。如果您未设置日历的语言环境(Calendar.getInstance(Locale),或新的 GregorianCalendar(Locale)),它将使用系统的默认值。

  public static void main(String [] args){
日历c = new GregorianCalendar();
System.out.println(Locale.getDefault()+: + c.getFirstDayOfWeek());
}

对于语言/国家,这应该显示具有不同JVM参数的不同输出:

  -Duser.language = zh-Duser.country = US-> zh_cn:1(星期日)
-Duser.language = zh-Duser.country = GB-> en_GB:2(星期一)

此外,您还可以使用方法 setFirstDayOfWeek( )设置一周的第一天。该方法只能影响WEEK_OF_MONTH 或返回值。 F 或DAY_OF_WEEK,它什么也没做



请参考

此外,如果您看到 Calendar.java ,您将看到天的值是恒定的,如下所示。因此,无论设置为一周的第一天,这都将在星期一返回1。


public final static int SUNDAY = 1;



public final static int MONDAY = 2;
....



公共最终静态int星期六= 7;


根据设置的第一天,您可以执行以下操作并处理数据。

  [c .get(Calendar.DAY_OF_WEEK)-1]); 


I have written code, which uses Java calendar and shows the DAY_OF_WEEK from timestamp. But the default calendar starts from Sunday (1). I want it start from Monday eg. It should return 1 for Monday. Here's my code :

Calender c = Calender.getInstance(TimeZone.getInstance("Australia/Sydney"));
c.setTimeInMillis(1413831601032L);
c.setFirstDayOfWeek(Calender.Monday);
System.out.println(c.get(c.DAY_OF_WEEK));

setFirstDayOfWeek() doesn't help in this case.

The output should be 2 for Tueday, but its showing me 3. Any help will be appreciated.

解决方案

The first day of the week is derived from the current locale. If you don't set the locale of the calendar (Calendar.getInstance(Locale), or new GregorianCalendar(Locale)), it will use the system's default.

public static void main(String[] args) {
    Calendar c = new GregorianCalendar();
    System.out.println(Locale.getDefault() + ": " + c.getFirstDayOfWeek());
}

This should show a different output with different JVM parameters for language/country:

-Duser.language=en -Duser.country=US -> en_US: 1 (Sunday)
-Duser.language=en -Duser.country=GB -> en_GB: 2 (Monday)

Also, You can use the method setFirstDayOfWeek() to set the first day of the week. The method can only affect the return values of WEEK_OF_MONTH or WEEK_OF_YEAR. For DAY_OF_WEEK, it does nothing.

Refer here for more

Also if you see the Calendar.java, you will see that values for days is constant, as below. So that's why it will return 1 for MONDAY, no matter what the first day of the week is set to.

public final static int SUNDAY = 1;

public final static int MONDAY = 2; ....

public final static int SATURDAY = 7;

You can do something as below and manipulate the data, according to the first day you are setting.

[c.get(Calendar.DAY_OF_WEEK) - 1]);

这篇关于如何使Java日历从星期一开始工作日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆