6

我想创建一个一周一周的时间表。

因为从一周到下一周的时间表是相同的,所以我需要存储的唯一信息是一周中的哪一天,以及它发生的时间。例如。Monday 2:30pm. 实际日期并不重要,时区也不重要。

到目前为止,我一直在编写代码,将日期和时间分开,使用DayOfWeek枚举和LocalTime类型来处理时间。但是我已经开始遇到使用两个变量来管理时间的问题,而不是像 DateTime 这样的单一方便类型。例如。如果我想在周二晚上 11 点后 3 小时获得时间,我可以使用该LocalTime.plus()方法获得凌晨 2 点,但这不考虑当天的翻转,我必须单独检查和更新。

我还想确保从周末到开始我所采用的任何解决方案:例如。周日晚上 10 点后 5 小时应该是周一凌晨 3 点。

JDK中是否存在这样的类,或者定义自己的时间类型是否足够容易?还是使用 java 提供的 LocalDateTime 解决问题并以某种方式忽略日期组件会更好?

4

2 回答 2

6

JDK中没有这样的内置类,但是自己创建这样的类应该不会太难。

LocalTime我认为您在使用and方面进展顺利DayOfWeek。确保编写一个包装这两个组件的类,然后添加方法以将时间单位添加到包装器对象。

其中一种方法可能如下所示:

public WeekDayTime plusHours(int hours) {
    int plusDays = (hours / 24);
    int plusHours = hours % 24;
    if (this.time.getHours() + plusHours >= 24) {
        plusDays++;
        plusHours -= 24;
    }
    DayOfWeek newDayOfWeek = ...; // Calculate the day of week somehow
    LocalTime newTime = this.time.plusHours(plusHours);
    return new WeekDayTime(newDayOfWeek, newTime);
}

或者,您也可以包装 aLocalDateTime并隐藏日期组件。这将使您免于实施这些计算。但是,请确保您equals正确实施例如该方法。

于 2020-04-26T12:32:08.483 回答
4

MC Emperor的正确答案启发了我迈出下一步,DayOfWeekTime通过接受更通用的PeriodDuration类对象而不是几个小时来设计一个更灵活地执行日期时间方法的类。

我尽可能地利用了现有的java.time类。我遵循java.time的命名约定。我模拟了java.time的功能。

对于字符串,我查看了java.time使用的ISO 8601标准。不幸的是,该标准没有使用时间概念来解决星期几。该标准确实有一年中星期的概念,并用 1 和 1-52 或 1-53 的数字表示。对于特定一周的特定日期,标准会在周一至周日附加一个带有数字 1-7 的连字符。所以我在我的和方法上遵循了这个模式。我以 开头,省略任何周数,后跟连字符和周数 1-7。我遵循标准的引导附加 a作为分隔符。然后在 24 小时制中附加时间,并在小时和分钟处填充零。例如,produces的日期时间。WtoStringparseWT2020-04-27T13:00-04:00[America/Montreal]W-7T19:46:40.937485

这段代码只经过了勉强测试。使用风险自负。

package work.basil.example;

import java.time.*;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAdjusters;
import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;

// Revised 2020-04-27. Fixed bug where the plus & minus methods adjusted from baseline by time-of-day but not day-of-week. Fixed.

public class DayOfWeekTime
{
    // ----------|  Fields  |------------------------------------------

    private DayOfWeek dayOfWeek;
    private LocalTime localTime;

    // ----------|  Statics  |------------------------------------------

    // We do the date-time math by picking a date arbitrarily to use as a `LocalDateTime`.
    // For convenience, we might as well pick a year that starts on a Monday.
    // https://en.wikipedia.org/wiki/Common_year_starting_on_Monday
    // Let us go with 2001-01-01.
    static private LocalDateTime BASELINE = LocalDateTime.of( 2001 , 1 , 1 , 0 , 0 , 0 , 0 );

    // ----------|  Constructor  |------------------------------------------

