Link to home
Start Free TrialLog in
Avatar of raysee
raysee

asked on

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.

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of raysee
raysee

ASKER

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 ?
"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
>>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()));
Avatar of raysee

ASKER

>>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.
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());
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of raysee

ASKER

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.
happy to help :)
:-)

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

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