简体   繁体   中英

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

 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 8 version full answer:

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.

I don't know from where you are but Java has an awesome Calendar which allows the following:

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:

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
  1. Get the current moment.
    See this Question: How to initialize a variable of type date in Java?
  2. Determine the first Sunday of the year.
    Handled thoroughly in this question: How to get all the Sunday's of a year
  3. Count weeks between that first Sunday and current moment.
    See the Question: Get the number of weeks between two Dates

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM