gpt4 book ai didi

java - Java 日历类中的 Calendar.WEEK_OF_MONTH 和 Calendar.DAY_OF_WEEK IN MONTH 有什么区别?

转载 作者:搜寻专家 更新时间:2023-10-30 19:48:43 25 4
gpt4 key购买 nike

Java 的 Calendar 类提供了两个字段:WEEK_OF_MONTH 和 DAY_OF_WEEK_IN_MONTH。有人可以向我解释其中的区别吗?使用以下代码进行测试时,它们似乎都返回相同的值:

Calendar date = Calendar.getInstance();
date.set(2011,5,29);
int weekNo1 = date.get(Calendar.WEEK_OF_MONTH);
int weekNo2 = date.get(Calendar.DAY_OF_WEEK_IN_MONTH);

最佳答案

  • Calendar.WEEK_OF_MONTH 仅返回“当前月份的当前周数”
  • Calendar.DAY_OF_WEEK 仅返回“从上周日开始的当前周中的当前日期”
  • Calendar.DAY_OF_WEEK_IN_MONTH 返回“如果当前日期是一个月中的第 N 天”“如果今天是第 3 个星期三则返回 3在一个月内”

所以我在 2016 年 12 月 21 日写下这篇文章:

enter image description here

这就是我得到的:

Calendar today = Calendar.getInstance();
System.out.println(today.get(Calendar.DAY_OF_WEEK)); //outputs 4, as today is 4th day in this week which started on 18
System.out.println(today.get(Calendar.DAY_OF_WEEK_IN_MONTH)); //outputs 3, as today is "3rd Wednesday of this month". Earlier two wednesday were on 7th and 14th
System.out.println(today.get(Calendar.WEEK_OF_MONTH)); //outputs 4, as currently 4th week of a month is running

关于java - Java 日历类中的 Calendar.WEEK_OF_MONTH 和 Calendar.DAY_OF_WEEK IN MONTH 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6538791/

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