date 类型转string
时间: 2023-09-26 08:11:53 浏览: 103
要将Date类型转换为String类型,可以使用SimpleDateFormat类的format()方法。该方法接受一个Date对象作为参数,并返回按照指定格式转换后的字符串。首先,需要创建一个SimpleDateFormat对象,指定所需的日期格式。然后,调用format()方法将Date对象转换为字符串。以下是一个示例代码:
```
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringExample {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = sdf.format(date);
System.out.println(dateString);
}
}
```
在这个示例中,我们创建了一个Date对象,并指定了日期格式为"yyyy-MM-dd HH:mm:ss"。然后,使用SimpleDateFormat的format()方法将Date对象转换为字符串,并将结果打印出来。请根据实际需求调整日期格式。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [String类型转localDate,date转localDate的实现代码](https://download.csdn.net/download/weixin_38742409/12721056)[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/zyp00793/article/details/113754468)[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 ]
阅读全文
相关推荐

















