Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date function

 
mohan kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
check the following code:

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

Result:
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?

Thanks
Mohan
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!



Regards,
cmbhatt
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



Yours,
Bu.

(edited, because forgot code tags)
[ April 16, 2007: Message edited by: Burkhard Hassel ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic