时间—我希望能够从java方法中的另一个类将长值转换为hh:mm:ss格式

jgwigjjp  于 2021-06-27  发布在  Java
关注(0)|答案(3)|浏览(246)

目前,我有一个播放列表中的总时长,所以我正在尝试使用歌曲中的长字段将播放列表中的方法转换为hh:mm:ss的格式。
目前我正在处理这个。
播放列表类

public class Playlist
{
public double getTotalAmountOfDurationInAPlaylist()
            {
                long sum = 0;
                setTotalPlaylistTime(sum);
                    for (Song individualSong : getSongs())
                        {
                            sum = sum + individualSong.getSongDurationInSeconds() + individualSong.getSongDurationInMinutes() + individualSong.getSongDurationInHours();
                        }
                return sum;
            }
}

在我的歌曲课上,它只是一个叫做songdurationinseconds,songdurationinminutes和songdurationinhours的私人领域。宋类还包括所有的设置和获取领域。
歌曲课

public class Song
{
        private long songDurationInSeconds;
        private long songDurationInMinutes;
        private long songDurationInHours;

        public Song(long songDurationInHours, long songDurationInMinutes, long songDurationInSeconds)
            {
                this.songDurationInHours = songDurationInHours;
                this.songDurationInMinutes = songDurationInMinutes;
                this.songDurationInSeconds = songDurationInSeconds;
            }
}

希望这能澄清这一点,我已经做了一些调整,因为我仍然在研究如何做到这一点。
如果需要更多信息,请告诉我:)

7tofc5zh

7tofc5zh1#

如果您需要它作为字符串,这应该可以解决您的问题

int s= (int) sum;
        System.out.println("Duration: "+String.format("%d:%02d:%02d", s / 3600, (s % 3600) / 60, (s % 60)));

或者使用apachecommons库

DurationFormatUtils.formatDuration(sum*1000l, "**H:mm:ss**", true);
vybvopom

vybvopom2#

java.time.duration文件

当你有时间/日期的事情要做的时候,时间几乎总是你想去的地方。

java.time.Duration d = Duration.ofMillis((long) (1000L * sum));

然后您可以根据需要使用j.t.format包中的datetimeformatter格式化它。

yiytaume

yiytaume3#

这个 java.time.Duration 类表示与时间线无关的时间跨度。

Duration duration = Duration.ZERO.plusHours( x ).plusMinutes( y ).plusSeconds( z ) ;

你可以审问 Duration 对象的完整性,以秒为单位。

long secondsTotal = duration.toSeconds() ;

我建议你考虑换掉你的三个 long 完全分开。只是使用 Duration 类作为成员字段的类型。

public class Song
{
        private String title ;
        private Duration duration ;

        public Song( final Duration d )
            {
                Objects.requiresNonNull( d ) ;
                this.duration = d ; 
            }

        public Song(long songDurationInHours, long songDurationInMinutes, long songDurationInSeconds)
            {
                // … check for valid data, no nulls, appropriate range of values.
                Duration d = Duration.ZERO.plusHours( songDurationInHours ).plusMinutes( songDurationInMinutes ).plusSeconds( songDurationInSeconds ) ;
                this.duration = d ;
            }
}

顺便说一下,这种类最好定义为 record 在Java16和更高版本中。
播放列表类可能如下所示。

public class Playlist
{
    private List < Song > songs ;

public Duration getDuration()
            {
                    Duration total = Duratiin.ZERO ;
                    for (Song song : this.songs )
                        {
                              total = total.plus( song.getDuration() ) ;
                         }
                     return total ; 
            }
}

我还建议避免使用时钟样式的时间来表示跨度。iso8601标准定义了一种文本格式,它不容易被误认为是一天中的某个时间。
这个 Duration 类默认情况下以这种标准格式生成和解析文本。

相关问题