Get current time and date on Android

Source: Internet
Author: User
Tags dateformat time and date

Could use:

Calendar c =Calendar.getInstance();int seconds = c.get(Calendar.SECOND);

There is plenty of constants in the Calendar for everything you need. Edit:calendar class Documentation

#########

can use Android.text.format.Time:

Time now =newTime();now.setToNow();

From the reference linked above:

The time class is a faster replacement for the Java.util.Calendar and Java.util.GregorianCalendar classes. An instance of the time class represents a moment in time, specified with second precision.

If you want to get the date and time of a specific pattern you can use the following:

SimpleDateFormat sdf =newSimpleDateFormat("yyyyMMdd_HHmmss");String currentDateandTime = sdf.format(newDate());


Actually, it ' s safer to set the current timezone set on Time.getCurrentTimezone() the device with, or else you'll get the current time in UT C.

Time today =newTime(Time.getCurrentTimezone());today.setToNow();

Then, you can get all the date fields you want, like, for example:

Textviewday.SetText(Today.MonthDay+"");//day of the Month (1-31)Textviewmonth.SetText(Today.Month+"");//Month (0-11)Textviewyear.SetText(Today. Year+"");// yearTextviewtime.SetText(Today.format("%k:%m:%s"));// Current time

See Android.text.format.Time class for all the details.

To GE, the current time, can use which are standard in System.currentTimeMillis() Java. Then you can use it to create a date

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

And as mentioned by others to create a time

Time currentTime =newTime();currentTime.setToNow();


For those who might rather prefer a customized format, you can use:

DateFormat df =newSimpleDateFormat("EEE, d MMM yyyy, HH:mm");String date = df.format(Calendar.getInstance().getTime());

Whereas you can have dateformat patterns such as:

"yyyy. Mm.dd G ' at ' HH:mm:ss z '----2001.07.GenevaAD at A: ,: AboutPDT"hh ' o ' clock ' A, zzzz"----------- Ao' clock PM, Pacific Daylight Time ' EEE, D MMM yyyy HH:mm:ss Z '-------Wed, 4 Jul 2001 12:08:56-0700 ' Yyyy-mm-dd 'T' HH:mm:ss. Sssz "-------2001-07-04t12:08:56.235-0700" yymmddhhmmssz "--------------------010704120856-0700" k:mm A, z "--------- --------------0:08 pm, PDT "h:mm a"--------------------------12:08 PM "EEE, MMM D, ' yy"----------------Wed, Jul 4, ' 
      01 

Http://stackoverflow.com/questions/5369682/get-current-time-and-date-on-android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.