3

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.

  • Get current time in milliseconds, `System.currentTimeMillis()` http://developer.android.com/intl/ko/reference/java/lang/System.html#currentTimeMillis() – Murtaza Khursheed Hussain Oct 24 '15 at 08:39
  • Possible duplicate of [Get current time and date on Android](http://stackoverflow.com/questions/5369682/get-current-time-and-date-on-android) – Brad Werth Nov 03 '15 at 05:47

3 Answers3

5

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

Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
1

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

Date date = new Date(); 
bhadresh
  • 445
  • 5
  • 15
0

Use this.

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

Which output are this,

Oct 24, 2015 5:41:23 PM