Java 无法添加时间以生成时间列表

Java 无法添加时间以生成时间列表,java,Java,我试图用Java生成一个时间列表。我读过关于如何加两次的书。在转换为使用时间之前,我使用float编写代码,因此我知道代码的一般格式是有效的。这是我遇到困难的代码: public class Test2 { public static void main(String[] args){ String time = "09:00"; String quarterHour = "00:15"; String halfHour = "00:30

我试图用Java生成一个时间列表。我读过关于如何加两次的书。在转换为使用时间之前,我使用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。它通过一次循环并结束

我通过调试器跟踪了它,结果是,当它将四分之一小时添加到时间中时,它会添加12:15,而不是应该添加的00:15

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

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

它是有效的——只是它进入了一个永恒的循环


问题:如何让它在使用24小时格式时只增加15分钟的时间?

将这一行添加到代码中:

timeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
声明
timeFormat
后立即执行

它解决了您在我的计算机上的问题。

使用,或者如果您使用的是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();
}
或者,使用
日历

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