我试图得到某个日期的星期数,在我的国家,星期一从星期天开始,所以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
更多推荐
发布评论