Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Hours from a date using java

What is the date format to get only hours in 12-hours format from this time

 Thu Oct 20 13:12:00 GMT+02:00 2011

edit:

using this code

Date eventDate = tempAppointments.get(i).mStartDate
System.out.println(eventDate.toString());

// date pattern
DateFormat df = new SimpleDateFormat("hh:'00' a");//output : Wed Nov 09 11:00:00 GMT+02:00 2011


// get the start date with new format (pattern) 
String hours = df.format(tempAppointments.get(i).mStartDate.getDay());
System.out.print(hours);//output: 02:00 AM

return hours as

02:00 AM

but for the given time. it must be 02:00 PM . why ?

like image 932
Bader Avatar asked Nov 19 '11 23:11

Bader


1 Answers

I'm not sure why you are passing date.getDay() (which is deprecated, by the way) into the formatter if you want the hour part.

Try this:-

Date date = new Date();
System.out.println("Date: " + date);

DateFormat df = new SimpleDateFormat("hh:'00' a");
String hour = df.format(date);
System.out.println("Hour: " + hour);

The output:

Date: Sat Nov 19 17:57:05 CST 2011
Hour: 05:00 PM
like image 149
limc Avatar answered Oct 03 '22 22:10

limc