• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date Problem

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How can i check the last day of the month excluding saturday and sunday.

Here, I have to check for the month end if it falls on saturday or sunday then i have take the prevoius date.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what have you tried? Do you know how to find the last day of the month? Do you know how to find what day of the week a given date is? Do you know how to, given a date, back up one day?

it seems to me if you can do these three things, your problem is solved. where EXACTLY are you stuck?
 
sarma kiran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calenar cal = Calendar.getInstance();
cal.set( Calendar.YEAR, cal.get (Calendar.YEAR) );
cal.set( Calendar.MONTH, cal.get(Calendar.MONTH));
cal.set( Calendar.DATE, cal.get(Calendar.DATE));
Date intialCurrentDate = new Date(cal.getTime().getTime());
cal.setTime(intialCurrentDate);
int monthVal =cal.get(Calendar.MONTH);
int dateVal = cal.get(Calendar.DATE);
int dayofMonth = cal.get(Calendar.DAY_OF_MONTH);
System.out.println(dayofMonth);


For the day check i am using this

int day = cal.get(Calendar.DAY_OF_WEEK);
if(day==1){
cal.add(Calendar.DATE, -2);
currentDate = cal.getTime();
System.out.println(currentDate+" "+cal.getTime());
}else if(day==7){
cal.add(Calendar.DATE, -1);
currentDate = cal.getTime();
System.out.println(currentDate);
}
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sarma kiran:
Calenar cal = Calendar.getInstance();
cal.set( Calendar.YEAR, cal.get (Calendar.YEAR) );
cal.set( Calendar.MONTH, cal.get(Calendar.MONTH));
cal.set( Calendar.DATE, cal.get(Calendar.DATE));
Date intialCurrentDate = new Date(cal.getTime().getTime());
cal.setTime(intialCurrentDate);
int monthVal =cal.get(Calendar.MONTH);
int dateVal = cal.get(Calendar.DATE);
int dayofMonth = cal.get(Calendar.DAY_OF_MONTH);


What exactly are you trying to do here? You create a Calendar object (excellent!), but then you set the year, month and day - to the values it already had!
Next you call getTime() which creates a new Date object, but decide to create yet another Date object equal to the one you just created. Then you use that to set the Calendar's date/time again - to the values it already had!
You can remove all of this code except for the first one and last three lines.



That should work just fine, but please please PLEASE don't use 1 and 7 - use Calendar.SUNDAY and Calendar.SATURDAY.

Now all you seem to be missing is getting your Calendar object to point to the last date of the month.

I can think of two ways:
  • Take the current date and add one month. Set the day of the month to 1 (so it is the first of the next month). Subtract one day.
  • Set the day of the month using Calendar.getActualMaximum. This will return the last day for the current month.

  • [ September 17, 2008: Message edited by: Rob Prime ]
     
    sarma kiran
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Just now am checked calendar.getActualMaximum will return the last day of the month, But my requirement last day excluding satruday and sunday.
     
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    But my requirement last day excluding satruday and sunday.


    That's exactly what Rob's code does, isn't it? Did you study and understand that code?
     
    Rob Spoor
    Sheriff
    Posts: 22781
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well not yet. But Sarma, the code you have written yourself (the second part) will do just that. Once you have the last day it can subtract 0, 1 or 2 days, depending on the week day. If you run that code after my code you should get just exactly what you need.
    [ September 17, 2008: Message edited by: Rob Prime ]
     
    reply
      Bookmark Topic Watch Topic
    • New Topic