gpt4 book ai didi

java.util.Calendar WEEK_OF_YEAR 为第 365 天和第 366 天提供 1

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:28:48 24 4
gpt4 key购买 nike

尝试查找给定日期的星期几。
使用下面的代码。 [Java 1.7]

问题:
2012 年 12 月 29 日 = 第 52 周
2012 年 12 月 30 日 = 第 1 周
2013 年 12 月 30 日 = 第 52 周
2013 年 12 月 31 日 = 第 1 周

当 day-of-year = 365 或 366 时,它会将 week-of-year 设为 1。
看起来有些模 52 发生了。
如何解决这个问题?

import java.util.Calendar;
import java.util.GregorianCalendar;

public class cal2{
public static void main (String[] args) {

Calendar mycal = GregorianCalendar.getInstance();

mycal.setLenient(false);

int year = 2012;
int month = 11; //0=Jan, 11=Dec
int date = 29;

mycal.set(year , 0 , 1);
mycal.setFirstDayOfWeek(mycal.get(mycal.DAY_OF_WEEK));

mycal.set(year,month,date);
System.out.println("\n>>>>>>>>WEEK :"+mycal.get(mycal.WEEK_OF_YEAR));

// System.out.println("\nDATE :"+mycal);

}

最佳答案

SEE HERE来自文档

第一周

Calendar defines a locale-specific seven day week using two parameters: the first day of the week and the minimal days in first week (from 1 to 7). These numbers are taken from the locale resource data when a Calendar is constructed. They may also be specified explicitly through the methods for setting their values.

When setting or getting the WEEK_OF_MONTH or WEEK_OF_YEAR fields, Calendar must determine the first week of the month or year as a reference point. The first week of a month or year is defined as the earliest seven day period beginning on getFirstDayOfWeek() and containing at least getMinimalDaysInFirstWeek() days of that month or year. Weeks numbered ..., -1, 0 precede the first week; weeks numbered 2, 3,... follow it. Note that the normalized numbering returned by get() may be different. For example, a specific Calendar subclass may designate the week before week 1 of a year as week n of the previous year.

关于java.util.Calendar WEEK_OF_YEAR 为第 365 天和第 366 天提供 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16278846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com