格式化Thu, 17 Aug 2023 09:44:30 +0800 格式时间

要将格式为EEE, dd MMM yyyy HH:mm:ss Z的时间字符串转换为标准时间,可以按照以下步骤操作:


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    
    
     public static void main(String[] args) {
    
    
        String inputDate = "Thu, 17 Aug 2023 09:44:30 +0800";
        String inputFormat = "EEE, dd MMM yyyy HH:mm:ss Z";
        String outputFormat = "yyyy-MM-dd HH:mm:ss";

        try {
    
    
            SimpleDateFormat inputFormatter = new SimpleDateFormat(inputFormat, Locale.ENGLISH);
            Date date = inputFormatter.parse(inputDate);
            SimpleDateFormat outputFormatter = new SimpleDateFormat(outputFormat);
            String outputDate = outputFormatter.format(date);
            System.out.println(outputDate);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
 
}

在上面的代码中,我们使用了SimpleDateFormat类的两个实例,
一个用于解析输入日期字符串,
另一个用于将日期格式化为输出格式。
为了在转换过程中保持一致的时间,我们使用了outputFormatter.setTimeZone(TimeZone.getTimeZone("GMT"))来将输出时间设置为GMT或UTC时区

运行以上代码,输出结果将为:2023-08-17 02:52:02

请注意,
由于输出时间已调整为GMT或UTC时区,可能与原始输入时间的时区不同。

或使用hutool

        String inputDate = "Thu, 17 Aug 2023 09:44:30 +0800";
        String inputFormat = "EEE, dd MMM yyyy HH:mm:ss Z";
       
DateUtil.parse(inputDate,inputFormat,Locale.ENGLISH)

猜你喜欢

转载自blog.csdn.net/qq_35385687/article/details/132337001
今日推荐