I have a duration returned from Google Direction API and I convert it to a String like this:
Common.DURATION = String.valueOf(hours + " hours " + minutes + " mins " + seconds + " seconds ");
What I want is to add up the duration and the current time and display it in a TextView. I have the current time and hours and I convert to a String like this
String currentDateTimeString = DateFormat.getTimeInstance().format(new Date());
How do I add these together by using the Time method and display it ?
double dist = totalDistance / 1000.0;
int days = totalSeconds / 86400;
int hours = (totalSeconds - days * 86400) / 3600;
int minutes = (totalSeconds - days * 86400 - hours * 3600) / 60;
int seconds = totalSeconds - days * 86400 - hours * 3600 - minutes * 60;
Common.DISTANCE = String.valueOf(dist + " km ");
Common.DURATION = String.valueOf(hours + " hours " + minutes + " mins " + seconds + " seconds ");
SimpleDateFormat DBFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
String currentDateandTime = DBFormat.format(new Date());
Date date = null;
try {
date = DBFormat.parse(currentDateandTime);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, hours);
calendar.add(Calendar.MINUTE, minutes);
calendar.add(Calendar.SECOND, seconds);
Log.v("1st",""+calendar.getTime());
Common.ESTIMATED_TIME = String.valueOf(calendar.getTime());
Try this
pass your duration in below three parameters.
int additionalHour = 0;
int additionalMinute = 0;
int additionalSeconds = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd:HH:mm", Locale.getDefault());
String currentDateandTime = sdf.format(new Date());
Date date = null;
try {
date = sdf.parse(currentDateandTime);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, additionalHour);
calendar.add(Calendar.MINUTE, additionalMinute);
calendar.add(Calendar.SECOND, additionalSeconds);
System.out.println("Desired Time here "+calendar.getTime());
Formate Date
private static SimpleDateFormat DBFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
public String getFormatDate() {
Date date = new Date();
return DBFormat.format(date);
}