我正在尝试在 TextView 中显示我的应用程序上次更新的时间(例如,“上次更新时间为 12:13)。我正在尝试使用日历实例,我认为我理解正确,但我似乎遇到了麻烦。我知道为了获取一个实例,我使用了 Calendar.getInstance() 方法,然后为了获取小时和分钟,我使用了 Calendar.get(Calendar.HOUR) 和 Calendar.get(Calendar.MINUTE)。 .小时是返回 24 小时时钟,我想要 12 小时时钟。我认为 HOUR_OF_DAY 是 24 小时时钟。
这是我正在使用的代码:
Calendar rightNow = Calendar.getInstance();
mTv.setText("Refreshed! Last updated " +
rightNow.get(Calendar.HOUR) + ":" +
rightNow.get(Calendar.MINUTE) + ".");
mTv 是我正在更新的 TextView。 。
另外,如果我能说“上次更新是在 5 分钟前”而不是“上次更新时间是中午 12:13”,那就更理想了,但我不确定进行此更新的最佳方式 每分钟不耗尽资源或电池......?
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...?
发布评论
评论(4)
我建议将 SimpleDateFormat 与 Date 类结合使用来格式化时间:
简短解释其工作原理:
您创建一个 SimpleDateFormat 对象并将一个 String 传递给它的构造函数,该构造函数告诉它如何格式化传递给它的 format() 函数的每个时间/日期对象。
有很多常量/字母表示特殊的时间对象(例如秒、AM/PM 标记等)。请参阅 类文档 以获得完整列表)。
“K:mm a”表示“11:42 AM”格式 - 以 12 小时格式表示小时的一位或两位数(取决于其值),始终用两位数表示分钟 (mm) 以及 AM 或 PM(a ),视时间而定。
完成此操作后,只需将 Date 对象传递给 format() 函数,您就会得到一个格式化的字符串。请注意,日期仅保存一个时间点,如果您从不带参数的构造函数创建它(“= new Date()”),它将使用当前时间。如果您需要另一个时间,您可以传递一个带有毫秒的长参数,您可以从 Calendar.getTimeInMillis() 中获取它。
在实现“更新 XY 分钟前功能”时 - 是的,您必须每分钟更新一次,并且必须计算更新时间与当前时间之间的差异。我想说,从电池和额外工作的角度来看,这是不值得的。
如果您的应用程序使用标准的短更新周期(例如每小时或类似的东西)并且不是全屏,则用户在屏幕的顶部/底部有一个可见的时钟。如果他确实想检查自更新以来已经过去了多长时间,他可以短暂查看并进行比较(主要是几分钟或几小时/分钟)。恕我直言,这对用户来说并没有什么不便,至少对我来说不会。我只是比较一下,没有考虑这一点。但我倾向于杀死无缘无故地浪费我电池的应用程序。
另请注意,并非每个人都使用 12 小时格式。要根据用户设置/国家/地区获取本地化时间格式,请使用 DateFormat.getTimeInstance()。这会返回一个 DateFormat,但这与 SimpleDateFormat 类似,只是将时间传递给 format()。
I'd recommend using SimpleDateFormat in combination with the Date class for formatting the time:
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.HOUR_OF_DAY 表示 24 小时制
Use Calendar.HOUR_OF_DAY for 24h clock
您可以使用如下所示的线程每分钟更新
editText
You can update
editText
in each minute using a thread like following好吧,Calendar.get(Calendar.HOUR) 应该返回 12 小时时间,但如果您想生成稍微好一点的文本,您可以使用 TimeUnit 类来简化解析。
Well, Calendar.get(Calendar.HOUR) should be returning 12-hour time, but if you wanted to produce your slightly nicer text, you can use the TimeUnit class for parsing simplicity.