wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Issues with fetching Year and Month from the Calender Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Issues with fetching Year and Month from the Calender" Watch "Issues with fetching Year and Month from the Calender" New topic
Author

Issues with fetching Year and Month from the Calender

shivashankarHg shankar
Greenhorn

Joined: Jun 26, 2009
Posts: 3
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

Joined: Oct 13, 2005
Posts: 38057
    
  22
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

The Calendar class in Java (unfortunately) counts months from 0 to 11, so 0 = January, 1 = February, ..., 5 = June, ..., 11 = December.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Compare to Calendar.JANUARY to Calendar.DECEMBER instead of 1 to 12.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
shivashankarHg shankar
Greenhorn

Joined: Jun 26, 2009
Posts: 3
Dear All, Thanks for the reply....

Regards
Shiva

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
You're welcome

And welcome to JavaRanch
 
jQuery in Action, 2nd edition
 
subject: Issues with fetching Year and Month from the Calender
 
Similar Threads
String to Date...
Date 01/01/1970 returning non-zero value for time
Puzzling Parsing of Dates
Date Conversion
Convert String value to Date Object