0

Hi we are migrating an application from ASP to Java.In ASP they have used the below function for finding the firstweekday for a given month and year.

for(var i=0; i < 12; i++) {
    iPrintDay = 1;
    iStopFlag = 0;
    iDays=0;
    iLastDay = arrMonthDay[i];
    datFirstDay = new Date(sParmYear,i, 1);  
    iFirstWeekday = datFirstDay.getDay();
}


From above for loop, the sysout of datFirstDay will return Wed Jan 1 00:00:00 CST 2014 and the sysout of iFirstWeekday will return 3[which is the starting weekday
for the month of Jan for the year 2014 which they are passing] for the first iteration of the for loop
given above.


I am trying to find the value of iFirstWeekday which is FIRST WEEKDAY STARTING FOR EVERY MONTH for a given year in Java.
I tried lot of solutions but none of them meets my expectation.
Any suggestions will be really helpful to resolve the issue

zulu
  • 63
  • 1
  • 5
  • 1
    It doesn't help that you haven't showed *any* of the solutions you've already tried, or explained why they're not doing what you expect. Which version of Java are you using? Can you use Joda Time if you're not using Java 8? You can do all this with `java.util.Calendar`, but either `java.time.*` or Joda Time would be better. – Jon Skeet Aug 20 '14 at 10:22
  • @JonSkeet Thanks for your reply.I am uinsg Java 6 .Here is the URL which i tried http://stackoverflow.com/questions/8940438/number-of-days-in-particular-month-of-particular-year.But thats giving the first day of week and i need first weekday for every month.Also i tried the solution in the google page results fyr ..https://www.google.com/search?q=find+first+week+day+for+every+month+in+java&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=sb. – zulu Aug 20 '14 at 10:50

2 Answers2

2

You can try to use Calendar

Calendar c = new GregorianCalendar(2014, Calendar.JANUARY, 1);
for(int i = 0 ;i < 12 ; i++){
    System.out.println(c.getTime());
    c.add(Calendar.MONTH, 1);
}
  • @cyril.Thank you cyril.Your code gives me the correct values in the SOP Wed Jan 01 00:00:00 IST 2014, Sat Feb 01 00:00:00 IST 2014 ...so on.But I need the integer value of that weekday which in this it will be 3 for Wed Jan 01 and so on.Any idea how can I extract such value of the day from the present code which you have given ? – zulu Aug 20 '14 at 11:38
  • 1
    @Techytech, you have to add this line : System.out.println(c.get(Calendar.DAY_OF_WEEK)); The first day is Sunday 1, Monday 2 ... – cyril wirth Aug 20 '14 at 11:53
1

You can add the following line to Cyril's code:

System.out.println(c.get(Calendar.DAY_OF_WEEK));

wednesday results in 4, but I doubt if you have any difficulties in substracting 1 from the result.

Jaap D
  • 501
  • 5
  • 18
  • Thanks Jaap.Since,there is no other way than subtracting. I am proceeding with cyril solution.Thanks a lot!! – zulu Aug 20 '14 at 12:09