获取当周第一天是星期日的那一年中的星期数(get number of the week in the year where first day of the week is Sunday)

编程入门 行业动态 更新时间:2024-10-25 04:15:01
获取当周第一天是星期日的那一年中的星期数(get number of the week in the year where first day of the week is Sunday)

我试图得到某个日期的星期数,在我的国家,星期一从星期天开始,所以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);

请注意,我不能使用Time Class,我只能使用Java 7

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

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

最满意答案

我不知道你在哪里,但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

你可以使用locale常量来指定你的位置,我想你会得到正确的周数。

编辑:现在我看到你的代码失败。 请注意,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'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

setFirstDayOfWeek(Calendar.SUNDAY); setMinimalDaysInFirstWeek(7);

this will tell the calendar to make the fist day is sunday and with 7 days minimal week

更多推荐

本文发布于:2023-07-30 00:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1320846.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:那一   年中   星期日   星期   number

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!