This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Date and Datetime creation from a String. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date and Datetime creation from a String." Watch "Date and Datetime creation from a String." New topic

Date and Datetime creation from a String.

Lucas Franceschi
Ranch Hand

Joined: Nov 10, 2008
Posts: 106

hello fellows.
I hope you are all in good mood today!
because I need some help from you guys.

thats the problem:
I have (coming from an XML file) this String "2008-06-06 10:23:27.14" and this string: "2008-06-06" right?

so, I need to create Datatime and Data objects from this string.

I tryed to split the string, having 6 Strings, representing year, month, day, hour, minutes, seconds and nanos, and then use the constructor for timestamp using all this parameters.
beside, this constructor got deprecated, and I haven't got correct results (confused date and time).

Same happened with the date string.

So, what do you fellows say?

Lucas Franceschi
Software Developer for SGI Sistemas,
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

The only Timestamp I found that has 6 parameters is the javax.sql.Timestamp, since you aren't using SQL I would guess that isn't the best tool to be using.

In fact, what you have is a Date (as in java.util.Date), and in one case you have Time values and in the other you don't.

My suggestion would be to use java.text.DateFormat to parse the Strings. If the format presented from the XML doesn't fit into one of pre-defined DateFormats, then you may need to use java.text.SimpleDateFormat to create your own format String.

Lucas Franceschi
Ranch Hand

Joined: Nov 10, 2008
Posts: 106

well, this class is dealing with database, so i am using java.sql.datetime... any changes?
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

You can create any SQL date type from a java.util.Date:

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Date and Datetime creation from a String.
It's not a secret anymore!