• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

simple date format

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I have this code:

i get an exception:
java.text.ParseException: Unparseable date: "Mon Jul 13 15:15:23 IDT 2009"

why is it?
Thanks
 
author
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

odelya yomtov-glick wrote:
why is it?




Well, you specified the format as... "dd/MM/yyyy".

And you sent it ""Mon Jul 13 15:15:23 IDT 2009" for parsing... Does the date string match the format?

Henry
 
Jordan Smith
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
i want my date to return as dd/MM/yyyy and when i call :
new Date()
i get it in Mon Jul 13 15:15:23 IDT 2009 format.
how can i create a new date that will be in dd/MM/yyyy format?
 
Henry Wong
author
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

odelya yomtov-glick wrote:
i want my date to return as dd/MM/yyyy and when i call :
new Date()



That is not what the class does. The simple date format class helps formats the string when it is convert to and from a string. It doesn't configure a format of a date class.

odelya yomtov-glick wrote:how can i create a new date that will be in dd/MM/yyyy format?



A date class doesn't have a concept of format, hence, it can't be done. You need to use the simple date format, to format it to a string so you can print it, instead of printing the date itself.

Henry
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want to do, format or parse a date? It seems like you might be confusing the two. Formatting means converting a Date object to a string. Parsing is the opposite: converting a string into a Date object.

A Date object represents a moment in time. It doesn't have any format by itself. To display a Date object, you must convert it to text (a string). For this, you use a DateFormat object. Class SimpleDateFormat is a subclass of DateFormat that allows you to specify how you want the Date object to be converted to a String.

If you want it with the format "dd/MM/yyyy", you can do this:


If you print a Date object directly, like this:


Then what happens is that toString() is called on the Date object, which formats the date with a default format string, so that you get something like "Mon Jul 13 15:15:23 IDT 2009".

Make sure that you understand what a Date object is exactly and what formatting and parsing mean.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me it seems that you are trying to format and then parse the Date. Then why format in the first place?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic