Get current time and date on Android

2018-12-31 04:00发布

How can I get the current time and date in an Android app?

30条回答
临风纵饮
2楼-- · 2018-12-31 04:41

Easy, you can dissect the time to get separate values for current time, as follows:

Calendar cal = Calendar.getInstance(); 

  int millisecond = cal.get(Calendar.MILLISECOND);
  int second = cal.get(Calendar.SECOND);
  int minute = cal.get(Calendar.MINUTE);
        //12 hour format
  int hour = cal.get(Calendar.HOUR);
        //24 hour format
  int hourofday = cal.get(Calendar.HOUR_OF_DAY);

Same goes for the date, as follows:

Calendar cal = Calendar.getInstance(); 

  int dayofyear = cal.get(Calendar.DAY_OF_YEAR);
  int year = cal.get(Calendar.YEAR);
  int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
  int dayofmonth = cal.get(Calendar.DAY_OF_MONTH);
查看更多
倾城一夜雪
3楼-- · 2018-12-31 04:44

Try with this way All formats are given below to get date and time format.

    Calendar c = Calendar.getInstance();
    SimpleDateFormat dateformat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss aa");
    String datetime = dateformat.format(c.getTime());
    System.out.println(datetime);

first

second third

查看更多
只靠听说
4楼-- · 2018-12-31 04:45

You could use:

import java.util.Calendar

Date currentTime = Calendar.getInstance().getTime();

There are plenty of constants in Calendar for everything you need.

Edit:
Check Calendar class documentation

查看更多
柔情千种
5楼-- · 2018-12-31 04:45

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

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(new Date());
查看更多
若你有天会懂
6楼-- · 2018-12-31 04:45

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

Time today = new Time(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 + "");                // Year 
textViewTime.setText(today.format("%k:%M:%S"));  // Current time

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

UPDATE

As many people are pointing out, Google says this class has a number of issues and is not supposed to be used anymore:

This class has a number of issues and it is recommended that GregorianCalendar is used instead.

Known issues:

For historical reasons when performing time calculations all arithmetic currently takes place using 32-bit integers. This limits the reliable time range representable from 1902 until 2037.See the wikipedia article on the Year 2038 problem for details. Do not rely on this behavior; it may change in the future. Calling switchTimezone(String) on a date that cannot exist, such as a wall time that was skipped due to a DST transition, will result in a date in 1969 (i.e. -1, or 1 second before 1st Jan 1970 UTC). Much of the formatting / parsing assumes ASCII text and is therefore not suitable for use with non-ASCII scripts.

查看更多
倾城一夜雪
7楼-- · 2018-12-31 04:46

If you need current date,

Calendar cc = Calendar.getInstance();
int year=cc.get(Calendar.YEAR);
int month=cc.get(Calendar.MONTH);
int mDay = cc.get(Calendar.DAY_OF_MONTH);
System.out.println("Date", year+":"+month+":"+mDay);

If you need current time,

 int mHour = cc.get(Calendar.HOUR_OF_DAY);
 int mMinute = cc.get(Calendar.MINUTE);
 System.out.println("time_format"+String.format("%02d:%02d", mHour , mMinute ));
查看更多
登录 后发表回答