简体   繁体   English

总结java.sql.Time对象

[英]Summing java.sql.Time objects

I have a list of time objects (representing the length of a song) that I would like to add together to get the total time. 我有一个时间对象列表(代表一首歌曲的长度),我想将它们加在一起以获得总时间。

Looking at the documentation for java.sql.Time objects it seems that the date part should be untouched and the number of milliseconds will represent the number of milliseconds since 1 Jan 1970, with a negative value meaning before 1970. 查看java.sql.Time对象的文档,似乎日期部分应该保持不变,毫秒数将表示自1970年1月1日以来的毫秒数,其中负值表示1970年前。

I have a Time object (lets call it time ) in the format 00:06:25 ie 6 mins 25 secs. 我有一个00:06:25格式的Time对象(可以称其为time ),即6分25秒。 If I call new Date(time.getTime()) I get Thu Jan 01 00:06:25, which is sensible. 如果我调用new Date(time.getTime())我会得到1月1日星期四00:06:25,这是明智的。 However, when I simply print the value of getTime() I get a value of -3215000, which makes absolutely no sense, this value would indicate that the time was before 1 Jan 1970...which it isn't. 但是,当我简单地打印getTime()的值时,我得到的值是-3215000,这绝对没有意义,该值表示时间早于1970年1月1日,而实际上不是。

Therefore when I try to add multiple calls of getTime() together (like 00:03:35/-3385000) I get a more and more negative number (-6600000). 因此,当我尝试将多个getTime()调用加在一起(如00:03:35 / -3385000)时,我得到的负数越来越多(-6600000)。

So when I then try and create a new Time object with that value I get 23:10:00 rather than 00:09:00. 因此,当我尝试使用该值创建一个新的Time对象时,我得到23:10:00而不是00:09:00。

I created a test job that displayed the following output: 我创建了一个显示以下输出的测试作业:

1 Jan 1970 00:06:25 GMT
1 Jan 1970 00:03:35 GMT
1 Jan 1970 00:03:18 GMT
1 Jan 1970 00:04:22 GMT
1 Jan 1970 00:03:47 GMT
1 Jan 1970 00:00:00 GMT

1 Jan 1970 00:21:27 GMT

The last Time is the sum of the preceding 6 times 最后一次是前6次的总和

The trick is to use the toGMTString() (depreciated) method of Time to generate the output string. 诀窍是使用Time的toGMTString()(不建议使用)方法生成输出字符串。

Here's the code I used: 这是我使用的代码:

package com.ggl.testing;

import java.sql.Time;

public class AddDates implements Runnable {

    private Time[] songLength;

    public AddDates() {
        this.songLength = new Time[6];
        long time = (6L * 60 + 25) * 1000;
        songLength[0] = new Time(time);
        time = (3L * 60 + 35) * 1000;
        songLength[1] = new Time(time);
        time = (3L * 60 + 18) * 1000;
        songLength[2] = new Time(time);
        time = (4L * 60 + 22) * 1000;
        songLength[3] = new Time(time);
        time = (3L * 60 + 47) * 1000;
        songLength[4] = new Time(time);
        time = 0L;
        songLength[5] = new Time(time);
    }

    @Override
    public void run() {
        long sum = 0L;
        for (int i = 0; i < songLength.length; i++) {
            sum += songLength[i].getTime();
            System.out.println(songLength[i].toGMTString());
        }

        Time totalTime = new Time(sum);
        System.out.println(totalTime.toGMTString());
    }

    public static void main(String[] args) {
        new AddDates().run();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM