当前位置:网站首页>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
边栏推荐
- Day. JS common methods
- 负载均衡简介
- [2021] Spatio-Temporal Graph Contrastive Learning
- Leetcode -- heuristic search
- Differences between redis and MySQL
- 信息学奥赛一本通 1212:LETTERS | OpenJudge 2.5 156:LETTERS
- Progress of innovation training (III)
- Detailed explanation of the differences between TCP and UDP
- A trinomial expression that causes a null pointer
- 2022/4/22
猜你喜欢
Making message board with PHP + MySQL
Repair of self calibration SPC failure of Tektronix oscilloscope dpo3054
Innovation training (VI) routing
Excel protects worksheets and workbooks from damage
[database] MySQL basic operation (basic operation ~)
Deep learning notes - data expansion
[2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测
Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘
DIY 一个 Excel 版的子网计算器
PHP+MySQL 制作留言板
随机推荐
Making message board with PHP + MySQL
Detailed explanation of the differences between TCP and UDP
Define defines constants and macros, pointers and structures
redis和mysql区别
Innovation training (II) task division
Download PDF from HowNet (I don't want to use CAJViewer anymore!!!)
vscode ipynb文件没有代码高亮和代码补全解决方法
拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
Introduction to load balancing
List&lt; Map&gt; Replication: light copy and deep copy
深度学习笔记 —— 语义分割和数据集
Innovation training (V) configuration information
Uglifyjs compress JS
《2021多多阅读报告》发布,95后、00后图书消费潜力攀升
Deep learning notes - semantic segmentation and data sets
Sword finger offer: push in and pop-up sequence of stack
Machine learning - linear regression
Implementation of switching windows and capturing data in selenium mode
#define 定义常量和宏,指针和结构体
Perfect test of coil in wireless charging system with LCR meter