简体   繁体   中英

Getting the time in 24 hour format

I have to print the date in 24 hour format rite now as shown below the date is printed in 12 hr format i want it to be get printed in 24 hour format as the file will be containg UK timestamp

String currDate = new SimpleDateFormat("MM-dd-yyyy-HH-mm").format(new Date());
System.out.println("ABC_0101-"+currDate);
ABC_0101-07-09-2013-11-16

Please advise as shown above the file name is ABC_0101 followd by a seprator "-" and then the time stamp 07-09-2013-11-16 , please advise how the time particulary will be printed in 24 hour format

you could use HH or KK to format hour in 24.

H Hour in day (0-23)
k Hour in day (1-24)

format you can use MM-dd-yyyy-kk-mm or MM-dd-yyyy-HH-mm .

Currently what have you done is working. and that will give you time in 24 hour format. change your system time to some where in afternoon and check it.

    String currDate = new SimpleDateFormat("MM-dd-yyyy-HH-mm").format(new Date());
    System.out.println(currDate);

I'm confused. It looks like your code will print in 24 hour format to me? It says here that HH represents the 24 hour format

http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

HH is exactly what you need. It's 24 hour format. See SimpleDateFormat API. Change your computer time to 16:00 and try your code again to test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM