java - 添加时间以生成时间表时遇到问题

标签 java

我正在尝试用 Java 生成时间表。我读过 this至于如何将两次相加。在转换为使用时间之前,我使用 float 编写代码,因此我知道代码的一般格式有效。这是我遇到困难的代码:

public class Test2 {

    public static void main(String[] args){
        String time = "09:00";
        String quarterHour = "00:15";
        String halfHour = "00:30";
        String quarterHour3 = "00:45";
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        Date times;
        Date temp;
        long sum;


        try {
            times = timeFormat.parse(time);
            while(times.before(timeFormat.parse("15:15"))){
                System.out.println("Timelist: " + time);
                if((times.equals(timeFormat.parse("10:15"))) || (times.equals(timeFormat.parse("13:45")))){
                    temp  = timeFormat.parse(halfHour);
                    sum   = times.getTime() + temp.getTime();
                    time  = timeFormat.format(new Date(sum));
                    times = timeFormat.parse(time);
                }
                else if(times.equals(timeFormat.parse("11:45"))){
                    temp  = timeFormat.parse(quarterHour3);
                    sum   = times.getTime() + temp.getTime();
                    time  = timeFormat.format(new Date(sum));
                    times = timeFormat.parse(time);
                }
                else{
                    temp  = timeFormat.parse(quarterHour);
                    sum   = times.getTime() + temp.getTime();
                    time  = timeFormat.format(new Date(sum));
                    times = timeFormat.parse(time);
                }
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

我从中得到的结果只是 09:00。它经过一次循环并结束。

我通过调试器跟踪它,发生的事情是,当它将 quarterHour 添加到时间时,它添加了 12:15 而不是它应该添加的 00:15。

这似乎与我使用 24 小时制有关,因为我更改了:

SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");

到:

SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm");

它有效 - 除了它进入一个永恒的循环。

问题:如何在使用 24 小时制时只增加 15 分钟?

最佳答案

使用 Calendar ,或者如果您使用的是 Java 8,则可以使用新的 java.time 类,例如

String timeStr = "09:00";
DateFormat timeFormat = new SimpleDateFormat("HH:mm");
try {
    LocalDateTime endTime = LocalDateTime.ofInstant(
            Instant.ofEpochMilli(timeFormat.parse("15:15").getTime()),
            ZoneOffset.ofHours(0));
    Instant instant = Instant.ofEpochMilli(timeFormat.parse(timeStr)
            .getTime());
    LocalDateTime time = LocalDateTime.ofInstant(instant,
            ZoneOffset.ofHours(0));
    while (time.isBefore(endTime)) {
        time = time.plus(15, ChronoUnit.MINUTES);
        Instant output = time.atZone(ZoneOffset.ofHours(0)).toInstant();
        System.out.println(timeFormat.format(Date.from(output)));
    }
} catch (Exception e) {
    e.printStackTrace();
}

或者,使用 Calendar 之类的

String timeStr = "09:00";
DateFormat timeFormat = new SimpleDateFormat("HH:mm");
try {
    Calendar cal = Calendar.getInstance();
    cal.setTime(timeFormat.parse(timeStr));
    Date when = timeFormat.parse("15:15");
    while (cal.getTime().before(when)) {
        cal.add(Calendar.MINUTE, 15);
        System.out.println(timeFormat.format(cal.getTime()));
    }
} catch (Exception e) {
    e.printStackTrace();
}

关于java - 添加时间以生成时间表时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27975985/

相关文章:

java - 安卓 "Cannot resolve symbol ' @id/myLabel"

java - 使用域而不是 localhost/ip 地址访问 Web 应用程序会导致找不到网页?

java - java中不同类加载器的需求是什么

java - 在 Java 中生成真值表

java - 如何在 Scala 的单元测试中利用不同类的变量?

Java - 创建日期列表

java - 在 Java 中如何舍入到最接近的 20 的倍数?

java - 使用 Java 确定更高维度的共面(或共线)点

Java swing - 如何设置指定树节点可编辑而不是整个树

java - 如何在没有 ParseException 的情况下使用 SimpleDateFormat 测试解析日期