- String today = "2013-01-14";
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Date date = null;
- try {
- date = format.parse(today);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Calendar calendar = Calendar.getInstance();
- calendar.setFirstDayOfWeek(Calendar.MONDAY);
- calendar.setTime(date);
- System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));
A somewhat complicated code is used to judge the number of weeks that the date belongs to in the current year. When looking at the Calendar class, I saw WEEK_OF_YEAR, which is very practical. But there is a small problem with the time, for example, 2010-01-03, the returned result is 2 (that is, the second week of 2010), because the United States uses Sunday as the first day of the week.
I thought of changing the starting day of the week, setFirstDayOfWeek(int value), passed a 1, and wanted to set Monday as the first day, but it didn't work. After checking the document, it will be fine to change it to Calendar.MONDAY, but I don’t know why it is not easy to use 1, but just use Calendar.MONDAY?
Supplement: I checked the "constant field value" again, and found that the value of MONDAY is not 1 at all, but 2.
SUNDAY :1
WORLD :2
TUESDAY :3
WEDNESDAY :4
THURSDAY : 5
FRIDAY : 6
SATURDAY :7