Calendar.get(Calendar.HOUR)的问题 [英] Problem with Calendar.get(Calendar.HOUR)

查看:825
本文介绍了Calendar.get(Calendar.HOUR)的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的应用程序上次更新时显示在TextView(例如,最后更新在12:13)。我试图使用一个日历实例,我想我正确理解它,但我似乎有麻烦。我知道得到一个实例,我使用Calendar.getInstance()方法,然后获取我使用Calendar.get(Calendar.HOUR)和Calendar.get(Calendar.MINUTE)的小时和分钟。我的分钟字段返回正确但是Calendar.HOUR在24小时制的时钟返回小时,我想要一个12小时的时钟,我认为HOUR_OF_DAY是24小时制。我在哪里出错?



这里是我使用的代码:

 日历rightNow = Calendar.getInstance(); 
mTv.setText (Refreshed!Last updated+
rightNow.get(Calendar.HOUR)+:+
rightNow.get(Calendar.MINUTE)+。);

mTv是我正在更新的TextView。感谢任何帮助。



此外,如果我可以说最后更新5分钟前。而不是最后更新在12:13下午是理想的。但是我不确定每分钟更新一次的最佳方式,而不消耗资源或电池...?

解决方案

我建议结合使用SimpleDateFormat与Date类格式化时间:

  Date now = new Date(); 
SimpleDateFormat sdf = new SimpleDateFormat(K:mm a);
String formattedTime = sdf.format(now);

简单解释它是如何工作的:
创建一个SimpleDateFormat对象并传递一个String到它的construtor,它告诉它如何格式化每个传递给它的format()函数的时间/日期对象。
有很多常量/字母代表一个特殊的时间对象(例如秒,AM / PM标记,...见类文档)。



K:mm a表示11:42 AM格式 - 以12小时格式表示小时的一位或两位数字(取决于其值)



在这之后,只需将Date对象传递给格式()函数,你会得到一个格式化的字符串。请注意,日期只保存一个时间点,如果从没有参数的构造函数(= new Date())创建它,它使用当前时间。如果你需要另一个时间,你可以传递一个长的争论与millis,你可以从Calendar.getTimeInMillis()。



实现更新的XY分钟以前的功能 - 是的,你必须每分钟更新一次,你必须计算更新和当前时间之间的差异。我想说,从电池和额外的工作角度来看,这是不值得的。
如果您的应用程序使用标准的短更新周期(例如,每小时或沿着这些线的一些东西),而不是全屏,用户在他的屏幕的顶部/底部有一个可见的时钟。如果他真的想检查更新以来的时间,他可以看看和比较(大多只是几分钟或小时/分钟)。和恕我直言对用户没有不舒服,至少它不会为我。我只是比较没有想到这一点。但我倾向于杀死浪费我的电池的应用程序没有什么有用的理由。



还要注意,并不是每个人都使用12小时格式。要获取本地化的时间格式取决于用户设置/国家使用DateFormat.getTimeInstance()。这会返回一个DateFormat,但是它的工作方式类似于SimpleDateFormat,只是将一个时间传递给format()。


I am trying to display in a TextView when my application last updated (e.g., "Last updated at 12:13). I am trying to use a Calendar instance and I thought I understood it correctly but I seem to be having trouble. I know to get an instance I use the method Calendar.getInstance(). And then to get the hour and minute I was using Calendar.get(Calendar.HOUR) and Calendar.get(Calendar.MINUTE). My minute field returns correctly but Calendar.HOUR is returning the hour on a 24 hour clock and I want a 12 hour clock. I thought HOUR_OF_DAY was 24 hour clock. Where am I going wrong?

Here is the code I'm using:

Calendar rightNow = Calendar.getInstance();
mTv.setText("Refreshed! Last updated " + 
           rightNow.get(Calendar.HOUR) + ":" + 
           rightNow.get(Calendar.MINUTE) + ".");

mTv is my TextView that I'm updating. Thanks for any help.

Also, it would be ideal if I could say "Last updated 5 minutes ago." instead of "Last updated at 12:13pm". But I'm not sure the best way to have this update each minute without draining resources or the battery...?

解决方案

I'd recommend using SimpleDateFormat in combination with the Date class for formatting the time:

Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("K:mm a");
String formattedTime = sdf.format(now);

Short explanation how it works: You create a SimpleDateFormat object and pass a String to it's construtor which tells it how to format every time/date object that gets passed to the format() function of it. There are plenty of constants/letters which represent a special time object (e.g. seconds, an AM/PM marker, .. see the class documentation for the full list).

"K:mm a" means a "11:42 AM" format - one or two digits for the hour (depending on its value) in a 12 hour format, always two digits for minutes (mm) and either AM or PM (a), depending on the time.

After you did that, just pass a Date object to the format() function, and you'll get a formatted string. Note that a Date just holds one single point in time, if you create it from the constructor with no arguments ("= new Date()") it uses the current time. If you need another time, you can pass a long argument with the millis, you may get that from Calendar.getTimeInMillis().

As of implementing the "updated XY minutes ago function" - yes you'd have to update this every minute and you have to calculate the difference between the update and the current time. I'd say it's not worth it from a battery and extra work perspective. If your app uses standard short update cycles (e.g. every hour or somthing along those lines) and is not fullscreen, the user has a visible clock on top/bottom of his screen. If he really wants to check how long it was since the update, he can take a short look and compare (mostly just minutes or hours/minutes). And IMHO thats no inconvinience for a user, at least it would not for me. I'd just compare without thinking about that. But I tend to kill apps which waste my battery for no useful reason.

Also note that not everybody uses a 12-hour format. To get a localized time format depending on users settings/country use DateFormat.getTimeInstance(). This returns a DateFormat, but this works like the SimpleDateFormat, just pass a time to format().

这篇关于Calendar.get(Calendar.HOUR)的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