FIX THE TIME FROM DIGITALCLOCK?

Pawan Shukla

Total Post:38

Points:274
Posted by  Pawan Shukla
 845  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. Kamlakar Singh

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

  1. Takeshi Okada

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

Answer

NEWSLETTER

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