Current location: Hot Scripts Forums » Programming Languages » Everything Java » Urgent converting String to Date


Urgent converting String to Date

Reply
  #1 (permalink)  
Old 01-13-04, 05:30 AM
yymae yymae is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Urgent converting String to Date

Hi,

I'm having a problem here. Suppose a date is in String format from the user selected, like "31/12/2003" (dd/MM/yyyy). I want to format it into date to this format (MM/dd/yyyy). And there will be a time in String format pass in from the form, like "3:00 PM"

Both date and time are separated String field, but when inserting into database I need to combine it into date format, like "12/31/2003 3:00 PM"

How to do it ? Please give some examples on this ASAP.

Thanks.

Cheers,
yymae
Reply With Quote
  #2 (permalink)  
Old 01-16-04, 11:22 AM
stdunbar stdunbar is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Superior, CO, USA
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Take a look at http://java.sun.com/j2se/1.4.2/docs/...ateFormat.html

To parse I would do something like:

String dateAndTime = userDateInput + " " + userTimeInput; // i.e. "31/12/2003 3:00PM"

SimpleDateFormat dateFormatter = new SimpleDateFormat( "dd/MM/yy kk:mm aa");

Date theDate = dateFormatter.parse( dateAndTime, new ParsePosition() );

...

// assume that the prepared statment is setup in some way

...
preparedStatement.setTimestamp( fieldNumber, new java.sql.Timestamp( theDate.getTime() ) );

// use timestamp if you want to remember the time component across multiple
// databases. setDate() truncates the time with some JDBC drivers.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM
Date From Access 2000 Database Kaikki ASP 4 09-25-03 06:04 PM
asp: URGENT! need to change code to create new form per id seala ASP 2 09-09-03 09:54 PM
Error: Syntax error converting datetime from character string. Han84 ASP 1 08-22-03 04:59 AM


All times are GMT -5. The time now is 05:48 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.