Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

java - Getting the current time and date on a 24 hour timescale

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...