Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i want to get the month of a particular date

 
Chandran Madesh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Chandran Madesh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic