【问题标题】:How to display time adding 30mins from current time如何显示从当前时间增加 30 分钟的时间
【发布时间】:2019-07-13 07:32:18
【问题描述】:

我只希望它从当前时间显示到一天结束时增加 30 分钟 前今天时间是上午 09:46 对于该特定日期,它应该显示为10:00AM,10:30AM,11:00AM....11:30PM。 但在我的代码中,它从 00:00...23:30 显示一整天。 这是我的代码:

SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        int startDate = cal.get(Calendar.DATE);
        while (cal.get(Calendar.DATE) == startDate) {
            Log.d("time","currenttime"+cal.getTime());
            cal.add(Calendar.MINUTE, 30);
        }

【问题讨论】:

  • SimpleDateFormatCalendar 设计不佳且早已过时。考虑将ThreeTenABP 添加到您的Android 项目中,而不是使用它们,以便使用java.time, the modern Java date and time API
  • 仅供参考,java.util.Datejava.util.Calendarjava.text.SimpleDateFormat 等麻烦的旧日期时间类现在已被 java.time 类所取代。大多数 java.time 功能在 ThreeTen-Backport 项目中被反向移植到 Java 6 和 Java 7。进一步适用于ThreeTenABP 中的早期 Android (How to use ThreeTenABP…
  • 当您处理一个敏感的现实生活项目时,您必须考虑 100 次才能导入外部库来执行简单的功能,更不用说由通用 Github 帐户提供的库了。例如,在某些国家/地区,您在开发医疗应用程序时不能参考通用库(它可能不符合法规要求,因此不会得到卫生当局的批准)

标签: android date calendar simpledateformat


【解决方案1】:

"HH:mm" 用于 24 小时格式,"hh:mm a" 用于 12 小时格式

更新 1

下面是一个完整的例子,在 textview 上显示你想要的内容:

    TextView timeNow = findViewById(R.id.time_now);

    Calendar cal, atMidnight;

    //SimpleDateFormat df = new SimpleDateFormat("dd/MM/YYYY hh:mm a");
    SimpleDateFormat df = new SimpleDateFormat("hh:mm a");

    cal = Calendar.getInstance();
    atMidnight = Calendar.getInstance();

    atMidnight.add(Calendar.DATE, 1);
    atMidnight.set(Calendar.HOUR_OF_DAY, 0);
    atMidnight.set(Calendar.MINUTE, 0);
    atMidnight.set(Calendar.SECOND, 0);

    cal.add(Calendar.MINUTE, 30);

    String txt = "";

    while (cal.getTime().getTime() < atMidnight.getTime().getTime()) {
        txt = txt + df.format(cal.getTime()) + "\n";
        cal.add(Calendar.MINUTE, 30);
    }

    timeNow.setText(txt);

更新 2

要将分钟舍入到接下来的 30 分钟,您可以执行以下操作:

while (cal.getTime().getTime() < atMidnight.getTime().getTime()) {
    int unroundedMinutes = cal.get(Calendar.MINUTE);
    int mod = unroundedMinutes % 30;
    cal.add(Calendar.MINUTE, 30-mod);
    txt = txt + df.format(cal.getTime()) + "\n";
    cal.add(Calendar.MINUTE, 30);
    }

timeNow.setText(txt);

【讨论】:

  • 我希望它通过添加 30 分钟从当前时间开始显示。请查看问题示例
  • 在您的代码中,仅显示当前时间加上 30 分钟,但我希望它从当前时间开始显示一整天,这就是我采取 loop.ex-12:30AM,1:00 的原因。 ....11:30PM
  • 仅供参考,java.util.Datejava.util.Calendarjava.text.SimpleDateFormat 等麻烦的旧日期时间类现在已被 java.time 类所取代。大多数 java.time 功能在 ThreeTen-Backport 项目中被反向移植到 Java 6 和 Java 7。进一步适用于ThreeTenABP 中的早期 Android (How to use ThreeTenABP…
  • @Davi 你能把它总结为 10:30,11:00,11:30 等等
  • 要将其四舍五入到接下来的 30 分钟,您需要找到 mod 并将其添加到 cal 分钟。检查我的第二次更新
【解决方案2】:

java.time 和 ThreeTenABP

    Duration interval = Duration.ofMinutes(30);
    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
    ZoneId zone = ZoneId.of("Europe/Podgorica");
    ZonedDateTime now = ZonedDateTime.now(zone);
    // Start at a whole half hour no earlier than now
    ZonedDateTime start = now.truncatedTo(ChronoUnit.HOURS);
    while (start.isBefore(now)) {
        start = start.plus(interval);
    }
    // End when a new day begins
    ZonedDateTime limit = now.toLocalDate().plusDays(1).atStartOfDay(zone);

    // Iterate
    ZonedDateTime currentTime = start;
    while (currentTime.isBefore(limit)) {
        System.out.println(currentTime.format(timeFormatter));
        currentTime = currentTime.plus(interval);
    }

刚才运行sn-p的时候,得到如下输出:

20:30
21:00
21:30
22:00
22:30
23:00
23:30

当然,用我放置欧洲/波德戈里察的你想要的时区替换。

我使用了以下导入:

import org.threeten.bp.Duration;
import org.threeten.bp.ZoneId;
import org.threeten.bp.ZonedDateTime;
import org.threeten.bp.format.DateTimeFormatter;
import org.threeten.bp.temporal.ChronoUnit;

问题:我可以在我的 Android API 级别上使用 java.time 吗?

是的,java.time 在较旧和较新的 Android 设备上运行良好。它只需要至少 Java 6

  • 在 Java 8 及更高版本以及新的 Android 设备(从 API 级别 26 开始)中,现代 API 是内置的。在这种情况下,从带有子包的 java.time 导入(不是 org.threeten.bp)。
  • 在 Java 6 和 7 中,获取 ThreeTen Backport,即新类的后向端口(对于 JSR 310,ThreeTen 是现代 API 的首次描述)。
  • 在(较旧的)Android 上,使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。确保从包 org.threeten.bp 和子包中导入日期和时间类。

链接

【讨论】:

  • V.V 需要 minSDKVersion 26 才能运行
  • @ShobhaDash 不,它没有。在低于 26 的 API 级别上,它需要 ThreeTenABP 库才能运行。请查看我的编辑。
  • 我应该添加依赖 com.jakewharton.threetenabp:threet
  • 是的,就是这样。
  • 我得到异常 org.threeteen.bp.zone.ZoneRulesException: 没有注册时区数据文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-27
  • 2014-06-13
  • 1970-01-01
  • 2020-07-02
  • 2020-01-30
  • 1970-01-01
相关资源
最近更新 更多