Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the current date/time on an android device

Tags:

android

How can I get the current date/time of an android device? Whether it's correct or not, doesn't matter, it just need the date/time which which is happens to be set on the android device.

I've tried this:

new Time().setToNow()

but it hasn't given me the current date/time, instead it's set it.


2 Answers

Getting the current date time can be done in several ways.

Option 1

System.currentTimeMillis() will return current datetime in Milliseconds.

if your parse this into Date object

Date newDate  = new Date(System.currentTimeMillis());

newDate.toString();  //This will print the Human Readable String

Option 2

SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ",Locale.getDefault());

String myDate = format.format(new Date());

Format Example

                 yyyy-MM-dd 1969-12-31
                 yyyy-MM-dd 1970-01-01
           yyyy-MM-dd HH:mm 1969-12-31 16:00
           yyyy-MM-dd HH:mm 1970-01-01 00:00
          yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
          yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
   yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
   yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
 yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
 yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000

Read more about SimpleDateFormat

like image 191
Murtaza Khursheed Hussain Avatar answered Jun 04 '26 10:06

Murtaza Khursheed Hussain


You can Use this function to get Current Date and Time:

Date date = new Date(); 
like image 21
bhadresh Avatar answered Jun 04 '26 10:06

bhadresh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!