    private DayOfWeekTime ( final DayOfWeek dayOfWeek , final LocalTime localTime )
    {
        Objects.requireNonNull( dayOfWeek , "Received a null argument. Message # e3e04fde-d96a-41d8-a4e7-c2b4f2f5634b." );
        Objects.requireNonNull( localTime , "Received a null argument. Message # 97ccf27a-6c05-402a-a4aa-0a48bcff62c2." );
        this.dayOfWeek = dayOfWeek;
        this.localTime = localTime;
    }

    // ----------|  Factory  |------------------------------------------

    static public DayOfWeekTime of ( final DayOfWeek dayOfWeek , final LocalTime localTime )
    {
        Objects.requireNonNull( dayOfWeek , "Received a null argument. Message # ecfe6bf6-de34-4f63-9a3e-d04cd70e721f." );
        Objects.requireNonNull( localTime , "Received a null argument. Message # 83020094-409d-40e1-8dc3-12592eea1b81." );
        return new DayOfWeekTime( dayOfWeek , localTime );
    }

    static public DayOfWeekTime now ( ZoneId zoneId )
    {
        Objects.requireNonNull( zoneId , "Received null argument for time zone. Message # 6044dd82-3616-40a6-8ac2-52581e12e60f." );
        ZonedDateTime now = ZonedDateTime.now( zoneId );
        DayOfWeek dow = now.getDayOfWeek();
        LocalTime lt = now.toLocalTime();
        DayOfWeekTime dayOfWeekTime = DayOfWeekTime.of( dow , lt );
        return dayOfWeekTime;
    }

    // ----------|  Duration  |------------------------------------------
    public DayOfWeekTime plus ( final Duration duration )
    {
        Objects.requireNonNull( duration , "Received a null argument. Message # cf60bd16-3992-4779-a621-a0a3fdb2d750." );
        LocalDateTime ldt = DayOfWeekTime.BASELINE.with( TemporalAdjusters.nextOrSame( this.dayOfWeek ) ).with( this.localTime );
        LocalDateTime ldtSum = ldt.plus( duration );
        DayOfWeekTime dayOfWeekTime = DayOfWeekTime.of( ldtSum.getDayOfWeek() , ldtSum.toLocalTime() );
        return dayOfWeekTime;
    }

    public DayOfWeekTime minus ( final Duration duration )
    {
        Objects.requireNonNull( duration , "Received a null argument. Message # 4e7bf8c9-6e4f-4e3f-a8b1-5a42dc23cd8a." );
        LocalDateTime ldt = DayOfWeekTime.BASELINE.with( TemporalAdjusters.nextOrSame( this.dayOfWeek ) ).with( this.localTime );
        LocalDateTime ldtSum = ldt.minus( duration );
        DayOfWeekTime dayOfWeekTime = DayOfWeekTime.of( ldtSum.getDayOfWeek() , ldtSum.toLocalTime() );
        return dayOfWeekTime;
    }

    // ----------|  Period  |------------------------------------------

    public DayOfWeekTime plus ( final Period period )
    {
        Objects.requireNonNull( period , "Received a null argument. Message # 3b1f65b0-5b2c-4e86-aaa3-527992356d32." );
        LocalDateTime ldt = DayOfWeekTime.BASELINE.with( TemporalAdjusters.nextOrSame( this.dayOfWeek ) ).with( this.localTime );
        LocalDateTime ldtSum = ldt.plus( period );
        DayOfWeekTime dayOfWeekTime = DayOfWeekTime.of( ldtSum.getDayOfWeek() , ldtSum.toLocalTime() );
        return dayOfWeekTime;
    }

    public DayOfWeekTime minus ( final Period period )
    {
        Objects.requireNonNull( period , "Received a null argument. Message # 045938fc-d4b2-4bd2-8803-91db54d92564." );
        LocalDateTime ldt = DayOfWeekTime.BASELINE.with( TemporalAdjusters.nextOrSame( this.dayOfWeek ) ).with( this.localTime );
        LocalDateTime ldtSum = ldt.minus( period );
        DayOfWeekTime dayOfWeekTime = DayOfWeekTime.of( ldtSum.getDayOfWeek() , ldtSum.toLocalTime() );
        return dayOfWeekTime;
    }

