开发者

Getting the current time and date on a 24 hour timescale

开发者 https://www.devze.com 2023-04-01 06:43 出处:网络
I am having problems getting the current time on a 24 hour timescale. As far as I know, \"HH\" should represent the current hour on a 24 hour timescale, however, for some reason, \"HH\" is not interpr

I am having problems getting the current time on a 24 hour timescale. As far as I know, "HH" should represent the current hour on a 24 hour timescale, however, for some reason, "HH" is not interpreted at all. This is why 开发者_开发技巧the following line of code outputs something like "HH:50:06 Uhr, 02. Sep.":

DateFormat.format("HH:mm:ss 'Uhr', dd. MMM", new Date());

Any ideas what I am doing wrong? Using "hh" works, however, this will output the time on a 12 hour scale, which is not what I'd like to do.

Help's appreciated!


You can use SimpleDateFormat to format it the way you like, this works:

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String str = sdf.format(new Date());

Also Android version of docs.


HH is the format specifier for hour of the day in the 24-hour format (0-23; with an offset of 0) only when you utilize the SimpleDateFormat class for formatting dates.

You are using the format method of the android.text.format.DateFormat class class, that does not employ this notation; instead it uses the symbol k/kk for displaying hours in the 24-hour format. Therefore, your date format string must be specified in the following manner:

DateFormat.format("kk:mm:ss 'Uhr', dd. MMM", new Date());


Try this:

      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
      String s = sdf.format(new Date());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号