in Android Code Tips

Java: Playing with Date, Time, Calendar and SimpleDateFormat

Java: Playing with Date, Time, Calendar and SimpleDateFormat

String time="14:32";
Date t = new Date();
String[] parts = time.split(":");
t.setHours(Integer.valueOf(parts[0]));
t.setMinutes(Integer.valueOf(parts[1]));
Calendar cal = Calendar.getInstance();
cal.setTime(t);
//Go ahead two hours
cal.add(Calendar.HOUR, +2);
//Go back 5 minutes
cal.add(Calendar.MINUTES, -5);
//Display the time
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
return sdf.format(cal.getTimeInMillis());
Share