【问题标题】:How to add time in Android如何在Android中添加时间
【发布时间】:2019-04-06 04:17:24
【问题描述】:

我从 Google Direction 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());

【问题讨论】:

  • 你能提供你当前的输出以及你想要的输出吗?
  • 我当前的输出会像这样显示当前时间,例如 12.21PM + 0 小时 + 10 分钟。而我想要的输出是 12.31 通过加起来时间
  • 顺便考虑扔掉长期过时且臭名昭著的麻烦DateFormat和朋友,并将ThreeTenABP添加到您的Android项目中,以便使用java.time,现代Java日期和时间API .与它一起工作要好得多。 java.time 的Duration 类可能也会派上用场。

标签: android date time simpledateformat date-format


【解决方案1】:

试试这个

在以下三个参数中传递您的持续时间。

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);
    }

【讨论】:

  • 我有一个问题。我得到的是这样的:“Thu Jan 01 19:25:15 GMT+08:00 1970”,我只需要得到诸如“19:25:15”或“7:25:15 PM/AM”之类的时间,如何获取小时/分钟/秒?我试图将 SimpleDateFormat 更改为 (""h:mm a") 但仍然不能。我参考developer.android.com/reference/java/text/SimpleDateFormat
  • 我仍然无法获得这样的时间:“19.25:15”,先生,我会为您更新我的代码,
  • 没关系,兄弟。我解决了。我的错误,我很抱歉
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-06
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
  • 1970-01-01
  • 2019-07-01
相关资源
最近更新 更多