Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issues with fetching Year and Month from the Calender

 
shivashankarHg shankar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

I want the year and Month from the following date format.

I am obtaining the DATE from spinner, which is of following format
"MMM/YYYY" from client side swings.

i want to retrieve the Month ,

Here is the code

public getMonth(String monthYearSelected, int weekSelected)
{
this.monthYearSelected = monthYearSelected;
Calendar nederlandCal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
try {
System.out.println("Spinner Date :"monthYearSelected);
Date date = format.parse(monthYearSelected);
System.out.println("Parsed Date :" date);
nederlandCal.setTimeInMillis(date.getTime());
System.out.println("Month is:"(nederlandCal.get(Calendar.MONTH)+1));
} catch (ParseException e1) {
e1.printStackTrace();
}
}


output:
Spinner Date :Mon Jun 01 00:00:00 GMT+05:30 2009
Parsed Date :Mon Jun 01 00:00:00 GMT+05:30 2009
Month is:5

But Ideally, I should get 6(For June),


I have a similar kind of issues for Year too


Calendar nederlandCal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
Calendar previous = new GregorianCalendar(TimeZone.getTimeZone("Netherland"));
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
try {
System.out.println("Spinner Date :"+yearSelected);
Date date = format.parse(yearSelected);
System.out.println("Parsed Date :" date);
//nederlandCal.setTime(date);
nederlandCal.setTimeInMillis(date.getTime());
System.out.println("Year is :"+nederlandCal.get(Calendar.YEAR));
} catch (ParseException e1) {
e1.printStackTrace();
}

output:
Spinner Date :Tue Jan 01 00:00:00 GMT+05:30 2008
Parsed Date :Tue Jan 01 00:00:00 GMT+05:30 2008
Year is :2007

But, I should get 2008

I have tried both setTime() and setTimeInMillis(), but none is giving me the proper results

Am i wrong any where....?... Can anybody give me better idea

Kindly help

Regards
Shiva

 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
5 = June is correct.
You set the TimeZone as GMT, so when you have the time as midnight 1st January in +5.30, it is still December in GMT. So 2007 is correct.

By the way: they don't use GMT in the Netherlands; they use GMT +1 hour.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Calendar class in Java (unfortunately) counts months from 0 to 11, so 0 = January, 1 = February, ..., 5 = June, ..., 11 = December.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compare to Calendar.JANUARY to Calendar.DECEMBER instead of 1 to 12.
 
shivashankarHg shankar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All, Thanks for the reply....

Regards
Shiva

 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

And welcome to JavaRanch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic