Fix the time from DigitalClock?

Total Post:36

Points:260
 1076  View(s)
Ratings:
Rate this:
For example, I have DigitalClock and TextView in MainActivity. How to fix time in TextView (24-hour format) in a moment of time when the Application is launched?
  1. Post:194

    Points:1396
    Re: Fix the time from DigitalClock?

    public class ApplicationController extends Application{
            /**
             * A singleton instance of the application class for easy access in other places
             */
            private static ApplicationController sInstance;
            public static synchronized ApplicationController getInstance() {
                return sInstance;
            }
           private String time ;
           public String getTime(){
              return time ;
           }
            @Override
            public void onCreate() {
                super.onCreate();
                  sInstance = this;
                  time = System.currentTimeMillis();
            }
    }
    Then you can just setText.

    TextView tx = findViewById(R.id.your_id);
    tx.setText(ApplicationController.getInstance.getTime());

      Modified On Apr-06-2018 12:13:27 AM
  1. Post:89

    Points:629
    Re: Fix the time from DigitalClock?

    You can try this :


    String time= "Time : "  + DateFormat.getTimeFormat(getContext()).format(new Date(calendar.getTimeInMillis()));

    This will return to the user smth like : " Time : 13:11"

    Assuming you are using an instance of calendar somewhere in your code.

    If not simply get an instance of calendar like this (it will return current device time ) :

    Calendar calendar = Calendar.getInstance();

      Modified On Apr-06-2018 12:12:50 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!