    // ----------|  Parsing  |------------------------------------------

    // This text output invented here in this method is styled to follow the designs of ISO 8601,
    // but is most certainly *not* defined in the standard.
    static public DayOfWeekTime parse ( final String input )
    {
        Objects.requireNonNull( input , "Received a null argument. Message # 7c519b65-a1ec-486b-a9e9-ff31ee1b8057." );
        if ( input.isEmpty() || input.isBlank() )
        {
            throw new IllegalArgumentException( "Received empty/blank string as argument. Message # 59993300-bdf9-4e69-82e0-823456715c60." );
        }

        DayOfWeek dayOfWeek = null;
        LocalTime localTime = null;

        // My regex powers are weak.
        // My half-baked scheme returns tokens = [, -1, 13:00] for an input of "W-1T13:00".
        String delimiters = "[WT]+";
        String[] tokens = input.toUpperCase( Locale.US ).split( delimiters );  // ISO 8601 requires the output of uppercase letters while mandating that we accept lowercase.
        System.out.println( "DEBUG tokens = " + Arrays.toString( tokens ) );
        if ( tokens.length != 3 )
        {
            throw new IllegalArgumentException( "Received invalid string as argument. Message # e521a4e3-1ee5-46e9-b351-a5edb7206b82." );
        }

        int dowNumber = Math.abs( Integer.parseInt( tokens[ 1 ] ) );
        dayOfWeek = DayOfWeek.of( dowNumber );

        String localTimeInput = Objects.requireNonNull( tokens[ 2 ] , "The time-of-day component of the input is null. Message # 1faed491-abaa-42bd-b767-d876f8ba07d9." );
        if ( localTimeInput.isEmpty() || localTimeInput.isBlank() )
        {
            throw new IllegalArgumentException( "The time-of-day component of the input is empty/blank. Message # 98208025-d7c2-4b59-bc5f-fed7bec09741." );
        }
        try
        {
            localTime = LocalTime.parse( localTimeInput );
        }
        catch ( DateTimeParseException e )
        {
            throw new IllegalArgumentException( "The time-of-day component of the input is invalid. Message # 8de7e8d8-f4a3-478d-96d8-911454aced14." );
        }

        DayOfWeekTime dayOfWeekTime = DayOfWeekTime.of( dayOfWeek , localTime );
        return dayOfWeekTime;
    }


    // ----------|  Moment  |------------------------------------------

    public ZonedDateTime atDateInZone ( LocalDate localDate , ZoneId zoneId )
    {
        Objects.requireNonNull( zoneId , "Received null argument. Message # b8f70601-2b1d-4321-a57f-96384759a960." );
        LocalDate ld = localDate.with( TemporalAdjusters.nextOrSame( this.dayOfWeek ) ); // Move to the next date with a matching day-of-week if not a match.
        LocalDateTime ldt = LocalDateTime.of( ld , this.localTime );
        ZonedDateTime zdt = ldt.atZone( zoneId );
        return zdt;
    }


    // ----------|  Accessors  |------------------------------------------

    public DayOfWeek getDayOfWeek ( ) { return this.dayOfWeek; }

    public LocalTime getLocalTime ( ) { return this.localTime; }


    // ----------|  Object  |------------------------------------------

    @Override
    public boolean equals ( Object o )
    {
        if ( this == o ) return true;
        if ( o == null || getClass() != o.getClass() ) return false;
        DayOfWeekTime that = ( DayOfWeekTime ) o;
        return dayOfWeek == that.dayOfWeek &&
                localTime.equals( that.localTime );
    }

    @Override
    public int hashCode ( )
    {
        return Objects.hash( dayOfWeek , localTime );
    }

