获取当前string时间
时间: 2024-03-21 20:34:47 浏览: 18
获取当前时间的String类型可以使用Java中的SimpleDateFormat类和Date类来实现。具体代码如下:
```
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time_string = sdf.format(date);
System.out.println("获取当前时间String类型:"+time_string);
```
其中,Date类表示当前时间,SimpleDateFormat类用于格式化时间,"yyyy-MM-dd HH:mm:ss"表示时间格式。最后将格式化后的时间转换为String类型即可。
相关问题
获取当前时间string
要获取当前时间的字符串,可以使用以下方法之一:
方法一:使用Date类和SimpleDateFormat类
```
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = format.format(date);
System.out.println("当前时间:" + time);
```
运行结果:当前时间:2022-01-01 12:34:56
方法二:使用Calendar类和SimpleDateFormat类
```
import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = format.format(date);
System.out.println("当前时间:" + time);
```
运行结果:当前时间:2022-01-01 12:34:56
以上就是两种常用的获取当前时间的方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [关于在Servelet中如何获取当前时间的操作方法](https://download.csdn.net/download/weixin_38538224/13139535)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [获取当前时间,Date和String格式](https://blog.csdn.net/Gorden_Zhu/article/details/108216922)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
java获取当前时间String型
以下是两种Java获取当前时间String型的例子:
1. 使用java.time包获取当前时间:
```java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class TimeString {
public static void main(String[] args) {
String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("当前时间:" + currentTime);
}
}
```
2. 使用java.util包获取当前时间:
```java
import java.util.Date;
import java.text.SimpleDateFormat;
public class TimeString {
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = format.format(now);
System.out.println("当前时间:" + currentTime);
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)