This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Convert to timestamp yyyy-MM-dd hh:mm:ss a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert to timestamp yyyy-MM-dd hh:mm:ss a" Watch "Convert to timestamp yyyy-MM-dd hh:mm:ss a" New topic
Author

Convert to timestamp yyyy-MM-dd hh:mm:ss a

jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
Hi;
I have a jsp and passing start_date as a string with this format
input string 2006-01-02 03:04:00
database timestamp: 2006-01-02 03:04:00

in the database, the java method expected Date '2006-01-02 03:04:00' as parameter . In the SQL it search like "select * from table where date1 >= '2006-01-01 12:13:00' and date2 <= '2007-01-02 12:13:00'"

Here what I do:


Many thanks for your help. Regards
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Your input string does not have the am/pm marker (aa) that your date format has.
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
Thanks Carol: i fixed it,
but for example on my jsp i enter 2007-01-02 15:00:00
i ran through my code [see previous post], the output string is
<b>Tue Jan 02 15:00:00 </b> not the format <b> "yyyy-MM-dd hh:mm:ss" </b> as I wanted? Any ideas how to fix this
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
Can someone please help,
this is what I have so far,
input date from jsp: 2007-02-03 09:00:00 as a string
I need to convert this to a java.util.Date format yyyy-MM-dd hh:mm:ss


Greatly appreciated if you can help thanks much
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Sorry, but I'm not following your problem.

You've got a String representing a date. (inputstring)
You turn it into a Date. (d1)
You turn it back into the String you started with. (mydt)
Now you cannot turn it back into a date? (what is wrong with d1?)
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
Carol: sorry for the confusing:
Let me try again:

On the jsp: there are two seperate field: Date, and Hour, User can enter date with format yyyy-MM-dd and Hour hh:mm:ss

When I got this two fields, i need to somehow reformat to be the whole thing yyyy-MM-dd hh:mm:ss,

I try to concate the string, but it won't work



So from the above code , you are saying that I just using d1 to passing into the method that epxect Date
private String getSomething (Date myDate) // passing d1 in here, but I got stuck at the hour,minute,seconde.

any ideas or hints. Greatly appreciated THNkS
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Try printing out your intermediate strings or use a debugger to look at what you are doing.

It looks like you are missing the space between date and time components when you concatenate.
Aditya Kumar
Ranch Hand

Joined: Oct 31, 2006
Posts: 56
Instead of modifying the Java code why dont you use the to_date/to_char functions in SQL? Its much simpler that way.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Aditya Kumar:
Instead of modifying the Java code why dont you use the to_date/to_char functions in SQL? Its much simpler that way.


...unless of course you change databases to one that does not support these functions (since they are not standard SQL).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Aditya Kumar
Ranch Hand

Joined: Oct 31, 2006
Posts: 56
Thanks, Paul

I hope I was not completely wrong there.
sarah Marsh
Ranch Hand

Joined: Mar 06, 2001
Posts: 282
correct the error line to:
java.util.Date d1 = sdf.parse(inputdate + " " + inputhr);
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
Thank you for everyone input. regards,i will try it out
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert to timestamp yyyy-MM-dd hh:mm:ss a
 
Similar Threads
Time Difference
change data type from string to timestamp
Time Difference
Comparing two date inputs
Reformating date