Android:添加GMT时区后显示时间 [英] Android:Display time after adding GMT time zone

查看:61
本文介绍了Android:添加GMT时区后显示时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个想要显示通知时间的应用程序.我可以显示通知时间,但不能在其中添加时区.我目前的位置是巴基斯坦,我想加格林尼治标准时间+5:00我的代码已附上

I am working on a App in which i want to display notification time. I can display notification time but not able to add time zone in it. My current location is Pakistan and i want to add GMT+5:00 My code is attached

String currentDateTimeString = DateFormat.getTimeInstance().format(notif.At);
textViewTime.setText(currentDateTimeString);

在此代码中的

,notif.At是dateTime变量.我还附上了我的应用程序的屏幕截图,我想问你,如何在notif.At中添加timeZone值.谢谢!

in this code, notif.At is dateTime variable. I also attached screenshot of my app, i want to ask you , how to add timeZone value in notif.At. Thanks!

推荐答案

更新

您可以用时区标记时间,以解决国际化问题,对吗?

Update

You mark time with timezone in order to solve internationalization problem, I understand, right?

如果是这样,我认为将您的日期转换为UTC日期可能会更好.当您更改为其他时区时,只需将此UTC日期转换为本地时间即可.

If so, I think it could be better to convert your date to UTC date. When you change to another timezone, just convert this UTC Date to local.

public static Date localToUtc(Date localDate) {
    return new Date(localDate.getTime()-TimeZone.getDefault().getOffset(localDate.getTime()));
}
public static Date utcToLocal(Date utcDate) {
    return new Date(utcDate.getTime()+TimeZone.getDefault().getOffset(utcDate.getTime()));
}


旧答案

如果您的 notif.At Date 对象,实际上是一个相同的问题:

TimeZone tz = TimeZone.getDefault();
Date date = new Date(); 
final String format = "yyyy-MM-dd HH:mm:ss";

SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
String result = sdf.format(date);
Log.d("Date ", "date: " +  result + " " + tz.getDisplayName(false, TimeZone.SHORT));

打印:

日期:2015-03-31 18:45:28 GMT + 08:00

date: 2015-03-31 18:45:28 GMT+08:00

这篇关于Android:添加GMT时区后显示时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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