java把String时间Thu Dec 01 00:00:00 GMT+08:00 2022转yyyy-MM-dd
时间: 2023-05-18 08:07:09 浏览: 61
可以使用SimpleDateFormat类来实现这个转换,代码如下:
```
String time = "Thu Dec 01 00:00:00 GMT 08:00 2022";
SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = inputFormat.parse(time);
String formattedDate = outputFormat.format(date);
System.out.println(formattedDate);
```
输出结果为:2022-12-01
相关问题
java string yyyy-mm-dd hh:mm:ss 转 string yyyy-mm-dd
可以使用 Java 中的 SimpleDateFormat 类来将字符串格式化成另一种格式。以下是将 "yyyy-MM-dd HH:mm:ss" 格式的字符串转换为 "yyyy-MM-dd" 格式的代码示例:
```
String dateString = "2022-05-28 13:20:30";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormat.parse(dateString);
dateFormat.applyPattern("yyyy-MM-dd");
String newDateString = dateFormat.format(date);
System.out.println(newDateString);
```
输出结果为:
```
2022-05-28
```
这样就成功将 "yyyy-MM-dd HH:mm:ss" 格式的字符串转换为 "yyyy-MM-dd" 格式的字符串了。
使用java 将中央时间Thu Mar 30 00:00:00 CST 2023 转换成 DateTime的yyyy-MM-dd HH:mm:ss格式
可以使用Java中的SimpleDateFormat类来实现:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
String dateString = "Thu Mar 30 00:00:00 CST 2023";
SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
inputFormat.setTimeZone(TimeZone.getTimeZone("CST"));
try {
Date date = inputFormat.parse(dateString);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String outputString = outputFormat.format(date);
System.out.println(outputString);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
输出结果为:
```
2023-03-30 00:00:00
```
注意事项:
- 输入日期字符串的格式必须与SimpleDateFormat中的格式相匹配;
- 需要通过setTimeZone方法设置时区,否则可能会出现时差问题;
- SimpleDateFormat是线程不安全的,不要在多线程环境下共享同一个SimpleDateFormat对象。