How to get first day of a given week number in Java

前端 未结 7 633
独厮守ぢ
独厮守ぢ 2020-12-01 12:38

Let me explain myself. By knowing the week number and the year of a date:

Date curr = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(curr);
i         


        
相关标签:
7条回答
  • 2020-12-01 13:02

    Try this:

    public static Calendar setWeekStart(Calendar calendar) {
      while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
        calendar.add(Calendar.DATE, -1);
      }
      setDayStart(calendar); // method which sets H:M:S:ms to 0
      return calendar;
    }
    
    0 讨论(0)
  • 2020-12-01 13:03

    Yet another way...

        GregorianCalendar cal = new GregorianCalendar();
        cal.clearTime();
        Integer correction = 1-cal.get(GregorianCalendar.DAY_OF_WEEK)
        cal.add(Calendar.DATE, correction);
    

    cal is now the first day of the week

    1-cal.get(GregorianCalendar.DAY_OF_WEEK)

    evaluates to 1-1 for Sunday (first day of week in my Locale) and 1-2 for Monday, so this will give you the correction needed to rewind the clock back to Sunday

    0 讨论(0)
  • 2020-12-01 13:04

    I haven't done much Date stuff in java but a solution might be:

    cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_WEEK));
    

    Logic:

    Get the day of the week and substract it from the current date (might need -1, depending on wether you need monday to be first day of the week or sunday)

    0 讨论(0)
  • 2020-12-01 13:09

    Try the Gregorian Calendar algorithm:

    public int getFirstDay(int m, int year)
        {
            int k=1;
            int c, y, w, M=0;
            if(m>2 && m<=12) M=m-2;
            else if(m>0 && M<=2)
            {
                M=m+10;
                year-=1;
            }
            c=year/100;
            y=year%100;
            w=(int)((k+(Math.floor(2.6*M - 0.2))-2*c+y+(Math.floor(y/4))+(Math.floor(c/4)))%7);//a fantastic formula           
            if(w<0) w+=7;
            return w;//thus the day of the week is obtained!
        }
    
    0 讨论(0)
  • 2020-12-01 13:11

    Be cautious with those, calendar.get(Calendar.WEEK_OF_YEAR) returns 1 if it is end of December and already a week that ends in the next year.

    Using

    //            cal2.set(Calendar.WEEK_OF_YEAR, cal.get(Calendar.WEEK_OF_YEAR));
    //            cal2.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    

    sets the cal2 date to e.g. end of 2009 on 29/12/2010 !!

    I used this:

    cal2.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR)); //to round at the start of day
    cal2.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    cal2.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //to round at the start of week
    

    I also make sure that weeks in my calendars, no matter what locale they are in, are starting on Mondays:

    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal2.setFirstDayOfWeek(Calendar.MONDAY);
    
    0 讨论(0)
  • 2020-12-01 13:15

    Those fields does not return the values. Those are constants which identifies the fields in the Calendar object which you can get/set/add. To achieve what you want, you first need to get a Calendar, clear it and set the known values. It will automatically set the date to first day of that week.

    // We know week number and year.
    int week = 3;
    int year = 2010;
    
    // Get calendar, clear it and set week number and year.
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(Calendar.WEEK_OF_YEAR, week);
    calendar.set(Calendar.YEAR, year);
    
    // Now get the first day of week.
    Date date = calendar.getTime();
    

    Please learn to read the javadocs to learn how to use classes/methods/fields and do not try to poke random in your IDE ;)

    That said, the java.util.Date and java.util.Calendar are epic failures. If you can, consider switching to Joda Time.

    0 讨论(0)
提交回复
热议问题