• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issues with fetching Year and Month from the Calender

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All, Thanks for the reply....

Regards
Shiva

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

And welcome to JavaRanch
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic