How to get hours and minutes from the date in java?

3.69K viewsJavaSpring
0

I want to get hours and time from the date like as below

If I pass date into a method, the method should return hours and minutes

Please advise me on this..!!

Manohar Changed status to publish June 23, 2019
Add a Comment
0

Please use below snippet and try, it will work

 

public String getHoursAndMinutes(Date date) {
		calendar.setTime(date); // assigns calendar to given date
		String min = "";
		if (calendar.get(Calendar.MINUTE) < 10) {
			min = "0" + calendar.get(Calendar.MINUTE);
		} else {
			min = "" + calendar.get(Calendar.MINUTE);
		}
		String hrsAndMinutes = calendar.get(Calendar.HOUR_OF_DAY) + "" + min;
		return hrsAndMinutes;
	}

 

Manohar Changed status to publish June 23, 2019
Add a Comment
Write your answer.