| Author |
simple date format
|
Jordan Smith
Ranch Hand
Joined: Apr 06, 2008
Posts: 89
|
|
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
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Jordan Smith
Ranch Hand
Joined: Apr 06, 2008
Posts: 89
|
|
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
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Himanshu Kansal
Ranch Hand
Joined: Jul 05, 2009
Posts: 257
|
|
|
To me it seems that you are trying to format and then parse the Date. Then why format in the first place?
|
Experience and talent are independent of age
|
 |
 |
|
|
subject: simple date format
|
|
|