• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in Incrementing Calendar Date ?  RSS feed

 
Mallik Avula
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI friends
my context is:
user will give some dates example 20/05/2008 to 04/07/2008 (dd/mm/yyyy).
i need to insert every date into database between those dates.
my code is:
st = new StringTokenizer("05/20/2008","/");
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.MONTH, Integer.parseInt(st.nextToken())); c1.set(Calendar.DATE, Integer.parseInt(st.nextToken()));
c1.set(Calendar.YEAR, Integer.parseInt(st.nextToken()));

st = new StringTokenizer("04/07/2008","/");
Calendar c2 = Calendar.getInstance();
c2.set(Calendar.MONTH, Integer.parseInt(st.nextToken())); c2.set(Calendar.DATE, Integer.parseInt(st.nextToken()));
c2.set(Calendar.YEAR, Integer.parseInt(st.nextToken()));

while(c2.equals(c2))
{
c1.add(Calendar.MONTH, -1); // because java month start from 0 to 11
c1.add(Calendar.DATE,1)
c1.add(Calendar.MONTH, 1); // oracle month start from 1 to 12
}

05/30/2008 is repeating?
why this problem is?
(i need to insert the date in database with month 1-12 )
Any help please.
thanks in advance.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You understand that c2.equals(c2) will be true ALWAYS? It is required by the Object.equals contract. That's why you get an infinite loop.


Why subtract one month, then add it again? All you need is adding single dates.

Let's assume that s1 and s2 are the date strings in the right format and s1 is before s2:



I also found out why it is staying stuck at that date, although it is June, not May, after adding again.

On June 30th, you subtract one month. It becomes May 30th.
You add one day. It becomes May 31st.
You add one month. Because June 31st does not exist, it is corrected to be June 30th.
And you start over again.

You don't need to add and subtract the month in Java - you just need that once you add it in Oracle. Also, try to see if you can use PreparedStatement to add the record. It has a method called setDate which takes a single java.sql.Date object, and you can use it as follows:



Final note: you do understand that c1 is AFTER c2, right? In your code, c1 is June 20th (since months are 0 based, month 5 is June) and c2 is May 7th. If you keep adding to c1, you will never stop.
[ May 12, 2008: Message edited by: Rob Prime ]
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect the comments about "Java date" and "Oracle date" represent a fundamental misunderstanding about how Dates and Calendars work in Java (and Oracle). I believe that using a PreparedStatement along with the setDate() or setTimestamp() method will make the code with those comments unnecessary.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!