在java中存储小时和分钟

时间:2015-12-18 18:10:26

标签: jodatime

我在我的应用程序中使用jodaTime,我必须存储小时和分钟,例如56:45(意味着56小时45分钟)。我已经尝试使用LocalTime(joda localtime),但是这种类型只接受24小时,并且alos不能存储在mongoDB中的Spring数据。我现在的问题是使用哪种类型?

目前我有字符串,但我对它不是很满意

3 个答案:

答案 0 :(得分:0)

JodaTimePeriod类,可以代表一段时间。虽然我认为如果您想存储56小时,您可能需要将其保存为2天8小时,然后当您解析它时显示它然而您不喜欢。

http://joda-time.sourceforge.net/apidocs/org/joda/time/Period.html

否则,请查看Java TimeSpan类。

https://docs.oracle.com/javase/tutorial/datetime/iso/period.html

答案 1 :(得分:0)

如果你想创建自己的Time类,它可能看起来像这样:

public class Time {

    private final int minutesPastMidnight;

    public Time(String timeString) {
        int hours = Integer.valueOf(timeString.substring(0, 2));
        int minutes = Integer.valueOf(timeString.substring(3, 5));
        this.minutesPastMidnight = hours * 60 + minutes;
    }

    public Time(Time time) {
        this.minutesPastMidnight = time.getMinutesPastMidnight();
    }

    private int getMinutesPastMidnight() {
        return minutesPastMidnight;
    }

    public int difference(Time time) {
        return this.getMinutesPastMidnight()
                - time.getMinutesPastMidnight();
    }

    public Time min(Time time) {
        return (difference(time) > 0) ? time : this;
    }

    public Time max(Time time) {
        return (difference(time) > 0) ? this : time;
    }

    @Override
    public String toString() {
        int hours = minutesPastMidnight / 60;
        int minutes = minutesPastMidnight - (hours * 60);
        return String.format("%02d:%02d", hours, minutes);
    }
}

答案 2 :(得分:0)

您应始终将UTC中的日期和时间存储为数据库中的java.util.Date

在您的情况下,我认为您必须使用String,而不是使用小时和分钟,您可以将Period对象用作String

我尝试了一些东西,希望对你有所帮助。

 public static void main(String[] args) {

        // You have "56:45" as string, firstly change all this values to Period Object including  days, hours and minutes
        Period newPeriod = convertStringtoPeriod("56:45");
        // Store newPeriod as String in DB
        // When using it crate a Period Object from it and use it.
        System.out.println("***  Period in Main Method  ***  =" + newPeriod);
        // now you have a period object including  days, hours and minutes
        System.out.println("***  Days in Main Method  ***  =" + newPeriod.getDays());
        System.out.println("***  Hours in Main Method  ***  =" + newPeriod.getHours());
        System.out.println("***  Minutes in Main Method  ***  =" + newPeriod.getMinutes());

    }

    public static Period convertStringtoPeriod(String hoursAndMinutes) {
        // Firstly create a formatter for your coming String
        PeriodFormatter dateFormat = new PeriodFormatterBuilder().appendHours().appendSeparator(":").appendMinutes().toFormatter();
        // create a period including hours and minutes
        Period period = dateFormat.parsePeriod(hoursAndMinutes);
        // create total duration of this period
        Duration duration = period.toStandardDuration();
        // get days from duration
        long days = duration.getStandardDays();
        System.out.println("day count    =" + days);
        // subtract days millis from total duration millis
        duration = duration.minus(days * (1000 * 60 * 60 * 24));
        // get hours from duration
        long hours = duration.getStandardHours();
        System.out.println("hour count   =" + hours);
        // subtract hours millis from total duration millis
        duration = duration.minus(hours * (1000 * 60 * 60));
        // get minutes from duration
        long minutes = duration.getStandardMinutes();
        System.out.println("minute count =" + minutes);
        // to Store in DB
        Period periodStoredInDB = new Period(0, 0, 0, (int) days, (int) hours, (int) minutes, 0, 0);

        return periodStoredInDB;
    }

输出是;

day count    =2
hour count   =8
minute count =45
***  Period in Main Method  ***  =P2DT8H45M
***  Days in Main Method  ***  =2
***  Hours in Main Method  ***  =8
***  Minutes in Main Method  ***  =45

在这种情况下,我建议您将P2DT8H45M存储在数据库中String