It's not a secret anymore!
The moose likes Beginning Java and the fly likes Date function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date function" Watch "Date function" New topic

Date function

mohan kumar

Joined: Apr 16, 2007
Posts: 11
hi all,
check the following code:

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String ss = dateFormat.format(new Date("16/04/2007"));
System.out.println("Result --> "+ss);

Result --> 04/04/2008

Here "new Date("String")" is constructor is deprecated.if this is cause to produce this result,then explain me how it is?

Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
Note that it is the Date constructor that is interpreting the String, not the DateFormat.format() call.
The Javadoc says that when interpreting a string of decimal numbers
If the number is followed by a slash, it is regarded as a month ... unless a month has already been recognized, in which case it is regarded as a day of the month.

So the 16 is being recognised as the month. Presumably (although it doesn't say in the Javadoc), if the month is greater than 12, it subtracts 12 from it until it is less than 12 and adds 1 to the year for each subtraction.

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1710
Hi Natarajan,

For sure it is not due to the reason that Date(String arg) constructor has been deprecated.

It is because of the pattern you pass to the SimpleDateFormat(args) constructor. Pattern is really mind boggling for me!


Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

what you need is the parse method. Example:

prints (depending on your locale)
"raw": Mon Apr 16 00:00:00 CEST 2007
dateFormat: 16/04/2007


(edited, because forgot code tags)
[ April 16, 2007: Message edited by: Burkhard Hassel ]

all events occur in real time
I agree. Here's the link:
subject: Date function
It's not a secret anymore!