Friday, 2 June 2017

How to get current time and date in Android



How to get the current time and date in an Android App?


Answer



You could use:



import java.util.Calendar


Date currentTime = Calendar.getInstance().getTime();



output - Thu Dec 19 18:53:43 GMT+05:30




SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());

//You can change "yyyyMMdd_HHmmss as per your requirement


String currentDateandTime = sdf.format(new Date());



Output - 20191219_185343




There are plenty of constants in Calendar for everything you need.




Edit:
Check Calendar class documentation


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...