user266003
user266003

Reputation:

Getting the current date/time on an android device

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.

Upvotes: 3

Views: 8056

Answers (3)

user5400869
user5400869

Reputation:

Use this.

String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());

Which output are this,

Oct 24, 2015 5:41:23 PM

Upvotes: 0

Murtaza Khursheed Hussain
Murtaza Khursheed Hussain

Reputation: 15336

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

Upvotes: 5

bhadresh
bhadresh

Reputation: 443

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

Date date = new Date(); 

Upvotes: 1

Related Questions