[英]get number of the week in the year where first day of the week is Sunday
I'm trying to get the number of the week for a date , In my country the week begins on Sunday so the week number of 6/5/2016 is 23 but it returning 22 because the ISO week in JAVA starts from Monday , I have used the following methods but it's not working我正在尝试获取某个日期的周数,在我的国家/地区,一周从星期日开始,因此 6/5/2016 的周数是 23,但它返回 22,因为 JAVA 中的 ISO 周从星期一开始,我使用了以下方法但它不起作用
mCalendar = Calendar.getInstance();
int weekNum = mCalendar.get(Calendar.WEEK_OF_YEAR); //returns 22 I need 23
// I have tried the following method but it has no effect
mCalendar.setFirstDayOfWeek(Calendar.SUNDAY);
note that I can't use the Time Class I can only use Java 7请注意,我不能使用 Time Class 我只能使用 Java 7
Java 8 version full answer: Java 8 版本完整答案:
public<T extends Temporal> long getWeekNumber(T tObj) {
DayOfWeek firstDayOfWeek = DayOfWeek.SUNDAY; // set your
Temporal firstDayOfThisYear = tObj.with(TemporalAdjusters.firstDayOfYear());
Temporal firstDayOfWeekInMonth = firstDayOfThisYear
.with(TemporalAdjusters.dayOfWeekInMonth(1, firstDayOfWeek));
return ChronoUnit.WEEKS.between(tObj, firstDayOfWeekInMonth);
}
The parameter can be any Temporal
type, even the Temporal
itself.参数可以是任何
Temporal
类型,甚至是Temporal
本身。
I don't know from where you are but Java has an awesome Calendar which allows the following:我不知道你在哪里,但 Java 有一个很棒的日历,它允许以下内容:
Calendar calendar = Calendar.getInstance(Locale.TRADITIONAL_CHINESE);
int weekNumber = calendar.get(Calendar.WEEK_OF_YEAR);
System.out.println("Number of week: " + weekNumber);
// produces 24
Calendar calendar = Calendar.getInstance(Locale.UK);
int weekNumber = calendar.get(Calendar.WEEK_OF_YEAR);
System.out.println("Number of week: " + weekNumber);
// produces 22
You could use the locale constants to specify your location and i think you will get the right number of weeks.您可以使用语言环境常量来指定您的位置,我认为您将获得正确的周数。
Edit: Now I see the failure in your code.编辑:现在我看到你的代码失败了。 Please note that Java works from the top to the button of your code:
请注意,Java 从代码的顶部到按钮工作:
Calendar calendar = Calendar.getInstance();
// First set the first day of the week ...
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
// ... and than you could ask the calendar for the week
int weekNumber = calendar.get(Calendar.WEEK_OF_YEAR);
// will produce 23
System.out.println("Number of week: " + weekNumber);
I don't know where you are from.我不知道你来自哪里。 I'm french and that works perfectly this way :
我是法国人,这样完美地工作:
Calendar mCalendar = Calendar.getInstance(Locale.FRANCE);
mCalendar.setFirstDayOfWeek(Calendar.SUNDAY);
int weekNum = mCalendar.get(Calendar.WEEK_OF_YEAR);
System.out.println(weekNum); // --> 23
I've just figured out how to change it you need to set up two things 1-first day of the week 2-the minimal day of week我刚刚想出了如何更改它,您需要设置两件事 1-一周的第一天 2-一周中的最少一天
setFirstDayOfWeek(Calendar.SUNDAY);
setMinimalDaysInFirstWeek(7);
this will tell the calendar to make the fist day is sunday and with 7 days minimal week这将告诉日历将第一天设为星期日,最少一周为 7 天
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.