aspose file tools
The moose likes Java in General and the fly likes Date need to be advance to next month Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Date need to be advance to next month" Watch "Date need to be advance to next month" New topic
Author

Date need to be advance to next month

SaurabhSri Sri
Ranch Hand

Joined: May 01, 2008
Posts: 43
Hi All,

From a page user enters Start date and End date. A java program returns one month advance of both dates as "Start date || End date". e.g

Suppose, Start Date = 04-06-2008, End Date = 10-06-2008
Then return value = 04-07-2008 || 10-07-2008

Now, a problem comes when I have select first day of month as start date and last day as end date -

Start Date = 01-06-2008, End Date = 30-06-2008
The return value is = 01-07-2008 || 30-07-2008
But it should return, 01-07-2008 || 31-07-2008 because July is having 31 days.
My code is -



Thanks in advance for your help.


Regards
SaurabhSri (SCJP 1.5)
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3268

Hmm, interesting problem, I ran this locally in my debugger and internally the JDK calls a method which corrects for the other way (e.g. It corrects the day if you were going from the 31st of Jan to 28th of Feb) but it doesn't appear to cover your case... (I tried JDK 1.5 and 1.6).

I'm no date and calendar guru so hopefully someone else can enlighten..

Cheers,
Martijn


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

Well, the result that you get sounds logical to me - one month after June 30th is July 30th, isn't it?

You should think carefully about what you want, and then write Java code to do it. Here is a question to get you thinking: Suppose the user enters 29-06-2008. What should the result be; 29-07-2008? And if the user enters 30-06-2008, you want the result to be 31-07-2008? So the result can never be 30-07-2008?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3268

Originally posted by Jesper Young:
Well, the result that you get sounds logical to me - one month after June 30th is July 30th, isn't it?

You should think carefully about what you want, and then write Java code to do it. Here is a question to get you thinking: Suppose the user enters 29-06-2008. What should the result be; 29-07-2008? And if the user enters 30-06-2008, you want the result to be 31-07-2008? So the result can never be 30-07-2008?


Yeah I was just thinking about this on the way back from Lunch, 'of course' the API wouldn't just bump you up to the last day of the month, otherwise you'd never be able get 30-07-2008 <smacks head>.

@Original poster: So as Jesper says you'll have to treat the last day of the month as a special case (if that's what you're truly intending).
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Date need to be advance to next month
 
Similar Threads
Date Confusion
SimpledateFormat class issue.
Date Function
Get Dates between the given dates.
Calculating no. of working days between tow dates