【问题标题】:Truetime Android API, how to get the values GMT, PST, device time automatic whenever open the activity?Truetime Android API,如何在打开活动时自动获取格林威治标准时间、太平洋标准时间、设备时间的值?
【发布时间】:2020-07-06 07:48:12
【问题描述】:

我正在使用 API truetime-android。 https://github.com/instacart/truetime-android

我的问题是,如果我想获取 GMT、PST、设备时间的值。我必须按下按钮立即获取时间。我不喜欢这样,我希望每当我打开该活动时,三个 GMT、PST、设备时间已经有了值,所以我不需要按下按钮 Get Time Now 获取值 GMT、PST、设备时间。如何实施?

这是我的代码

public class SampleActivity extends AppCompatActivity {
private Button refreshBtn;
private TextView timeGMT;
private TextView timePST;
private TextView timeDeviceTime;

   protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sample_activity);
    setupUI();
    init(this);
    click() ;
}

public static void init(final Context context) {
(new Thread(new Runnable() {
    public void run() {
        try {
           TrueTime.build().withNtpHost("time.google.com").withLoggingEnabled(false).
           withSharedPreferencesCache(context).
           withConnectionTimeout(31428).initialize();
        } catch (IOException var2) {
            var2.printStackTrace();
        }
    }
})).start();

private void click() {
    Date trueTime;
    refreshBtn!!.setOnClickListener(View.OnClickListener {
        if (!TrueTime.isInitialized()) {
            //do nothing
        } else {
       trueTime = TrueTime.now();
       Date deviceTime = new Date();
       timeGMT.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT")));
       timePST.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("GMT07:00")));
       timeDeviceTime.setText(formatDate(deviceTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT-07:00"))));
        }
    }
}
private void setupUI() {
    refreshBtn = (Button) findViewById(R.id.tt_btn_refresh);
    timeGMT = (TextView) findViewById(R.id.tt_time_gmt);
    timePST = (TextView) findViewById(R.id.tt_time_pst);
    timeDeviceTime = (TextView) findViewById(R.id.tt_time_device);
}

private String formatDate(Date :date ,String : pattern,TimeZone : timeZone){
    String format = SimpleDateFormat(pattern, Locale.ENGLISH);
    format.timeZone = timeZone;
    return format.format(date);
}

【问题讨论】:

    标签: java android datetime time current-time


    【解决方案1】:

    试试这个。您可以在 onCreate 中找到我的更改

    public class SampleActivity extends AppCompatActivity {
    private Button refreshBtn;
    private TextView timeGMT;
    private TextView timePST;
    private TextView timeDeviceTime;
    
       protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_activity);
        setupUI();
        init(this);
        new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
            if (TrueTime.isInitialized()) {
               trueTime = TrueTime.now();
               Date deviceTime = new Date();
               timeGMT.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT")));
               timePST.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("GMT07:00")));
               timeDeviceTime.setText(formatDate(deviceTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT-07:00"))));
            }
             }
            }, 1000);
    }
    
    public static void init(final Context context) {
    (new Thread(new Runnable() {
        public void run() {
            try {
               TrueTime.build().withNtpHost("time.google.com").withLoggingEnabled(false).
               withSharedPreferencesCache(context).
               withConnectionTimeout(31428).initialize();
            } catch (IOException var2) {
                var2.printStackTrace();
            }
        }
    })).start();
    
    private void click() {
        Date trueTime;
        refreshBtn!!.setOnClickListener(View.OnClickListener {
              if (!TrueTime.isInitialized()) {
                //do nothing
            } else {
           trueTime = TrueTime.now();
           Date deviceTime = new Date();
           timeGMT.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT")));
           timePST.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("GMT07:00")));
           timeDeviceTime.setText(formatDate(deviceTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT-07:00"))));
            }
        }
    }
    private void setupUI() {
        refreshBtn = (Button) findViewById(R.id.tt_btn_refresh);
        timeGMT = (TextView) findViewById(R.id.tt_time_gmt);
        timePST = (TextView) findViewById(R.id.tt_time_pst);
        timeDeviceTime = (TextView) findViewById(R.id.tt_time_device);
    }
    
    private String formatDate(Date :date ,String : pattern,TimeZone : timeZone){
        String format = SimpleDateFormat(pattern, Locale.ENGLISH);
        format.timeZone = timeZone;
        return format.format(date);
    }
    

    【讨论】:

    • 好吧,如果我第一次打开 SampleAcitivy,它什么也没显示。但是,如果我再次打开 SampleActivity,它就会显示这些值。如何解决这个问题?
    • 谢谢你,我真的很感激。你能解释一下为什么添加处理程序会使代码工作吗?
    • 有时代码会在我们的 UI 初始化之前被调用。这是延迟我们的代码执行的一种方法。或者您可以在 onResume 中调用无处理程序。这可能行得通。
    • 如果您觉得我的回答有帮助,请将我的回答标记为已接受。
    猜你喜欢
    • 2013-03-10
    • 2011-10-30
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 2019-11-25
    • 2010-12-02
    • 2011-02-20
    相关资源
    最近更新 更多