0

I have a requirement like when I insert values into a table in my DB, I need to set target dates which is 3 months apart from the day it was inserted. For Eg: If I insert data on 9th November 2013, My target dates will be 9th Feb 2014, 9th May 2015 and so on. Am storing these target dates in respective columns as well. For this am using Calendar function and adding 3 months to the current instance. Now business does not want my target dates to come on a saturday or Sunday. If it comes, I need to add the required days so that it comes on a weekday.

Any suggestions on how we can do it

Sanjai Palliyil
  • 599
  • 2
  • 11
  • 27
  • 1
    If it falls on a Sunday, do you go the next day? If you land on a Saturday, goes it go to the previous day? Also, if this isn't a class assignment, you might want to keep track of holidays too. Also, how do have to fill in Nov 30th? – LazyCubicleMonkey Oct 10 '13 at 04:16
  • If it falls on a sunday, then monday will have to be taken and If it falls on a saturday again monday will have to be taken. Yes we need to keep track of holidays as well. But my question is how do I determine what day of the week is the target date coming to – Sanjai Palliyil Oct 10 '13 at 04:20
  • Look into: http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#get(int) using the constants at the top of the page. – LazyCubicleMonkey Oct 10 '13 at 04:29

2 Answers2

2

Start from what you already know. You know you can use a Calendar to add/subtract values to/from and generate a resulting value....

Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.DATE, 9);
cal.set(Calendar.MONTH, Calendar.NOVEMBER);
cal.set(Calendar.YEAR, 2013);
System.out.println("Start at " + cal.getTime());

cal.add(Calendar.MONTH, 3);
System.out.println("End at " + cal.getTime());

Which outputs...

Start at Sat Nov 09 00:00:00 EST 2013
End at Sun Feb 09 00:00:00 EST 2014

Now, you need to use this concept to move the day till it's not a week end. You can get the "day" name using Calendar.DAY_OF_WEEK and simply either add or subtract a day to the Calendar.DATE based on your business rules, for example...

while (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
    cal.add(Calendar.DATE, 1);
}
System.out.println("Should end at " + cal.getTime());

Which will (based on the previous example), output...

Should end at Mon Feb 10 00:00:00 EST 2014

Take a closer look at Calendar for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

java.time

The modern approach uses the java.time classes, extended by the ThreeTen-Extra project.

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

LocalDate start = LocalDate.of( 2013 , Month.NOVEMBER , 9 ) ;

Step back one day, then ask for the next working day (non-Saturday/Sunday) by using an implementation of TemporalAdjuster found in org.threeten.extra.Temporals.

LocalDate startAgain = 
    start.minusDays( 1 )
         .with( org.threeten.extra.Temporals.nextWorkingDay() ) ;

Add three months for next date. Again, step back a day and ask for next working day.

LocalDate threeMonthsLaterWorkingDay = 
    startAgain.plusMonths( 3 )
              .minusDays( 1 )
              .with( org.threeten.extra.Temporals.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?

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154