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

2019-01-09 03:27发布

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);
int nweek = cal.WEEK_OF_YEAR;
int year = cal.YEAR;

But now I don't know how to get the date of the first day of that week. I've been looking in Calendar, Date, DateFormat but nothing that may be useful...

Any suggestion? (working in Java)

7条回答
神经病院院长
2楼-- · 2019-01-09 03:46

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;
}
查看更多
做自己的国王
3楼-- · 2019-01-09 03:53

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!
    }
查看更多
时光不老,我们不散
4楼-- · 2019-01-09 03:54

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)

查看更多
劳资没心,怎么记你
5楼-- · 2019-01-09 03:56

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

查看更多
干净又极端
6楼-- · 2019-01-09 03:58

Here's some quick and dirty code to do this. This code creates a calendar object with the date of the current day, calculates the current day of the week, and subtracts the day of the week so you're on the first one (Sunday). Although I'm using DAY_OF_YEAR it goes across years fine (on 1/2/10 it'll return 12/27/09 which is right).

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DOW {

    public static void main(String[] args) {
        DOW dow = new DOW();
        dow.doIt();
        System.exit(0);
    }

    private void doIt() {
        Date curr = new Date(); 
        Calendar cal = Calendar.getInstance(); 
        cal.setTime(curr); 
        int currentDOW = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DAY_OF_YEAR, (currentDOW * -1)+1);

        Format formatter = new SimpleDateFormat("MM/dd/yy");
        System.out.println("First day of week="+formatter.format(cal.getTime()));
    }
}
查看更多
Rolldiameter
7楼-- · 2019-01-09 04:02

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);
查看更多
登录 后发表回答