如何在 Android 中添加时间
How to add time in Android
我有一个从 Google 方向 API 返回的持续时间,我将它转换为这样的字符串:
Common.DURATION = String.valueOf(hours + " hours " + minutes + " mins " + seconds + " seconds ");
我想要的是将持续时间和当前时间相加并显示在TextView中。我有当前时间和小时数,我转换成这样的字符串
String currentDateTimeString = DateFormat.getTimeInstance().format(new Date());
如何使用 Time 方法将它们相加并显示出来?
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());
试试这个
在以下三个参数中传递您的持续时间。
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());
约会
private static SimpleDateFormat DBFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
public String getFormatDate() {
Date date = new Date();
return DBFormat.format(date);
}
我有一个从 Google 方向 API 返回的持续时间,我将它转换为这样的字符串:
Common.DURATION = String.valueOf(hours + " hours " + minutes + " mins " + seconds + " seconds ");
我想要的是将持续时间和当前时间相加并显示在TextView中。我有当前时间和小时数,我转换成这样的字符串
String currentDateTimeString = DateFormat.getTimeInstance().format(new Date());
如何使用 Time 方法将它们相加并显示出来?
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());
试试这个
在以下三个参数中传递您的持续时间。
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());
约会
private static SimpleDateFormat DBFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
public String getFormatDate() {
Date date = new Date();
return DBFormat.format(date);
}