• 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

Getting months from date

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to know how to get total number of months between given dates.
Please ,help me..
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll give you a hint Try using API for Calendar and Date. You can start there.
[ October 22, 2008: Message edited by: Vijitha Kumara ]
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neeba Rebbaca ,
Welcome to the Ranch. We are happy to have you here

We encourage everybody to try out things on their own because that is the best way to learn things.

Check out this link. It will give you more information on how to extract the most of the ranch members.
http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch

So why don't you tell us what you have tried out so far and we can take it up from there?
Which part are you stuck at? The approach to solve this problem? The logic? The code?
 
Neeba Rebbaca
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried in this way:


This works well if both the dates fall within a year.If between different year.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You can do it using API. But i feel that it will be easy applying your own logic. For example
parse the given dates and get the startyear,endyear,startmonth,endmonth.
Now
years=endyear-startyear;
months=(years-2)*12; //since start year and end year are not completely included
months=months+(endmonth-1)+(12-startmonth);//since start month and end months are not completely inlcuded.
Correct me if i am wrong
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I agree with the fact that using logic to solve this will be very helpful in developing the programming skills. However, in case you want to try your hands on APIs, then the class java.util.GregorianCalendar will be helpful. refer to the Javadoc and you will know how to use them.

Thanks!
Romit
[ October 24, 2008: Message edited by: Romit Bose ]
 
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
Don't use Date.getMonth - it's deprecated and should not be used. It advices you to use Calendar.get(Calendar.MONTH) instead.

Given a Date object you can create a Calendar object quite easily:
 
expectation is the root of all heartache - shakespeare. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic