Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 484
Tim Avatar asked Sep 02 '11 08:09

Tim


People also ask

How do you get the current time in 24-hour format?

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.

How to get current time in 24-hour format in c#?

DateTime date = DateTime. ParseExact(strDate, "dd/MM/YYYY HH:mm:ss", CultureInfo. InvariantCulture);

How do you display javascript DateTime in 24-hour format?

Use the toLocaleString() method to change time formatting to 24 hours, e.g. date. toLocaleString('en-US', {hour12: false}) . The toLocaleString method returns a string that represents the date and time according to the provided locale and options parameters.


1 Answers

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.

like image 60
Ashkan Aryan Avatar answered Oct 03 '22 13:10

Ashkan Aryan