calendar类获取当前日期
时间: 2023-07-04 12:16:00 浏览: 49
可以使用Python中的datetime模块来获取当前日期,具体实现代码如下:
```python
import datetime
now = datetime.datetime.now()
print("当前日期为:", now.date())
```
输出结果类似于:
```
当前日期为: 2021-10-20
```
其中,`datetime.datetime.now()`获取当前时间,再使用`.date()`方法获取日期部分。
相关问题
Calendar获取当前日期
你可以使用Java的Calendar类来获取当前日期。以下是一个获取当前日期的示例代码:
```
// 创建Calendar对象
Calendar calendar = Calendar.getInstance();
// 获取年、月、日
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始,需要加1
int day = calendar.get(Calendar.DAY_OF_MONTH);
// 输出当前日期
System.out.println("当前日期:" + year + "-" + month + "-" + day);
```
这将输出当前日期,例如:当前日期:2021-09-23。
java使用Calendar类获取当前时间日期,要求按照两位数显示。
可以使用以下代码实现:
```java
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);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.printf("%02d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
}
}
```
输出结果类似于:`2022-01-01 23:59:59`,其中 `%02d` 表示按照两位数显示数字。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)