09-Jun-2023转yyyy-mm-dd java实现
时间: 2023-08-21 09:05:13 浏览: 41
可以使用Java中的SimpleDateFormat类来实现日期格式转换,以下是将"09-Jun-2023"转换为"2023-06-09"的示例代码:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateConvertExample {
public static void main(String[] args) throws Exception {
String inputDate = "09-Jun-2023";
SimpleDateFormat inputFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date = inputFormat.parse(inputDate);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
String outputDate = outputFormat.format(date);
System.out.println(outputDate);
}
}
```
运行结果为:
```
2023-06-09
```
相关问题
java new date转为yyyy-mm-dd格式 返回Date
在Java中,可以使用SimpleDateFormat类将日期转换为指定格式的字符串,然后再将字符串解析为Date对象。
以下是示例代码:
```java
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
public static Date formatDate(Date date) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateString = dateFormat.format(date);
return dateFormat.parse(dateString);
}
}
```
解释一下代码:
- `SimpleDateFormat` 是一个用于格式化和解析日期的类。
- `format` 方法将 `date` 对象格式化为 "yyyy-MM-dd" 格式的字符串。
- `parse` 方法将字符串解析为 `Date` 对象。
- `DateFormat` 是一个抽象类,`SimpleDateFormat` 是其子类,它定义了一组格式化和解析日期的方法。
使用示例:
```java
Date date = new Date();
try {
date = DateUtils.formatDate(date);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(date);
```
输出结果类似于:`Thu Jun 03 00:00:00 CST 2021`,但是在内部已经将其格式化为 "yyyy-MM-dd" 格式。
如何将Thu Jun 08 15:09:58 CST 2023格式转换为yyyy-MM-dd HH:mm:ss格式
可以使用Java中的SimpleDateFormat类来实现将"Thu Jun 08 15:09:58 CST 2023"格式的日期转换为"yyyy-MM-dd HH:mm:ss"格式的日期,具体代码如下:
```
String dateString = "Thu Jun 08 15:09:58 CST 2023";
SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf1.parse(dateString);
String newDateString = sdf2.format(date);
System.out.println(newDateString);
```
解释一下上述代码的具体步骤:
1. 定义一个字符串变量dateString来存储要转换的日期字符串。
2. 创建一个SimpleDateFormat对象sdf1,指定它的日期格式为"EEE MMM dd HH:mm:ss zzz yyyy",这个格式与待转换的日期字符串格式相同,同时指定Locale.US表示使用美国地区的日期格式。
3. 创建另一个SimpleDateFormat对象sdf2,指定它的日期格式为"yyyy-MM-dd HH:mm:ss",这个格式是我们要转换成的格式。
4. 使用sdf1的parse()方法将字符串dateString解析为Date对象。
5. 使用sdf2的format()方法将Date对象格式化为"yyyy-MM-dd HH:mm:ss"格式的字符串。
6. 最后输出转换后的字符串newDateString。
执行上述代码后,输出的结果为"2023-06-08 15:09:58"。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)