    // This text output invented here in this method  is styled to follow the designs of ISO 8601,
    // but is most certainly *not* defined in the standard.
    @Override
    public String toString ( )
    {
        String output = "W" + "-" + this.dayOfWeek.getValue() + "T" + this.localTime.toString();
        return output;
    }
}

星期日的示例用法。

DayOfWeekTime dayOfWeekTime = DayOfWeekTime.parse( "W-7T13:00" );

DayOfWeekTime nineDaysPrior = dayOfWeekTime.minus( Period.ofDays( 9 ) );
DayOfWeekTime oneDayPrior = dayOfWeekTime.minus( Period.ofDays( 1 ) );
DayOfWeekTime oneDayLater = dayOfWeekTime.plus( Period.ofDays( 1 ) );
DayOfWeekTime twoDaysLater = dayOfWeekTime.plus( Period.ofDays( 2 ) );
DayOfWeekTime weekLater = dayOfWeekTime.plus( Period.ofWeeks( 1 ) );
DayOfWeekTime nineDaysLater = dayOfWeekTime.plus( Period.ofDays( 9 ) );

DayOfWeekTime twoHoursLater = dayOfWeekTime.plus( Duration.ofHours( 2 ) );
DayOfWeekTime nineHoursLater = dayOfWeekTime.plus( Duration.ofHours( 9 ) );
DayOfWeekTime twentyFourHoursLater = dayOfWeekTime.plus( Duration.ofHours( 24 ) );

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = dayOfWeekTime.atDateInZone( LocalDate.now( z ) , z );

DayOfWeekTime now = DayOfWeekTime.now( z );

转储到控制台。

System.out.println( "After parsing: dayOfWeekTime = " + dayOfWeekTime );

System.out.println( "nineDaysPrior = " + nineDaysPrior );
System.out.println( "oneDayPrior = " + oneDayPrior );
System.out.println( "oneDayLater = " + oneDayLater );
System.out.println( "twoDaysLater = " + twoDaysLater );
System.out.println( "weekLater = " + weekLater );
System.out.println( "nineDaysLater = " + nineDaysLater );

System.out.println( "twoHoursLater = " + twoHoursLater );
System.out.println( "nineHoursLater = " + nineHoursLater );
System.out.println( "twentyFourHoursLater = " + twentyFourHoursLater );

System.out.println( "zdt = " + zdt );
System.out.println( "now = " + now );
DEBUG tokens = [, -7, 13:00]
After parsing: dayOfWeekTime = W-7T13:00
nineDaysPrior = W-5T13:00
oneDayPrior = W-6T13:00
oneDayLater = W-1T13:00
twoDaysLater = W-2T13:00
weekLater = W-7T13:00
nineDaysLater = W-2T13:00
twoHoursLater = W-7T15:00
nineHoursLater = W-7T22:00
twentyFourHoursLater = W-1T13:00
zdt = 2020-05-03T13:00-04:00[America/Montreal]
now = W-1T16:10:33.248253

星期一。

DayOfWeekTime dayOfWeekTime = DayOfWeekTime.parse( "W-1T13:00" );
…
DEBUG tokens = [, -1, 13:00]
After parsing: dayOfWeekTime = W-1T13:00
nineDaysPrior = W-6T13:00
oneDayPrior = W-7T13:00
oneDayLater = W-2T13:00
twoDaysLater = W-3T13:00
weekLater = W-1T13:00
nineDaysLater = W-3T13:00
twoHoursLater = W-1T15:00
nineHoursLater = W-1T22:00
twentyFourHoursLater = W-2T13:00
zdt = 2020-04-27T13:00-04:00[America/Montreal]
now = W-1T16:16:11.543665

似乎这个功能真的对人们有用,用于处理工人的轮班或商店的开/关时间。如果这对其他人有意义,也许有人会愿意在ThreeTen-Extra项目的这个拉取请求功能上提供帮助。

于 2020-04-26T23:48:50.987 回答