怎么获取当前时间 要求年月日时分秒 用java代码实现
时间: 2023-09-20 18:14:39 浏览: 118
要获取当前时间,并以年月日时分秒的格式显示,可以使用Java中的Date类和SimpleDateFormat类。以下是一个示例代码:
```java
import java.util.Date;
import java.text.SimpleDateFormat;
public class GetCurrentTime {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = dateFormat.format(date);
System.out.println(currentTime);
}
}
```
通过以上代码,你可以获取当前的年月日时分秒,并以字符串的形式显示出来。例如,输出可能是:2021-08-20 15:30:45。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [JAVA 获取当前时间(年月日时分秒)](https://blog.csdn.net/qq_39207963/article/details/122663142)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *3* [java怎么获取当前时间年月日时分秒,保准看明白!](https://blog.csdn.net/m0_57286679/article/details/117988774)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文