cancel
Showing results for 
Search instead for 
Did you mean: 

fetching previous month's date

Former Member
0 Kudos

Hii,

Can anyone provide me the code for retrieving firstWorkingDay for previous month ??

Accepted Solutions (1)

Accepted Solutions (1)

former_member185086
Active Contributor
0 Kudos

Hi

Just add on another method in my previous given class and called it through main method thats it

Note : Again Previous month first working day could be sunday So I have put the proper checks

public void getPrevoiosMonthWorkingDay() {
		Calendar calender = Calendar.getInstance();

		calender.set(Calendar.DATE, 1);
		calender.add(Calendar.MONTH, -1);
		
		if(getDay(calender).equals("SUNDAY"))
		{
			calender.set(Calendar.DATE, 2);
		}
		else
		{
			calender.set(Calendar.DATE, 1);
		}
		System.out.println("Previous month working day :"
				+ format.format(calender.getTime()));
	}

and

testUtilSeries.getPrevoiosMonthWorkingDay();

Best Regards

Satish Kumar

pravesh_verma
Active Contributor
0 Kudos

Hi,

Seems you guys are already in discussion regarding this Util class ..

Its true, Parama you can easily use this method. However it is similar to what I have posted above. But since you are already implementing in the way Satish has suggested you, therefore it will be easy to integrate this method instead of what I have given.

Please note that the code to access the calander API will always be same. You just need to search it !

Its really very easy!!

Cheers! Carry on Guys..

Pravesh

former_member185086
Active Contributor
0 Kudos

Hi Parvesh

Just for your information that it is not just a copy paste of searched code sometimes requirement is entirely different and need your personal attention to solve it. Actual requirement was [this|] and I have spend around 2-3 hours to solve it out (and learn a lot form this exercise that was my objective).

It good you provided the code for this reply but didn't went through it (Check this code for February month and then see the parama' question again, u will feel the difference which proof my first point again. )

Well nice interaction with you.

Best Regards

Satish Kumar

pravesh_verma
Active Contributor
0 Kudos

Hi Satish,

First of all, please check my name, dear it's Pravesh not Parvesh!! Anyways, not your problem most of the time people take it the other way!

Coming to the point, I totally agree with you!! And I am happy to know that you have understood the difference of the just copy and paste of the code without understanding the requirement. Couple of time I have seen the same thing with your reply. Dear please understand one thing, we all are here to help others.

I think you would agree with me on the point that the requirement which Parama is having, is not the rocket science, its really very basic requiement and the code which I have given is the very basic code which explains the use of the Calander API's.

I always believe that you should teach people instead of spoon feed them. However sometime they require code snippets.. I would have loved it if Parama would have spend those 2 hours instead of you spending it. The learning would have been his instead of yours..

I have just given the complete code from a link and have expected him to do the R & D.

Anyways, no probs.. Thanks for making out this point!

Thanks and Regards,

Pravesh

Former Member
0 Kudos

Hi Satish,

Thanks a lot for providing me the code but the thing is the following code provided by you I implemented in Webdynpro giving me 1st feb which is a Sunday and not Monday's date(first working day).

public void firstWorkingDayPreviousMonth( )

{

//@@begin firstWorkingDayPreviousMonth()

String DATE_FORMAT_NOW = "yyyy-MM-dd";

SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_NOW);

Calendar calender = Calendar.getInstance();

calender.set(Calendar.DATE, 1);

calender.add(Calendar.MONTH, -1);

if(getDay(calender).equals("SUNDAY"))

{

calender.set(Calendar.DATE, 2);

}

else

{

calender.set(Calendar.DATE, 1);

}

wdComponentAPI.getMessageManager().reportSuccess("Previous month working day :"

+ format.format(calender.getTime()));

//@@end

}

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Parama,

Refer to my last post for working code.

Kind Regards,

Nitin

Former Member
0 Kudos

Hi Parama,

Refer the below code.

Calendar calender = Calendar.getInstance();

int month = calendar.get(Calendar.MONTH);

calender.set(Calendar.DATE, 1);

if(month != 0) // Check if the current month is not January

calender.set(Calendar.MONTH, (month-1) );

else

