Saturday 24 November 2018

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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...