my dog learned polymorphism*
The moose likes Beginning Java and the fly likes i want to get the month of a particular date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "i want to get the month of a particular date" Watch "i want to get the month of a particular date" New topic
Author

i want to get the month of a particular date

Chandran Madesh
Greenhorn

Joined: Nov 30, 2007
Posts: 18
Hi i want to get the month of a particular date(in dynamic).
I ahve used the below code, but i am getting month of curent date...
Pl help me...



<b>Chandran.M<br /> <br /> <br /> Do Your Best <br /></b>
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
Calendar.getInstance() returns a Calendar object that represents the current date and time. You would need to use the set or setTime methods to set it to the date you want, but this would require you to know the month, which sort of defeats the purpose.

In what form have you got the date that you want to know the month of ?


Joanne
Chandran Madesh
Greenhorn

Joined: Nov 30, 2007
Posts: 18
Thanks for your reply Joanne, actually i need to get a starting date of a week, if i get the date of month start say 1,2... whose week start date is last months date. In such a situation i am getting the perfect week start date but getting month of current month.... see the below code

code:
--------------------------------------------------------------------------------


import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class WeekStartDate{
public static void main(String[] args){
String sFirstDayOfWeek = "";
Date sDateString = null;
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
//calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH));
calendar.add(Calendar.DATE,4);
int month = calendar.get(Calendar.MONTH);


switch(month)
{
case 0: sFirstDayOfWeek += "JANUARY ";break;
case 1: sFirstDayOfWeek += "FEBRUARY ";break;
case 2: sFirstDayOfWeek += "MARCH ";break;
case 3: sFirstDayOfWeek += "APRIL ";break;
case 4: sFirstDayOfWeek += "MAY ";break;
case 5: sFirstDayOfWeek += "JUNE ";break;
case 6: sFirstDayOfWeek += "JULY ";break;
case 7: sFirstDayOfWeek += "AUGUST ";break;
case 8: sFirstDayOfWeek += "SEPTEMBER ";break;
case 9: sFirstDayOfWeek += "OCTOBER ";break;
case 10: sFirstDayOfWeek += "NOVEMBER ";break;
case 11: sFirstDayOfWeek += "DECEMBER ";break;
}


int iDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
//System.out.println("iDayOfWeek" +iDayOfWeek);

switch(iDayOfWeek){
case 1:
calendar.add(Calendar.DATE,-6);
break;
case 2:
calendar.add(Calendar.DATE,-0);
break;
case 3:
calendar.add(Calendar.DATE,-1);
break;
case 4:
calendar.add(Calendar.DATE,-2);
break;
case 5:
calendar.add(Calendar.DATE,-3);
break;
case 6:
calendar.add(Calendar.DATE,-4);
break;
case 7:
calendar.add(Calendar.DATE,-5);
break;
default :
System.out.println("Invalid Date.");
}

sFirstDayOfWeek += calendar.get(Calendar.DATE)+", "+calendar.get(Calendar.YEAR);
System.out.println("sFirstDayOfWeek is" + sFirstDayOfWeek);
}
}

--------------------------------------------------------------------------------

and the output am getting is "sFirstDayOfWeek isFEBRUARY 28, 2008" But actual output i need is "sFirstDayOfWeek isJanuary 28, 2008".
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
You are calculating your month before you adjust the date. Move your first switch statement to after your second switch statement and i think it will work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i want to get the month of a particular date