Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple date format

 
Jordan Smith
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
 
Henry Wong
author
Marshal
Pie
Posts: 20894
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
Marshal
Pie
Posts: 20894
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • 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.
 
Himanshu Kansal
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic