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.