如何在Android应用程序中获取当前时间和日期?


当前回答

你可以(但不再应该-见下文!)使用android.text.format.Time:

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

从上面的参考链接:

Time类是一个更快的替代品 为java.util.Calendar和 java.util.GregorianCalendar类。 Time类的实例 表示指定的时间点 精准度第二。


注1: 我写这个答案已经有好几年了, 它是关于一个旧的,android专用的,现在已被弃用的类。 谷歌现在这样说 “他的类有一些问题,建议使用公历代替”。


注2:尽管Time类有一个toMillis(ignoreDaylightSavings)方法,但这只是为了方便传递给以毫秒为单位的方法。时间值仅精确到一秒;毫秒部分总是000。如果在循环中,你会这样做

Time time = new Time();   time.setToNow();
Log.d("TIME TEST", Long.toString(time.toMillis(false)));
... do something that takes more than one millisecond, but less than one second ...

结果序列将重复相同的值,例如1410543204000,直到下一秒开始,此时1410543205000将开始重复。

其他回答

您可以从日历中单独获取时间和日期。

// You can pass time zone and Local to getInstance() as parameter

Calendar calendar = Calendar.getInstance();

int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
int currentMinute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int date = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);

实际上,使用time . getcurrenttimezone()在设备上设置当前时区更安全,否则您将获得UTC的当前时间。

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

然后,你可以得到你想要的所有日期字段,例如:

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

详情请参阅android.text.format.Time类。

更新

正如许多人指出的那样,谷歌说这个类有一些问题,不应该再使用了:

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.

如果你想以特定的模式获取日期和时间,你可以使用以下命令:

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
String currentDateandTime = sdf.format(new Date());

Or,

日期:

String currentDate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());

时间:

String currentTime = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date());

试试下面的方法。下面给出了所有格式以获得日期和时间格式。

    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);

对于自定义的时间和日期格式:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ",Locale.ENGLISH);
String cDateTime = dateFormat.format(new Date());

输出格式如下:

2015 - 06 - 18 - t10:15:56凌晨