You can use SimpleDateFormat like this for 24 hours:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss);
or change HH
to hh
to get 12 hours format:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss);
then just use:
dateFormat.format(yourDate);
which you can get using calendar:
SimpleDateFormat dateFormat;
if (DateFormat.is24HourFormat()) {
dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss);
} else {
dateFormat = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss);
}
Calendar c = new Calendar.getInstance();
c.set(Calendar.HOUR, yourIntHour); // Or HOUR_OF_DAY
c.set(Calendar.MINUTE, yourIntMinute);
Date d = c.getTime();
String dateResult = dateFormat.format(d);
You can use HOUR_OF_DAY
instead of HOUR
:
(soruce: javadoc)
public static final int HOUR
Field number for get and set indicating the hour of the morning or
afternoon. HOUR is used for the 12-hour clock (0 - 11). Noon and
midnight are represented by 0, not by 12. E.g., at 10:04:15.250 PM the
HOUR is 10.
public static final int HOUR_OF_DAY
Field number for get and set indicating the hour of the day.
HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM
the HOUR_OF_DAY is 22.