当前位置:网站首页>calendar.getActualMaximum(calendar.DAY_OF_MONTH)的坑点
calendar.getActualMaximum(calendar.DAY_OF_MONTH)的坑点
2022-04-22 05:54:00 【长城Ol】
问题代码如下
public static Date monthLastDate(Integer year, Integer month) throws ParseException {
if (year == null || month == null || month > 12 || month < 1) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
int i = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
无论方法传入的month为何值,i值总是当前月份的最大值。
因为Calendar类在初始化时系统时间时已经设置了Calendar.DAY_OF_MONTH的最大值,当我们修改了年份和月份的值,Calendar并没有同时去修改设置对应字段的ActualMaximum的值。
| 方法 | 说明 |
|---|---|
| void clear() | 此方法设置此日历的所有日历字段值和时间值(毫秒从历元至偏移量)未定义。 |
| int getActualMaximum(int field) | 此方法返回指定日历字段可能拥有的最大值,鉴于此日历时间值。 |
解决办法:
如果要修改创建的calendar的值,首先先清空calendar所有初始的默认值,然后在修改值。
可以理解为使用clear()方法清除calendar的缓存。
public static Date monthLastDate(Integer year, Integer month) throws ParseException {
if (year == null || month == null || month > 12 || month < 1) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
int i = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
问题探究
我们看一下他的set方法,当我们set字段A的值时,并不会把所有跟A相关的值同时修改掉,只是设置了一个状态,表时其他字段还没有设置。
public void set(int field, int value)
{
// If the fields are partially normalized, calculate all the
// fields before changing any fields.
if (areFieldsSet && !areAllFieldsSet) {
computeFields();
}
internalSet(field, value);
isTimeSet = false;
areFieldsSet = false;
isSet[field] = true;
stamp[field] = nextStamp++;
if (nextStamp == Integer.MAX_VALUE) {
adjustStamp();
}
}
当我们get某个字段B的值时,会调用计算所有字段的方法,计算完毕后再去取这个字段B的值。
public int get(int field)
{
complete();
return internalGet(field);
}
下面计算所有字段的方法
/** * Fills in any unset fields in the calendar fields. First, the {@link * #computeTime()} method is called if the time value (millisecond offset * from the <a href="#Epoch">Epoch</a>) has not been calculated from * calendar field values. Then, the {@link #computeFields()} method is * called to calculate all calendar field values. */
protected void complete()
{
if (!isTimeSet) {
updateTime();
}
if (!areFieldsSet || !areAllFieldsSet) {
computeFields(); // fills in unset fields
areAllFieldsSet = areFieldsSet = true;
}
}
```
版权声明
本文为[长城Ol]所创,转载请带上原文链接,感谢
https://blog.csdn.net/baijunzhijiang_01/article/details/102845375
边栏推荐
- Mysql 根据某一列的值 循环添加序号
- Jasmine X4 tutorial instructions, graphic explanation tutorial
- The difference between hash mode and history mode
- OpenInfra Live | 九州云黄舒泉当选Track Chair并带来精彩主题分享
- PostgreSQL使用clickhousedb_fdw访问ClickHouse
- A problem caused by MySQL gap lock
- MySQL中的锁与事务
- 数美科技社交行业未成年人保护解决方案:守护未成年人的“社交圈”
- congratulations! China Mobile Smart Home Center was selected in the list of ICT industry in 2020
- Chemex
猜你喜欢

优秀 | 九州云入选首届混合云大会优秀案例及产业全景图

Async and await

九州云入选36氪最具登陆科创板潜力企业榜单

JS reverse: omnipotent hook function

恭喜! 中移动智家中心入选2020年度ICT行业龙虎榜

ITS智能服务优秀企业年度榜单出炉,九州云荣获“2021信创运维10强”

Jasmine x4-q and jasmine x4-1u, detailed comparison

One mountain is higher than another. Comparison of parameters between panda H3 and panda h3plus

Distributed transaction solution Seata

JS debugging interference - infinite debugger bypass
随机推荐
Method of querying cumulative value in MySQL
喜报|九州云获评“浙江省高新技术企业研究开发中心”
Nacos源码启动报错解决方法
Outstanding | Kyushu cloud was selected as an excellent case and industry panorama of the first hybrid cloud conference
hash模式与history模式之间的区别
PostgreSQL如何对URL进行解析
PostgreSQL中的MVCC 事务隔离
使用pgBackRest并行归档解决wal堆积问题
数美科技受邀参加信通院内容治理标签研讨会
PostgreSQL 13.3、12.7、11.12、10.17和9.6.22发布了!
mysql 基础知识2
有点意思的质数题HDU5750
synchronized 关键字和volatile关键字的作用和区别
MySQL 5.7.16 decompression installation process
分布式事务解决方案Seata
解决Windows2012 R2下安装PostgreSQL报错的问题
通过代码理解分布式事务:XA模式
Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: “/?k=“
PostgreSQL在线调整最大连接数方案设想
Redis持久化