android 中Time类的过时与GregorianCalendar类

本文介绍了在Android中遇到Time类及其方法过时的问题,并解释了这一问题与Year_2038_problem的相关性。文章提供了使用GregorianCalendar类替代Time类的示例代码,以及如何配合SimpleDateFormat类实现定制化的时间格式输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天白天上课时,老师是这样获取当前时间的,

		 Time time;
		 time = new Time();
		 time.setToNow();
		 Log.i("MYTAG", time.format2445());
下课后,我在自己的电脑上一用,发现出现警告:

The type Time is deprecated,

The method setToNow() from the type Time is deprecated,

The method format2445() from the type Time is deprecated;

于是我查了下文档,原来android中Time类型的过时与Year_2038_problem和有关,简单来讲,就是大部分32位类Unix系统能表示的最新时间是03:14:07 UTC on Tuesday, 19 January 2038,这样便出现了"Unix上的千禧年Bug"。解决方案之一就是使用java.util.GregorianCalendar类来代替android.text.format.Time类。

GregorianCalendar类的使用也很简单,下面的例子中是与SimpleDateFormat类配合使用,用于控制时间显示格式,如下:

		GregorianCalendar calendar;
		calendar = new GregorianCalendar();
		
		SimpleDateFormat dateFormat;
		dateFormat = new SimpleDateFormat("y年M月d日 H时m分s秒", Locale.CHINA);
		System.out.println(dateFormat.format(calendar.getTime()));
打印出的时间格式为:2016年8月18日 22时35分21秒




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值