Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Error in Date FAQ example "Parsing a string into a date"  RSS feed

 
Paula Berinstein
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just thought I'd let you know that the above example doesn't work as advertised. Output doesn't yield the correct time of day; there is no such time as 5:60; and d1 is used in all the examples, which means the second two date/time strings are never used.

Just so you don't have to go looking for the code, here it is. You can find it directly at http://faq.javaranch.com/java/JavaDatesFaq.

Parsing a string into a date

Parsing a String to a Date can be done with the DateFormat class:
DateFormat dflong = DateFormat.getDateInstance(DateFormat.LONG);
DateFormat tflong = DateFormat.getTimeInstance(DateFormat.LONG);

DateFormat dfshort = DateFormat.getDateInstance(DateFormat.SHORT);
DateFormat tfshort = DateFormat.getTimeInstance(DateFormat.SHORT);

DateFormat dfmedium = DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat tfmedium = DateFormat.getTimeInstance(DateFormat.MEDIUM);

Date d1 = dflong.parse("January 12, 2005 5:60 PM PDT");
Date d2 = dfshort.parse("5/2/05 9:34 AM");
Date d3 = dfmedium.parse("Jul 12, 2005");

String s1 = dflong.format(d1);
String s2 = tflong.format(d1);
System.out.printf("Long Date is %s and time is %s \n",s1,s2);

s1 = dfshort.format(d1);
s2 = tfshort.format(d1);
System.out.printf("Short Date is %s and time is %s \n",s1,s2);

s1 = dfmedium.format(d1);
s2 = tfmedium.format(d1);
System.out.printf("Medium Date is %s and time is %s \n",s1,s2);
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Thanks for letting us know; I've changed the page accordingly.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paula,

Thanks for pointing this out. That's a user-editable page in our Wiki, meaning that the code isn't an "official" JavaRanch example, but rather it's just a bit of code posted by a member that they felt would be useful. Since the page can be edited by anyone, you can -- and should! -- jump right in and fix it to your liking. Press the "Edit" link near the top of the page, and have at it!
 
Paula Berinstein
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! Thanks for letting me know about the wiki. I didn't realize the example was editable. Does that mean all the examples in the FAQs are submitted by members?

Just by way of introduction, let me say that I am a rank beginner. I was a COBOL programmer 20 years ago and am finding Java extremely challenging. However, I was able to figure out that the example didn't work, so I think I'm making progress.

Thanks for your quick reply!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, all content is from members, although I think it's fair to say that a lot of the members were also moderators at the time :-) But by no means all, and we welcome contributions from all. In fact, one of the reasons we moved the CodeBarn to the wiki is so that it would be easier for people to contribute.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!