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

2.07K 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..!!

Changed status to publish
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;
	}

 

Changed status to publish
Add a Comment
Write your answer.