0

Lets say I have 2 dates in milliseconds or java.sql.Timestamp:

1342162320 <->  Fri Jul 13 2012 09:52:00 GMT+0300 (FLE Daylight Time)
1343162320 <-> Tue Jul 24 2012 23:38:40 GMT+0300 (FLE Daylight Time)

Lets say I provide these 2 dates, and I need:

Return int, if the time between JUL 13 09:52 - JUL 24 23:58 falls on weekends or big holidays, and if so, how many days.

I thought about just going through all dates, and checking if it is saturday or sunday, but maybe some more clever APIs?

I also looked into Joda library, but it does not specifically have these methods, just has some simplified JDK ones. Joda does not have holiday recognition.

5
  • Holidays? You mean like for every country in the world with all variations? What about different religions in same country, their religious holidays dont match.
    – bezmax
    Jul 13, 2012 at 7:47
  • @Max Hmm, I mean like RED holidays in calenders, only the big ones. Kind of global ones.
    – Jaanus
    Jul 13, 2012 at 7:48
  • Take a look at stackoverflow.com/questions/1044921/… , that might be the think you are looking for. Jul 13, 2012 at 7:49
  • 2
    There are no "global" holidays. All of them are country/religion specific. Or are you good with providing list of "red" dates manually, as per @ortang's solution?
    – bezmax
    Jul 13, 2012 at 7:49
  • Those numbers look like a count from 1970 in whole seconds not milliseconds. Aug 15, 2016 at 0:28

3 Answers 3

1

You can use defined holiday calendars from http://www.calendarlabs.com/ical-calendar-holidays.php, they are available for a number of countries. It's format is called iCal, used by MS Outlook. It shouldn't be to hard to write a parser for that format. Then you have the 'red' dates and don't have to define and update, just download a new definition for every year.

It might also be possible to get the holidays from Google Calendars or similar.

0

Yes, This is very easy,Just follow the below codes:

    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    //cal.setTimeInMillis(milliseconds); use this one instead if you have milliseconds. 
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    if(Calendar.SUNDAY == dayOfWeek || Calendar.SATURDAY == dayOfWeek){
        System.out.println("it is saturday or sunday")
   }

This can recognize that the day is Saturday or Sunday, but if you want to determine whether it is public holiday? eg: national holiday, I would say that could be impossible via JDK, since this is very in accordance on different regions or countries, it is not permanent.

0

tl;dr

Boolean isWeekend = EnumSet.of( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY ).contains ( Instant.ofEpochSecond( 1342162320L ).atZone( ZoneId.of( "Europe/Helsinki" ) ).getDayOfWeek() )

Details

The other Answers are correct but use outmoded legacy classes. Use java.time classes instead.

The weekend part of the Question is easy with built-in Java libraries. You can define the set of days that is a weekend, such as Saturday & Sunday in the United States.

Covering holidays requires some kind of external library, web service, or database. The definition of holidays varies over time and by jurisdiction and by tradition. I will ignore that part of the Question; see the Answer by Kennet as one possible solution.

DayOfWeek

The DayOfWeek enum identifies each of the seven days of the week. You can interrogate a ZonedDateTime for its DayOfWeek.

First parse you input number. Appears to be a count of whole seconds since the epoch of first moment of 1970 in UTC. Note the L on the end to make this a long integer.

Instant instant = Instant.ofEpochSecond( 1342162320L );

Apply a time zone.

ZoneId zoneId = ZoneId.of( "Europe/Helsinki" );
ZonedDateTime zdt = instant.atZone( zoneId );

Test for weekend by asking if the day of week is either Saturday or Sunday.

DayOfWeek dow = zdt.getDayOfWeek();
Boolean isWeekend = ( dow.equals( DayOfWeek.SATURDAY ) || dow.equals( DayOfWeek.SUNDAY ) );

EnumSet

You could also use an EnumSet, for fast execution and low memory usage.

Set<DayOfWeek> weekend = EnumSet.of( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY );

Declare this as a static final constant if its definition does not change during execution of your app.

static final Set<DayOfWeek> WEEKEND = EnumSet.of( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY );

When you get a DayOfWeek object, see if it is contained in the EnumSet which is an implementation of Set interface.

Boolean isWeekend = WEEKEND.contains( dow.getDayOfWeek() );

java.time

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

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

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

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.

Not the answer you're looking for? Browse other questions tagged or ask your own question.