如何显示从当前时间添加30分钟的时间

问题描述 投票:0回答:2

我只是希望它显示从当前时间添加30分钟直到日结束前 - 今天时间是09:46 AM它应该显示像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);
        }
android date calendar simpledateformat
2个回答
1
投票

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

刚刚运行代码片段时,我得到了以下输出:

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,这是新类的后端(JST 310的ThreeTen,其中首次描述了现代API)。
  • 在(较旧的)Android上,使用Android版的ThreeTen Backport。它被称为ThreeTenABP。确保从包org.threeten.bp和子包中导入日期和时间类。

链接


0
投票

使用"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);
© www.soinside.com 2019 - 2024. All rights reserved.