Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointing to previous working day using java

Tags:

java

I am new with using java.calendar.api. I want to point to the previous working day for a given day using java. BUT the conditions goes on increasing when i am using calendar.api to manipulate dates since I had to consider the usual weekends and the pointing to the previous month and also i had to consider the regional holidays in my region......

for ex:say i had to consider the U.S holidays and point to the day before that.

Is there any way i can define my own calendar and use it so that date manipulation senses all those usual changes?

like image 573
crazypaladin Avatar asked May 06 '11 07:05

crazypaladin


People also ask

How can I add previous date in Java?

LocalDate. Use LocalDate 's plusDays() and minusDays() method to get the next day and previous day, by adding and subtracting 1 from today.

How do you check if a date is after another date in Java?

The after() method is used to check if a given date is after another given date. Return Value: true if and only if the instant represented by this Date object is strictly later than the instant represented by when; false otherwise.

How do I get the day of the week in Java?

To get the day of the week, use Calendar. DAY_OF_WEEK.


2 Answers

While you should consider using the Joda Time library, here's a start with the Java Calendar API:

public Date getPreviousWorkingDay(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    int dayOfWeek;
    do {
        cal.add(Calendar.DAY_OF_MONTH, -1);
        dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    } while (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY);

    return cal.getTime();
}

This only considers weekends. You'll have to add additional checks to handle days you consider holidays. For instance you could add || isHoliday(cal) to the while condition. Then implement that method, something like:

public boolean isHoliday(Calendar cal) {
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

    if (month == 12 && dayOfMonth == 25) {
        return true;
    }

    // more checks

    return false;
}
like image 180
WhiteFang34 Avatar answered Oct 15 '22 23:10

WhiteFang34


tl;dr

LocalDate.now ( ZoneId.of ( "America/Montreal" ) )
         .with ( org.threeten.extra.Temporals.previousWorkingDay ()  )

java.time

Java 8 and later has the java.time framework built-in. Inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project.

These new classes replace the notoriously troublesome old date-time classes bundled with the earliest versions of Java, java.util.Date/.Calendar. Avoid the old classes where possible. When you must interface look for newly added conversion methods to switch into java.time for most of your work. Also, the makers of Joda-Time have told us to move to java.time as soon as is convenient.

Basics of java.time… An Instant is a moment on the timeline in UTC. Apply a time zone (ZoneId) to get a ZonedDateTime. For a date-only value without a time-of-day nor a time zone, use LocalDate.

First we get "today" as an example date value. Note how a time zone is required in order to determine the current date even though a LocalDate does not contain a time zone. The date is not simultaneously the same around the globe, as a new day dawns earlier in the east.

LocalDate today = LocalDate.now ( ZoneId.of ( "America/Los_Angeles" ) );

Adjustors

The ThreeTen-Extra project extends java.time with additional or experimental features. These features may or may not eventually be folded into java.time proper. This project provides a Temporals class which provides implementations of adjustors including a couple for nextWorkingDay and previousWorkingDay. Easy to use as seen here.

// The 'Temporals' class is from the ThreeTen-Extra library, not built into Java.
LocalDate previousWorkingDay = today.with ( Temporals.previousWorkingDay () );
LocalDate nextWorkingDay = today.with ( Temporals.nextWorkingDay () );

When Run

Dump to console. Notice how today is a Friday, so the previous working day is -1 (yesterday, Thursday) and the next working day is +3 (Monday).

System.out.println ( "today: " + today + " | previousWorkingDay: " + previousWorkingDay + " | nextWorkingDay: " + nextWorkingDay );

today: 2016-01-22 | previousWorkingDay: 2016-01-21 | nextWorkingDay: 2016-01-25

Saturday & Sunday

This pair of adjustors simply skips over every Saturday and Sunday. It knows nothing of holidays. Nor does it know about other definitions of the working week and weekend. The class documentation suggests writing your own java.time.temporal.TemporalAdjuster is easy if you want to handle other definitions.

like image 20
Basil Bourque Avatar answered Oct 16 '22 00:10

Basil Bourque