• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setTime() function not setting the time

 
Nipun Bahr
Ranch Hand
Posts: 55
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see anything wrong with that. What makes you think there is something incorrect happening?
 
Nipun Bahr
Ranch Hand
Posts: 55
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does System.out.println(currentDate); print ?
 
Nipun Bahr
Ranch Hand
Posts: 55
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does give June 1 and June 26. Month numbers in the Calendar class run from 0 to 11.
 
Nipun Bahr
Ranch Hand
Posts: 55
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Paul for clearing the doubt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic