• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

why Date value is changing

Hello,

I want to set my java.util.Date date variable with 31.12.9999.

but I saw in the documentation that the year could not be more than 8901 or so (I don't know exactly.) Then I thought to set my date with 31.12.9000 and did the following.

Calendar c = Calendar.getInstance();    
 c.clear();
   c.set(Calendar.YEAR, 9000);
   c.set(Calendar.MONTH, 12);
   c.set(Calendar.DATE, 31);  
   
    java.util.Date date = c.getTime();             <------   but in this line if I watch, it is showing the date as  --> "Sat Jan 31 00:00:00 CET 9001"

The date is changed to one month after than what I gave. why it happend ?

and I need the axact format 31.12.9999 in my date (java.util.Date ) variable.

Thank you.

0
raysee
Asked:
raysee
  • 4
  • 3
  • 3
2 Solutions
 
CEHJCommented:
Months are numbered 0-11
0
 
rayseeAuthor Commented:
oops, Sorry CEHJ, ya I knew and forgot , Thanks a lot for reminding ...

and could you please tell me, how to get the exact format I need ?
0
 
objectsCommented:
"The first month of the year is JANUARY which is 0; the last depends on the number of months in a year."

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html#MONTH
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
CEHJCommented:
>>and could you please tell me, how to get the exact format I need ?

You need to use a DateFormat to print the date

DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
System.out.println(df.format(cal.getTime()));
0
 
rayseeAuthor Commented:
>>df.format(cal.getTime())

gives a string or a java.util.Date ?

because I don't need to print it, but I have to format the value in my java.util.Date date variable so that I can send it to other layers of my application. for example I have to send that Date object with that specific value and format to the stored procedured so that I can get the required result.

so that is the requirement.

please suggest. thank you.
0
 
CEHJCommented:
Date itself has no format. It's just a number. Only strings have formats. If you need to use it in an sp:


ps.setDate(1, new java.sql.Timestamp(date.getTime());
0
 
objectsCommented:
>  for example I have to send that Date object with that specific value and format to the stored procedured so that I can get the required result.

What data type is the sp expecting?
If its a string them you'll need to convert it to a string
if its a date then use java.sql.Date
0
 
rayseeAuthor Commented:
Thanks to you both.

my problem is solved. you are right the date has no format. and I don't need to do that.

Thanks again.
0
 
objectsCommented:
happy to help :)
0
 
CEHJCommented:
:-)

You'll find the following approach to be less error-prone

c.set(Calendar.MONTH, Calendar.DECEMBER);
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now