当前位置:网站首页>calendar. Pit point of getactualmaximum (calendar. Day_of_month)
calendar. Pit point of getactualmaximum (calendar. Day_of_month)
2022-04-23 05:01:00 【Great Wall ol】
The problem code is as follows
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();
}
Regardless of the method passed in month Why is it worth ,i Value is always the maximum value of the current month .
because Calendar The system time of class initialization has been set Calendar.DAY_OF_MONTH The maximum of , When we change the values of year and month ,Calendar It is not necessary to modify and set the corresponding fields at the same time ActualMaximum Value .
| Method | explain |
|---|---|
| void clear() | This method sets all calendar field values and time values for this calendar ( Milliseconds from epoch to offset ) Undefined . |
| int getActualMaximum(int field) | This method returns the maximum value that the specified calendar field may have , Given this calendar time value . |
terms of settlement :
If you want to modify the created calendar Value , Empty first calendar All initial defaults , Then modify the value .
Can be understood as using clear() Method to remove calendar The cache of .
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();
}
Question inquiry
Let's take a look at his set Method , When we set Field A The value of , Not all with A The relevant values are modified at the same time , Just set a state , Other fields have not been set in the table .
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();
}
}
When we get A field B The value of , It will call the method of calculating all fields , After the calculation, go to get this field B Value .
public int get(int field)
{
complete();
return internalGet(field);
}
Here's how to calculate all fields
/** * 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;
}
}
```
版权声明
本文为[Great Wall ol]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220552270974.html
边栏推荐
- L2-011 play binary tree (build tree + BFS)
- COM in wine (2) -- basic code analysis
- Graduation project
- Differences between redis and MySQL
- [2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测
- 机器学习---线性回归
- Wechat payment function
- Details related to fingerprint payment
- Manually write smart pointer shared_ PTR function
- vscode ipynb文件没有代码高亮和代码补全解决方法
猜你喜欢

使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘

Field injection is not recommended using @ Autowired
![[database] MySQL multi table query (I)](/img/c7/43167aa6169c24a8d9734032775335.png)
[database] MySQL multi table query (I)

MySQL 慢查询

Deep learning notes - fine tuning

Wine (COM) - basic concept

Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)

MySQL slow query

A trinomial expression that causes a null pointer

Sword finger offer: the median in the data stream (priority queue large top heap small top heap leetcode 295)
随机推荐
Machine learning - linear regression
[WinUI3]编写一个仿Explorer文件管理器
Making message board with PHP + MySQL
Manually write smart pointer shared_ PTR function
List&lt; Map&gt; Replication: light copy and deep copy
Introduction to load balancing
MySQL slow query
Com alibaba. Common methods of fastjson
Other problems encountered in debugging fingerprints
[2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测
Arduino UNO r3+LCD1602+DHT11
Use the built-in function of win to transfer files between two computers in the same LAN (the speed is the same as that between local disks)
PHP counts the number of files in the specified folder
Informatics Aosai yibentong 1212: letters | openjudge 2.5 156: Letters
PHP+MySQL 制作留言板
2022/4/22
AQS source code reading
L2-011 玩转二叉树(建树+BFS)
[winui3] Écrivez une copie du gestionnaire de fichiers Explorer
How to exit VIM