JavaAndroid-将GMT时间字符串转换为本地时间

JavaAndroid-将GMT时间字符串转换为本地时间,第1张

Java / Android-将GMT时间字符串转换为本地时间

您提供的用于 解析 的格式字符串与您实际获得的文本格式不匹配。您需要先解析,然后再格式化。看起来像您想要的:

SimpleDateFormat inputFormat = new SimpleDateFormat(    "EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd, yyyy h:mm a");// Adjust locale and zone appropriatelyDate date = inputFormat.parse(inputText);String outputText = outputFormat.format(date);

编辑:这是简短但完整的程序形式的相同代码,并带有您的示例输入:

import java.util.*;import java.text.*;public class Test {    public static void main(String[] args) throws ParseException {        String inputText = "Tue May 21 14:32:00 GMT 2012";        SimpleDateFormat inputFormat = new SimpleDateFormat ("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);        inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));        SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd, yyyy h:mm a");        // Adjust locale and zone appropriately        Date date = inputFormat.parse(inputText);        String outputText = outputFormat.format(date);        System.out.println(outputText);    }}

您可以编译并运行 该确切代码 吗?



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5615676.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存