How to set 24-hours format for date on java?

37

I have been developing Android application where I use this code:

Date d=new Date(new Date().getTime()+28800000);
String s=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").format(d);

I need to get date after 8 hours from current moment, and I want that this date has 24-hours format, but I don't know how I can make it by SimpleDateFormat. I also need that date has DD/MM/YYYY HH:MM:SS format.

This question is tagged with java android datetime

~ Asked on 2012-01-18 09:02:03

The Best Answer is


62

This will give you the date in 24 hour format.

    Date date = new Date();
    date.setHours(date.getHours() + 8);
    System.out.println(date);
    SimpleDateFormat simpDate;
    simpDate = new SimpleDateFormat("kk:mm:ss");
    System.out.println(simpDate.format(date));

~ Answered on 2012-01-18 09:04:08


88

for 12-hours format:

SimpleDateFormat simpleDateFormatArrivals = new SimpleDateFormat("hh:mm", Locale.UK);

for 24-hours format:

SimpleDateFormat simpleDateFormatArrivals = new SimpleDateFormat("HH:mm", Locale.UK);

~ Answered on 2015-05-29 10:15:46


Most Viewed Questions: