-4

How can I find the Last working day of a month in Java.If the last day of a month is Saturday or Sunday the last working day should be on Friday. For example if March 31 is Sunday, the last working day is March 29 Friday.

For getting the current month

Date today = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(today);
calendar.add(Calendar.MONTH, 1);
Renjith Krishnan
  • 2,446
  • 5
  • 29
  • 53
Devika
  • 51
  • 1
  • 6
  • 4
    where is your code – KishanCS Mar 03 '17 at 09:55
  • Have you checked [this](http://stackoverflow.com/questions/13647422/check-if-the-calendar-date-is-a-sunday) – Piyush Mar 03 '17 at 09:57
  • 3
    Hint for beginners: A) you are a beginner - everything you can dream of asking about has been asked. Here. Already. Many. Times. Please do some research the next time. B) and even if you would have done some research: this is not a "we write code for you service". You show us the code you wrote, and point out what doesnt work. So please: spent some more time at the [help] and learn how things work here. – GhostCat Mar 03 '17 at 10:00
  • @KishanCS Added the code – Devika Mar 03 '17 at 10:02
  • @Piyush checked it but not got the answer – Devika Mar 03 '17 at 10:03
  • get the last day, and check if it is a working day. If not - get the pre-last, and check again. And so on until you get the working day. – Vladyslav Matviienko Mar 03 '17 at 10:24

2 Answers2

5

tl;dr

LocalDate.now( ZoneId.of( "America/Montreal" ) )                     // Today
         .with( TemporalAdjusters.firstDayOfNextMonth() )            // First of next month.
         .with( org.threeten.extra.Temporals.previousWorkingDay() )  // Move backwards in time, looking for first day that is not Saturday nor Sunday.

Avoid legacy classes

You are use the troublesome old date-time classes, now legacy, supplanted by the java.time classes.

Table of date-time types in Java, both modern and legacy.

Using java.time

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

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

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

The TemporalAdjuster interface provides a way to adjust date-time values. The TemporalAdjusters class (note the plural s) provides several handy implementations.

LocalDate endOfMonth = today.with( TemporalAdjusters.lastDayOfMonth() );

The ThreeTen-Extra project extends java.time with additional functionality. This includes more TemporalAdjuster implementations. One of those is previousWorkingDay to skip over Saturday and Sunday. To use this, we need to step past the end of month as that day itself may be a working day.

LocalDate previousWorkingDay = endOfMonth.plusDays( 1 ).with( org.threeten.extra.Temporals.previousWorkingDay() );

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.

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

Try This

Date today = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(today);

calendar.add(Calendar.MONTH, 1);//Used for finding next month
calendar.set(Calendar.DAY_OF_MONTH, 1);//Setting the Day of month as 1 for starting    
do{
       calendar.add(Calendar.DATE, -1); //In the first case decease day by 1 so get the this months last day
   } while (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
           || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ); // Checking whether the last day is saturday or sunday then it will decrease by 1
    Date lastDayOfMonth = calendar.getTime();
    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println("Today: " + sdf.format(today));
    System.out.println("Last Day of Month: " +df.format(lastDayOfMonth));
Renjith Krishnan
  • 2,446
  • 5
  • 29
  • 53
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Mar 24 '17 at 22:32