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?
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(); } }
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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();
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();
}
}