如何在Android应用程序中获取当前时间和日期?
当前回答
Time now = new Time();
now.setToNow();
试试这个对我也有用。
其他回答
实际上,使用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());
试着用简单的方法获取当前的日期和时间:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
String currentDateandTime = sdf.format(new Date());
String DataString = DateFormat.getDateInstance(DateFormat.SHORT).format(Calendar.getInstance().getTime());
获取单元本地化格式的短日期格式字符串。
我不明白为什么这么多答案使用硬编码的日期和时间格式,而OS/Java提供了正确的日期和时间本地化。总是使用设备的格式不是比使用程序员的格式更好吗?
它还提供了本地化格式的日期读取:
DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT);
Date date = null;
try {
date = format.parse(DateString);
}
catch(ParseException e) {
}
然后由用户来设置显示日期和时间的格式,而不是你。不论语言等,同一种语言在不同的国家有不同的格式。
科特林
下面是在Kotlin中获取当前日期时间的各种方法。
fun main(args: Array<String>) {
println(System.currentTimeMillis()) // Current milliseconds
val date = Calendar.getInstance().time // Current date object
val date1 = Date(System.currentTimeMillis())
println(date.toString())
println(date1.toString())
val now = Time(System.currentTimeMillis()) // Current time object
println(now.toString())
val sdf = SimpleDateFormat("yyyy:MM:dd h:mm a", Locale.getDefault())
println(sdf.format(Date())) // Format current date
println(DateFormat.getDateTimeInstance().format(System.currentTimeMillis())) // using getDateTimeInstance()
println(LocalDateTime.now().toString()) // Java 8
println(ZonedDateTime.now().toString()) // Java 8
}
推荐文章
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 前一个月的Python日期
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?