【问题标题】:How do I display current timezone and full datetime in two different TextView?如何在两个不同的 TextView 中显示当前时区和完整日期时间?
【发布时间】:2015-03-18 15:37:25
【问题描述】:

我想将时区显示为

Current Timezone is: Indian Standard Time (IST) (GMT +05:30)

日期时间为

Wednesday, 18-March-2015, 09:07:12 pm

第二个TextView(日期时间)应该每秒更新一次。

我将日期时间创建为

Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR);
int min = c.get(Calendar.MINUTE);
int sec = c.get(Calendar.SECOND);

时区为

TimeZone tz = TimeZone.getDefault();
current_Time_Zone = (TimeZone.getTimeZone(tz.getID()).getDisplayName(false, TimeZone.SHORT));
currentTimeZone.setText("Current Timezone: " + current_Time_Zone);

【问题讨论】:

    标签: java datetime timezone


    【解决方案1】:

    文本创建本身不应该是一个问题。对于更新:使用这样的计时器:

    Timer t = new Timer(true);
    t.schedule(new TimerTask() {
            @Override
            public void run() {
                updateTime();
            }
        }, new Date() , 1000l);
    

    这将从计时器创建开始,每秒更新一次时间。

    【讨论】:

    • 如何显示时区? Indian Standard Time (IST) (GMT +05:30)我需要正确的格式字符串。目前我正在使用SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM dd, yyyy - h:mm:s a", Locale.US);
    • 终于得到了准确的字符串,@paul,这里是 android 文档参考 - [link]developer.android.com/reference/java/text/SimpleDateFormat.html
    • zzzz (zzz) \'( GMT + 05:30)\' 应该是正确的格式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多