This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths 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 Head First Android this week in the Android 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
Author

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, lukas1596@gmail.com
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
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
Sheriff

Joined: Oct 27, 2005
Posts: 20049
    
  30

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date and Datetime creation from a String.
 
It's not a secret anymore!