javaandroiddatetimetimecurrent-time

Truetime Android API, how to get the values GMT, PST, device time automatic whenever open the activity?


I am using the API truetime-android. https://github.com/instacart/truetime-android

My problem is, if i want to get the values GMT, PST, device time. I have to press the button Get Time Now. I don't like that, I want whenever I open that activity, the three GMT, PST, device time are already have the values, so i don't need to press button Get Time Now the get the values GMT, PST, device time. How to implement that? enter image description here

Here is my code

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);
}

Solution

  • Try this out. You can find my changes as part of 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);
    }