Calendar将今年最初的年月日放到里面
时间: 2023-07-30 22:09:09 浏览: 42
如果您是指在程序中将当前的年月日存储到一个 Calendar 对象中,可以使用以下代码:
```java
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2022); // 设置年份为2022
calendar.set(Calendar.MONTH, Calendar.JANUARY); // 设置月份为1月
calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置日期为1号
```
上述代码会创建一个 Calendar 对象,并将其设置为2022年1月1日。如果您想要获取当前的年月日,可以使用以下代码:
```java
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR); // 获取当前年份
int month = calendar.get(Calendar.MONTH) + 1; // 获取当前月份,注意要加1
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); // 获取当前日期
```
上述代码会获取当前的年月日,并将其分别存储在 `year`、`month` 和 `dayOfMonth` 变量中。
相关问题
Calendar方法获取当前年月日
您好,可以使用以下代码获取当前年月日:
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("当前日期为:" + year + "年" + month + "月" + day + "日");
}
}
javaCalendar方法 只保留年月日
您可以使用以下代码来只保留年月日:
```
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
System.out.println(calendar.getTime());
}
}
```
这将会输出当前日期的年月日,而时分秒毫秒都将被设置为0。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)