wood burning stoves 2.0*
The moose likes JDBC and the fly likes setTime() function not setting the time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "setTime() function not setting the time " Watch "setTime() function not setting the time " New topic
Author

setTime() function not setting the time

Nipun Bahr
Ranch Hand

Joined: Jul 09, 2008
Posts: 55


Hi All,

I am using below code snippet, but setTime() function is not setting up the time correctly.
currentDate = new Date();

Calendar startCal = Calendar.getInstance();
Calendar endCal = Calendar.getInstance();

startCal.setTime(currentDate);
startCal.add(Calendar.DATE,12);
endCal.setTime(currentDate);
endCal.add(Calendar.DATE,37);
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

I don't see anything wrong with that. What makes you think there is something incorrect happening?
Nipun Bahr
Ranch Hand

Joined: Jul 09, 2008
Posts: 55


Hi Paul,

If I am printing startCal and endCal values after incrementing, its giving me April 30 and may 24 respectively-

Date currentDate = new Date();

Calendar startCal = Calendar.getInstance();
Calendar endCal = Calendar.getInstance();

startCal.setTime(currentDate);
startCal.add(Calendar.DATE,12);
System.out.println(startCal);
endCal.setTime(currentDate);
endCal.add(Calendar.DATE,37);
System.out.println(endCal);
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

And what's wrong with that?

Before you answer, please spend a little time preparing an explanation of why you think there is a problem. Show us the inputs, the outputs, and tell us what you think the output should be and why. Also show us the actual outputs instead of your interpretation of the outputs.
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

what does System.out.println(currentDate); print ?


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Nipun Bahr
Ranch Hand

Joined: Jul 09, 2008
Posts: 55

Nipun Bahr wrote:
Hi Paul,

If I am printing startCal and endCal values after incrementing, its giving me April 30 and may 24 respectively-

Date currentDate = new Date();

Calendar startCal = Calendar.getInstance();
Calendar endCal = Calendar.getInstance();

startCal.setTime(currentDate);
startCal.add(Calendar.DATE,12);
System.out.println(startCal);
endCal.setTime(currentDate);
endCal.add(Calendar.DATE,37);
System.out.println(endCal);



Hi All,

First of all sorry for late reply

When I run the above code snippet, I got the output as-

java.util.GregorianCalendar[time=1306951576296,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+05:30",offset=19800000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=5,WEEK_OF_YEAR=23,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=152,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=36,SECOND=16,MILLISECOND=296,ZONE_OFFSET=19800000,DST_OFFSET=0]

java.util.GregorianCalendar[time=1309111576296,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+05:30",offset=19800000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=5,WEEK_OF_YEAR=27,WEEK_OF_MONTH=5,DAY_OF_MONTH=26,DAY_OF_YEAR=177,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=36,SECOND=16,MILLISECOND=296,ZONE_OFFSET=19800000,DST_OFFSET=0

If I look inside this output it gives me Day of the month=1, month=5
and Day of the month=26, month=5 respectively

which does not fit according to today's date i.e may 20th.
How come its correct??

should not it give 1 June and 26 June respectively considering today's date as 20 May??
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

It does give June 1 and June 26. Month numbers in the Calendar class run from 0 to 11.
Nipun Bahr
Ranch Hand

Joined: Jul 09, 2008
Posts: 55


Thanks Paul for clearing the doubt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setTime() function not setting the time