DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    juanma Guest

    String to Date conversion


    Hi everybody,

    I'm trying to convert a String to sql date value to update a database.
    I'm new on Java and i cannot solve the following problem:

    A user has an HTML form where he introduce a date value in an "input
    type=text" cell.
    when he submit the form i receive the request and must convert the string
    value into a date value.

    How can do that? I think solution is around DateFormat class...

    Thanks in advance,


    Juanma



  2. #2
    Ruchi Dhar Guest

    Re: String to Date conversion


    I am assuming that your text field in the HTML is having MM/dd/yyyy format.

    import java.text.*;
    import java.util.*;
    import java.sql.*;

    public class DateConversion
    {
    public java.sql.Date getDate(String date)
    {
    //The format of the input date string
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    java.util.Date dd = sdf.parse(date);
    //Oracle Date is in yyyy-MM-dd.
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    String jdbcDate = sdf1.format(dd);
    return java.sql.Date.valueOf(jdbcDate);
    }

    public java.sql.TimeStamp getTime(String dateTime)
    {
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
    Date dd = sdf.parse(dateTime);
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.fffffffff");
    String jdbcTime = sdf1.format(dd);
    return java.sql.TimeStamp.valueOf(jdbcTime);
    }
    }


    "juanma" <jmherreros@unisoft-x.com> wrote:
    >
    >Hi everybody,
    >
    > I'm trying to convert a String to sql date value to update a database.
    >I'm new on Java and i cannot solve the following problem:
    >
    > A user has an HTML form where he introduce a date value in an "input
    >type=text" cell.
    > when he submit the form i receive the request and must convert the string
    >value into a date value.
    >
    > How can do that? I think solution is around DateFormat class...
    >
    > Thanks in advance,
    >
    >
    > Juanma
    >
    >



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links