Recently in the process of doing projects encountered a problem, is that I use the Java Date Tool class to get weeks when the number of weeks to find with the database (especially the year and year of the critical value) does not correspond, so did some analysis, found that there are some mystery. There are two important parameters for getting the number of weeks
Setting Monday is the beginning of the week
Calendar.setfirstdayofweek (calendar.monday);
The first week of each year at least a few days ODPs function weekofyear more than 4 days
Calendar.setminimaldaysinfirstweek (4);
1, set Monday as the first day of no week, the first week of the year contains at least 4 days
/** * According to the date string is the first weeks of the year * @param date_str format YYYY-MM-DD * @return * * * public static Integer Getweekofyear (String date_str) {try {simpledateformat format = new Simpledate
Format ("Yyyy-mm-dd");
Date date = Format.parse (DATE_STR);
Calendar calendar = Calendar.getinstance ();
Setting Monday is the beginning of the week Calendar.setfirstdayofweek (Calendar.monday);
The first week of each year at least a few days ODPs function WeekOfYear more than 4 days Calendar.setminimaldaysinfirstweek (4);
Calendar.settime (date);
Return Calendar.get (calendar.week_of_year);
catch (Exception e) {return null; }
}
System.out.println ("2017-01-01>>>>>>>>" +getweekofyear ("2017-01-01"));
System.out.println ("2017-01-02>>>>>>>>" +getweekofyear ("2017-01-02"));
System.out.println ("2017-11-12>>>>>>>>" +getweekofyear ("2017-11-12"));
System.out.println ("2017-11-13>>>>>>>>" +getweekofyear ("2017-11-13"));
System.out.println ("2016-05-01>>>>>>>>" +getweekofyear ("2016-05-01"));
System.out.println ("2016-05-02>>>>>>>>" +getweekofyear ("2016-05-02"));
System.out.println ("2017-12-31>>>>>>>>" +getweekofyear ("2017-12-31"));
System.out.println ("2018-01-01>>>>>>>>" +getweekofyear ("2018-01-01")); System.out.println ("2014-12-29>>>>>>>>" +getweekofyear ("2014-12-29")); 1 System.out.println ("2014-12-31>>>>>>>>" +get)WeekOfYear ("2014-12-31")); 1 System.out.println ("2015-12-29>>>>>>>>" +getweekofyear ("2015-12-29")); 53
Get
2017-01-01>>>>>>>>52
2017-01-02>>>>>>>>1
2017-11-12 >>>>>>>>45
2017-11-13>>>>>>>>46
2016-05-01>>> >>>>>17
2016-05-02>>>>>>>>18
2017-12-31>>>>>> >>52
2018-01-01>>>>>>>>1
2014-12-29>>>>>>>>1
2014-12-31>>>>>>>>1
2015-12-29>>>>>>>>53
2, do not make any settings
/**
* based on date string is the first weeks of the year
* @param date_str format yyyy-mm-dd
* @return *
/public static Integer getweekofyear (String date_str) {
try {
SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd"); C10/>date Date = Format.parse (DATE_STR);
Calendar calendar = Calendar.getinstance (); setting Monday is the beginning of the week
// Calendar.setfirstdayofweek (calendar.monday); the first week of each year at least a few days ODPs function weekofyear more than 4 days
// Calendar.setminimaldaysinfirstweek (4);
Calendar.settime (date);
Return Calendar.get (calendar.week_of_year);
} catch (Exception e) {return
null;
}
}
Call
System.out.println ("2017-01-01>>>>>>>>" +getweekofyear ("2017-01-01"));
System.out.println ("2017-01-02>>>>>>>>" +getweekofyear ("2017-01-02"));
System.out.println ("2017-11-12>>>>>>>>" +getweekofyear ("2017-11-12"));
System.out.println ("2017-11-13>>>>>>>>" +getweekofyear ("2017-11-13"));
System.out.println ("2016-05-01>>>>>>>>" +getweekofyear ("2016-05-01"));
System.out.println ("2016-05-02>>>>>>>>" +getweekofyear ("2016-05-02"));
System.out.println ("2017-12-31>>>>>>>>" +getweekofyear ("2017-12-31"));
System.out.println ("2018-01-01>>>>>>>>" +getweekofyear ("2018-01-01")); System.out.println ("2014-12-29>>>>>>>>" +getweekofyear ("2014-12-29")); 1 System.out.println ("2014-12-31>>>>>>>>" +get)WeekOfYear ("2014-12-31")); 1 System.out.println ("2015-12-29>>>>>>>>" +getweekofyear ("2015-12-29")); 53
Get
2017-01-01>>>>>>>>1
2017-01-02>>>>>>>>1
2017-11-12> >>>>>>>46
2017-11-13>>>>>>>>46
2016-05-01>>>> >>>>19
2016-05-02>>>>>>>>19
2017-12-31>>>>>>> >1
2018-01-01>>>>>>>>1
2014-12-29>>>>>>>>1
2014-12-31>>>>>>>>1
2015-12-29>>>>>>>>1
Summarize:
The two values are set with different parameters. The number of weeks is different, because by default the calendar is the Sunday as the first day of the week. If you are making inquiries or teaching research in the weeks of the logic, must be agreed to set parameters, or you may not get the data you want.