How can I add business days to the current date in Java?

后端 未结 14 1404
Happy的楠姐
Happy的楠姐 2020-12-03 02:05

How can I add business days to the current date in Java?

public Calendar addBusinessDate(Calendar cal, int days) {
//
// code goes over here
//
}
         


        
14条回答
  •  感情败类
    2020-12-03 02:27

    tl;dr

    Going forward.

    myLocalDate.with( 
        org.threeten.extra.Temporals.nextWorkingDay() 
    )
    

    Going backward.

    myLocalDate.with( 
        org.threeten.extra.Temporals.previousWorkingDay() 
    )
    

    Using java.time

    The Question and other Answers use the troublesome old date-time classes, now legacy, supplanted by the java.time classes.

    Also, see my Answer to a similar Question.

    TemporalAdjuster

    In java.time, the TemporalAdjuster interface provides for classes to manipulate date-time values. Using immutable objects, a new instance is created with values based on the original.

    nextWorkingDay

    The ThreeTen-Extra project extend java.time with additional functionality. That includes a nextWorkingDay adjuster that skips over Saturday and Sunday days. So we can loop, incrementing a date one day at a time, and skip over any weekend days.

    The LocalDate class represents a date-only value without time-of-day and without time zone.

    LocalDate start = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;
    int businessDaysToAdd = 13 ;
    // … ensure that: ( businessDaysToAdd >= 0 )
    
    int daysLeft = businessDaysToAdd ;
    LocalDate localDate = start ;
    while ( daysLeft > 0 ) {
        localDate = localDate.with( Temporals.nextWorkingDay() );
        daysLeft = ( daysLeft - 1 ) ;  // Decrement as we go.
    }
    return localDate ;
    

    Holidays

    Holidays are an entirely different matter. Obviously there is no simple solution. You must either supply a list of your honored holidays, or obtain a list with which you agree.

    Once you have such a list, I suggest writing your own implementation of TemporalAdjuster similar to nextWorkingDay.


    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    Where to obtain the java.time classes?

    • Java SE 8 and SE 9 and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

提交回复
热议问题