• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date function

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic