今天白天上课时,老师是这样获取当前时间的,
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秒