在Android中如何获取当前时间和日期

1288

我该如何在Android应用中获取当前的时间和日期?


8
43个回答!虽然其中许多回答在撰写时是很好的,但2018年可以使用的好答案在这里 - Ole V.V.
@OleV.V. 2023年怎么样? - user16217248
2
@user16217248,我仍然推荐我之前链接的Basil Bourque的答案 - Ole V.V.
43个回答

10

如何以特定格式在Android中显示当前时间和日期

Calendar c = Calendar.getInstance();
System.out.println("Current dateTime => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss a");
String formattedDate = df.format(c.getTime());
System.out.println("Format dateTime => " + formattedDate);

输出

I/System.out: Current dateTime => Wed Feb 26 02:58:17 GMT+05:30 2020
I/System.out: Format dateTime => 26-02-2020 02:58:17 AM

9

自定义时间和日期格式:

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

输出的格式如下所示:

2015年06月18日10点15分56秒-05:00


Date类已于1997年被弃用 - Peter Mortensen

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

尝试这个,我也用过它。

8
您可以使用以下方法获得日期:
Time t = new Time(Time.getCurrentTimezone());
t.setToNow();
String date = t.format("%Y/%m/%d");

这将会以一个很好的格式呈现结果,例如: "2014/02/09"。

无参构造函数 Time t = new Time(); 将使用默认时区。根据我的经验,default == current。 - William T. Mallard

7

Java

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

Kotlin

将毫秒数转换为日期并且转换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

考虑放弃过时且出了名的麻烦的 SimpleDateFormat 及其相关类。使用 desugaring 来使用 java.time,现代 Java 日期和时间 API。它更加易于使用。 - Ole V.V.

7

我遇到了一些API返回的答案问题,因此结合了以下代码:

Time t = new Time(Time.getCurrentTimezone());
t.setToNow();
String date1 = t.format("%Y/%m/%d");

Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm aa", Locale.ENGLISH);
String var = dateFormat.format(date);
String horafecha = var+ " - " + date1;

tvTime.setText(horafecha);

输出:

03:25 PM - 2017/10/03

6
Date todayDate = new Date();
todayDate.getDay();
todayDate.getHours();
todayDate.getMinutes();
todayDate.getMonth();
todayDate.getTime();

似乎这个方法可以工作 - “分配一个日期对象并初始化它,以便它表示分配时间,精确到最近的毫秒。”但是为什么在之前的16个答案中没有提到过,而且已经过去了两年多?看起来太简单了。它在Android上真的可行吗?它是在Android的后续版本中才可用的吗? - Peter Mortensen
1
另一个答案说:“Date类现在已经过时了。”。一条评论说:“你应该使用Calendar或GregorianCalendar。Date类已经过时了。”。 - Peter Mortensen
3
getDategetHours等方法的文档标注了 “已弃用”。例如, "自JDK 1.1版本开始,被Calendar.get(Calendar.HOUR_OF_DAY)取代"。 JDK 1.1版本发布于1997年2月**(!!!)- 在本回答发布时已经弃用了16年。 - Peter Mortensen

5

根据新的API,您应该使用Calender类。Date类现在已被弃用。

Calendar cal = Calendar.getInstance();

String date = "" + cal.get(Calendar.DATE) + "-" + (cal.get(Calendar.MONTH)+1) + "-" + cal.get(Calendar.YEAR);

String time = "" + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE);

是的,Date类在1997年就已经被弃用了。 - Peter Mortensen

5
下面的方法将返回当前日期和时间的字符串,根据您实际的时区使用不同的时区。我使用了GMT。
public static String GetToday(){
    Date presentTime_Date = Calendar.getInstance().getTime();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    return dateFormat.format(presentTime_Date);
}

5

尝试这样简单地获取当前日期和时间:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
String currentDateandTime = sdf.format(new Date());

Enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接