{

calender.set(Calendar.MONTH, 11); //previous month is then December

int year = calendar.get(Calendar.YEAR);

calender.set(Calendar.YEAR, (year-1) ); // year needs to be decreased by 1 too

}

if(calender.get(Calendar.DAY_OF_WEEK) == 1) // Check if 1st is Sunday

{

calender.set(Calendar.DATE, 2);

}

System.out.println("Previous month first working date is " +calender.get(Calendar.DATE));

Kind Regards,

Nitin

PS: @Satish's code

Using add() is better in two ways - takes care of proper year setting & forces an immediate recomputation of the calendar's milliseconds and all fields.

I feel there is no need of else condition as date is already set to 1

Edited by: Nitin Jain on Mar 23, 2009 8:35 AM

PPS: Verified to be working for last month

Edited by: Nitin Jain on Mar 25, 2009 11:54 AM

pravesh_verma
Active Contributor
0 Kudos

Hi,

Please check code:


import java.text.*;
import java.util.*;

public class DateUtils {

  public enum IntervalType {  Month, Week  }
  
  private DateUtils() {  }

  public static Calendar[] getDateIntervals(IntervalType type, Calendar reference) {
    if (reference == null) {
      reference = Calendar.getInstance();
    }
    Calendar startDate = (Calendar)reference.clone();
    Calendar endDate = (Calendar)reference.clone();

    if (type == IntervalType.Month) {
      // first date of the month
      startDate.set(Calendar.DATE, 1);
      // previous month
      startDate.add(Calendar.MONTH, -1);

      // first date of the month
      endDate.set(Calendar.DATE, 1);
      // previous month, last date
      endDate.add(Calendar.DATE, -1);
    }
    else {
      // previous week by convention (monday ... sunday)
      // you will have to adjust this a bit if you want
      // sunday to be considered as the first day of the week.
      //   start date : decrement until first sunday then 
      //   down to monday  
      int dayOfWeek = startDate.get(Calendar.DAY_OF_WEEK);
      while (dayOfWeek  != Calendar.SUNDAY) {
        startDate.add(Calendar.DATE, -1);
        dayOfWeek = startDate.get(Calendar.DAY_OF_WEEK);
      }
      while (dayOfWeek != Calendar.MONDAY) {
        startDate.add(Calendar.DATE, -1);
        dayOfWeek = startDate.get(Calendar.DAY_OF_WEEK);
      }

      // end date , decrement until the first sunday
      dayOfWeek = endDate.get(Calendar.DAY_OF_WEEK);
      while (dayOfWeek  != Calendar.SUNDAY) {
        endDate.add(Calendar.DATE, -1);
        dayOfWeek = endDate.get(Calendar.DAY_OF_WEEK);
      }
    }
    return new Calendar[] { startDate, endDate }; 
  }

  public static void main(String[] args) {
    try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

      System.out.println("** previous month (relative today)");
      Calendar [] results = DateUtils.getDateIntervals(IntervalType.Month, null);
      System.out.println(sdf.format(results[0].getTime()));
      System.out.println(sdf.format(results[1].getTime()));
      
      System.out.println("** previous week (relative today)");
      results = DateUtils.getDateIntervals(IntervalType.Week, null);
      System.out.println(sdf.format(results[0].getTime()));
      System.out.println(sdf.format(results[1].getTime()));

      System.out.println("** previous month (relative jan 1, 2007)");
      results = DateUtils.getDateIntervals(IntervalType.Month, 
        new GregorianCalendar(2007, 00, 1));
      System.out.println(sdf.format(results[0].getTime()));
      System.out.println(sdf.format(results[1].getTime()));
      
      System.out.println("** previous week (relative jan 1, 2007)");
      results = DateUtils.getDateIntervals(IntervalType.Week, 
        new GregorianCalendar(2007, 00, 1));
      System.out.println(sdf.format(results[0].getTime()));
      System.out.println(sdf.format(results[1].getTime()));

    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  /*
    output :
    ** previous month (relative today)
    2008-06-01
    2008-06-30
    ** previous week (relative today)
    2008-06-30
    2008-07-06
    ** previous month (relative jan 1, 2007)
    2006-12-01
    2006-12-31
    ** previous week (relative jan 1, 2007)
    2006-12-25
    2006-12-31
  */  
}

this will solve all your requirements!

Thanks

Pravesh