Java / Android - 将GMT时间字符串转换为本地时间 [英] Java/Android - Convert a GMT time string to local time
问题描述
异常是未报告的异常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屋!