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


当前回答

在com.google.gson.internal.bind.util包中有一个ISO8601Utils实用程序类,所以如果你在你的应用程序中使用Gson,你可以使用它。

它支持毫秒和时区,所以这是一个很好的开箱即用的选择。

其他回答

Java

Long date=System.currentTimeMillis();
SimpleDateFormat dateFormat =new SimpleDateFormat("dd / MMMM / yyyy - HH:mm", Locale.getDefault());
String dateStr = dateFormat.format(date);

科特林

日期(毫秒)和13位数字(十六进制到日期)

val date=System.currentTimeMillis() //here the date comes in 13 digits
val dtlong = Date(date)
val sdfdate = SimpleDateFormat(pattern, Locale.getDefault()).format(dtlong)

日期格式化程序

"dd / MMMM / yyyy - HH:mm" -> 29 / April / 2022 - 12:03 
"dd / MM / yyyy" -> 29 / 03 / 2022
"dd / MMM / yyyy" -> 29 / Mar / 2022 (shortens the month) 
"EEE, d MMM yyyy HH:mm:ss" -> Wed, 4 Jul 2022 12:08:56

你也可以使用android.os.SystemClock。 例如,SystemClock.elapsedRealtime()将在手机处于睡眠状态时为您提供更准确的时间读数。

final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);

textView.setText("" + mDay + "-" + mMonth + "-" + mYear);

下面是一些获取时间和日期的方法:

public static void getCurrentTimeUsingDate() {
    Date date = new Date();
    String strDateFormat = "hh:mm:ss a";
    DateFormat dateFormat = new SimpleDateFormat(strDateFormat);
    String formattedDate= dateFormat.format(date);
    Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
}

使用日历的时间

public static void getCurrentTimeUsingCalendar() {
    Calendar cal = Calendar.getInstance();
    Date date=cal.getTime();
    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    String formattedDate=dateFormat.format(date);
    Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
}

当地时间日期

public static void getCurrentTime(){
    System.out.println("-----Current time of your time zone-----");
    LocalTime time = LocalTime.now();
    Toast.makeText(this, time, Toast.LENGTH_SHORT).show();
}

分区时间

public static void getCurrentTimeWithTimeZone(){
    Toast.makeText(this, "Current time of a different time zone using LocalTime", Toast.LENGTH_SHORT).show();

    ZoneId zoneId = ZoneId.of("America/Los_Angeles");
    LocalTime localTime=LocalTime.now(zoneId);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
    String formattedTime=localTime.format(formatter);
    Toast.makeText(this,formattedTime , Toast.LENGTH_SHORT).show();
}

获取当前时间和日期的简单方法

import java.util.Calendar

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

实际上,使用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.