Start with a SimpleDateFormat
, this will allow you parse and format time values, for example...
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
try {
// Get the start time..
Date start = sdf.parse("09:00 AM");
System.out.println(sdf.format(start));
} catch (ParseException ex) {
ex.printStackTrace();
}
With this, you can then use Calendar
with which you can manipulate the individual fields of a date value...
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.MINUTE, 45);
Date end = cal.getTime();
And putting it all together...
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
try {
Date start = sdf.parse("09:00 AM");
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.MINUTE, 45);
Date end = cal.getTime();
System.out.println(sdf.format(start) + " to " + sdf.format(end));
} catch (ParseException ex) {
ex.printStackTrace();
}
Outputs 09:00 AM to 09:45 AM
Updated
Or you could use JodaTime
...
DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendHourOfDay(2).appendLiteral(":").appendMinuteOfHour(2).appendLiteral(" ").appendHalfdayOfDayText().toFormatter();
LocalTime start = LocalTime.parse("09:00 am", dtf);
LocalTime end = start.plusMinutes(45);
System.out.println(start.toString("hh:mm a") + " to " + end.toString("hh:mm a"));
Or, if you're using Java 8's, the new Date/Time API...
DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendPattern("hh:mm a").toFormatter();
LocalTime start = LocalTime.of(9, 0);
LocalTime end = start.plusMinutes(45);
System.out.println(dtf.format(start) + " to " + dtf.format(end));