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

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

问题描述

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

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?

下面是code我使用的是:

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是我的TextView是我更新。感谢您的帮助。

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

此外,这将是理想的,如果我可以说,最后更新5分钟前。而不是在下午12时13分最后更新。但我不知道最好的方法有此更新每分钟不消耗资源或电池...?

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...?

推荐答案

我推荐使用SimpleDateFormat的结合Date类用于格式化时间:

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);

简短说明它是如何工作的:
您创建一个的SimpleDateFormat对象,并传递一个字符串给它的construtor告诉它如何格式化获取传递给它的格式()函数每次时间/日期对象。
有很多常量/字母从而重新present一个特殊的时刻物体(例如秒,一个AM / PM标记..看的类文档的完整列表)。

K:毫米是指上午11:42格式 - 一位或两位数字的小时(根据其价值)的12小时格式,总是两位数分钟(MM)和AM或PM(a)中,根据时间

"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.

你这样做之后,只是传递一个Date对象格式()函数,你会得到一个格式化字符串。请注意,只是日期持有一个时间单点,如果从不带参数的构造函数创建它(=新的Date()),它使用当前时间。如果需要其他时间,你可以传递一个长期争论的米利斯,你可能会得到来自Calendar.getTimeInMillis()。

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().

由于实施更新XY分钟前功能的 - 是的,你不得不更新这每一分钟,你必须计算的更新和当前时间之间的差异。我会说这是不值得的,从电池和额外的工作角度。
如果应用程序使用标准短的更新周期(如沿着这些线路每隔一小时或财产以后),而不是全屏,用户在他的屏幕顶部/底部可见的时钟。如果他真的要查多久是自更新,他可以采取短期的外观和比较(大多只是数分钟或数小时/分钟)。恕我直言那没有inconvinience为用户,至少它不会对我来说。我只是比较没有想到这一点。但我倾向于杀死它浪费我的电池没有有用的应用程序的原因。

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.

另外请注意,不是每个人都使用12小时格式。要获得本地化的时间格式取决于用户的设置/国家使用DateFormat.getTimeInstance()。这将返回一个日期格式,但是这就像SimpleDateFormat的,只是通过一次格式化()。

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天全站免登陆