android 中给出 GMT 格式时如何将日期和时间从一个时区转换为另一个时区
How to convert date and time from one time zone to another in when GMT format is given in android
我有一个特定的日期和时间 +10:00 GMT (Australia/Melbourne),它来自 google api,格式如下:-
2015-05-12T14:00:00.000+10:00
现在,我想根据设备当前时区转换上述时间。
例如:-
以上格林威治标准时间+5:30(印度)的时间应该是2015-05-129:30:00
如何实现?
我下面的代码是:-
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date parsed = null;
try {
parsed = sourceFormat.parse("2015-05-12 14:00:00");
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // => Date is in UTC now
TimeZone tz = TimeZone.getTimeZone(TimeZone.getDefault().getID());
SimpleDateFormat destFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
destFormat.setTimeZone(tz);
String result = destFormat.format(parsed);
System.out.println(result);
但结果是 2015-05-12 19:30 这是错误的..
提到 this 我认为你对 getTimeZone("GMT")
的理解是错误的
你应该使用 getTimeZone("GMT+10:00"));
因为 GMT 是 +/-0:00
看看我发布的文档:)
您还可以使用 Joda library 让生活更轻松。
private String showTimeStringExample(String timeStamp) {
//timestamp = "2015-05-12T14:00:00.000+10:00"
// use Joda to parse the time stamp and convert it to java
final Date date = ISODateTimeFormat.dateTimeParser().parseDateTime(timeStamp).toDate();
// use the format you had in your question, setting the locale to the device's default
SimpleDateFormat destFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss",
getResources().getConfiguration().locale
);
// comes out as "2015-05-12 05:00:00 in the UK
return destFormat.format(date);
}
Gradle 依赖关系:
compile 'joda-time:joda-time:2.3'
请试试这个,它在这里工作
try {
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT+10"));
//Local time zone
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//Time in GMT
String date = dateFormatLocal.format(dateFormatGmt.parse("2015-05-12 14:00:00"));
LogUtil.LOGD(Constants.ACTIVITY_TAG, "Date- " + date);
}catch (ParseException e) {
e.printStackTrace();
}
我有一个特定的日期和时间 +10:00 GMT (Australia/Melbourne),它来自 google api,格式如下:-
2015-05-12T14:00:00.000+10:00
现在,我想根据设备当前时区转换上述时间。
例如:-
以上格林威治标准时间+5:30(印度)的时间应该是2015-05-129:30:00
如何实现?
我下面的代码是:-
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date parsed = null;
try {
parsed = sourceFormat.parse("2015-05-12 14:00:00");
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // => Date is in UTC now
TimeZone tz = TimeZone.getTimeZone(TimeZone.getDefault().getID());
SimpleDateFormat destFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
destFormat.setTimeZone(tz);
String result = destFormat.format(parsed);
System.out.println(result);
但结果是 2015-05-12 19:30 这是错误的..
提到 this 我认为你对 getTimeZone("GMT")
你应该使用 getTimeZone("GMT+10:00"));
因为 GMT 是 +/-0:00
看看我发布的文档:)
您还可以使用 Joda library 让生活更轻松。
private String showTimeStringExample(String timeStamp) {
//timestamp = "2015-05-12T14:00:00.000+10:00"
// use Joda to parse the time stamp and convert it to java
final Date date = ISODateTimeFormat.dateTimeParser().parseDateTime(timeStamp).toDate();
// use the format you had in your question, setting the locale to the device's default
SimpleDateFormat destFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss",
getResources().getConfiguration().locale
);
// comes out as "2015-05-12 05:00:00 in the UK
return destFormat.format(date);
}
Gradle 依赖关系:
compile 'joda-time:joda-time:2.3'
请试试这个,它在这里工作
try {
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT+10"));
//Local time zone
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//Time in GMT
String date = dateFormatLocal.format(dateFormatGmt.parse("2015-05-12 14:00:00"));
LogUtil.LOGD(Constants.ACTIVITY_TAG, "Date- " + date);
}catch (ParseException e) {
e.printStackTrace();
}