Java / Android - 将GMT时间字符串转换为本地时间 [英] Java/Android - Convert a GMT time string to local time

查看:225
本文介绍了Java / Android - 将GMT时间字符串转换为本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我有一个字符串,说星期二5月21日14:32:00格林尼治标准时间2012我想将此字符串转换为当地时间2012年5月21日下午2:32的格式。我试过SimpleDateFormat(MM dd,yyyy hh:mm a)。parse(),但它抛出一个异常。那么我应该怎么做?



异常是未报告的异常java.text.ParseException;必须被捕获或声明被抛出。



在行 Date date = inputFormat.parse(inputText);



代码我在TextMate上运行:

  public class test {
public static void main(String arg []){
String inputText =Tue May 22 14:52:00 GMT 2012;
SimpleDateFormat inputFormat = new SimpleDateFormat(
EEE MMM dd HH:mm:ss'GMT'yyyy,Locale.US);
inputFormat.setTimeZone(TimeZone.getTimeZone(Etc / UTC));
SimpleDateFormat out = new SimpleDateFormat(MMM dd,yyyy h:mm a);
Date date = inputFormat.parse(inputText);
String output = out.format(date);
System.out.println(output);
}
}


解决方案

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

  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);
//适当调整区域设置和区域

日期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);
//适当地调整区域设置和区域
日期date = inputFormat.parse(inputText);
String outputText = outputFormat.format(date);
System.out.println(outputText);
}
}

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


Ok, so I have a string, say "Tue May 21 14:32:00 GMT 2012" I want to convert this string to local time in the format May 21, 2012 2:32 pm. I tried SimpleDateFormat("MM dd, yyyy hh:mm a").parse(), but it threw an exception. So what should I do?

The exception is "unreported exception java.text.ParseException; must be caught or declared to be thrown."

in the line Date date = inputFormat.parse(inputText);

The code I ran on TextMate:

public class test{
    public static void main(String arg[]) {
        String inputText = "Tue May 22 14:52:00 GMT 2012";
        SimpleDateFormat inputFormat = new SimpleDateFormat(
            "EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);
        inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
        SimpleDateFormat out = new SimpleDateFormat("MMM dd, yyyy h:mm a");
        Date date = inputFormat.parse(inputText);
        String output = out.format(date);
       System.out.println(output);
    }
}

解决方案

The format string you provided for parsing doesn't correspond with the text format you've actually got. You need to parse first, then format. It looks like you want:

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);

EDIT: Here's the same code in the form of a short but complete program, with your sample input:

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);
    }
}

Can you compile and run that exact code?

这篇关于Java / Android - 将GMT时间字符串转换为本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