Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By wsaryada

Thread: How to yyyy/mm/dd into dd/mm/yyyy from MySQL to Java fromate

  1. #1
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    254
    Rep Power
    3

    Question How to yyyy/mm/dd into dd/mm/yyyy from MySQL to Java fromate

    Nice to Have your link Genius!

    In my project i am facing an problem,

    The My SQL Data base will accept the date format of yyyy/mm/dd only as "Date" data type
    but in my program i wants to use dd/mm/yyyy format. (i have this same format now)
    that's why i unable to insert / retrieve it..,
    anyone can help me..,
    Thank you..,

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: How to yyyy/mm/dd into dd/mm/yyyy from MySQL to Java fromate

    How do you pass the date to your MySQL query, using a string? Instead of worrying about the date format you can pass the date as Date object if your are using PreparedStatement.
    Tolls likes this.

  3. #3
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    254
    Rep Power
    3

    Default Re: How to yyyy/mm/dd into dd/mm/yyyy from MySQL to Java fromate

    sorry for the late replay sir, now only the internet is get connected..,
    i will try as you said now..,
    Thank you sir..,

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: How to yyyy/mm/dd into dd/mm/yyyy from MySQL to Java fromate

    Expanding on wsaryada's post, MySQL DATE columns accept Dates.
    They can accept Strings, but internally they are converted to a DATE so it is generally preferable to use a Date in the first place.

    As for the two formats you mention above, you are confusing the data with how that data is displayed.
    Your formatted date should be converted to a Date object via SimpleDateFormat in order to get your Date, and when you want to display your Date to someone you would use a SimpleDateFormat to convert it into a String.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    254
    Rep Power
    3

    Default Re: How to yyyy/mm/dd into dd/mm/yyyy from MySQL to Java fromate

    I try As you said it is working well

    Java Code:
    String n="100";
    try {
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/payslip", "root", "root");
                    PreparedStatement ps1 = conn.prepareStatement("select DateOfBirth from newemp where EmployeeID= ?");
                    ps1.setString(1,n);
                    ResultSet rs=ps1.executeQuery();
                    if(rs.next())
                    {
                    Date dat=rs.getDate(1);
                    DateFormat df=new SimpleDateFormat("dd/MM/yyyy");
                    String h=df.format(dat);
                    JOptionPane.showMessageDialog(rootPane, h);
                    }
                   
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(rootPane, e);
        }

    Thank you sir..,

  6. #6
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    254
    Rep Power
    3

    Default How to dd/mm/yyyy into yyyy/mm/dd from java to MySQL fromate

    Thank for Your Help sir
    & Now
    i wants to invert format sir!
    like dd/mm/yyyy into yyyy/mm/dd (dd/mm/yyyy into yyyy/mm/dd from java to MySQL fromate)

    try with the fallowing coding

    Java Code:
     try
        {
        String sd=dtpick.getText();  //(dd/MM/yyyy) format
        Date de=Date.valueOf(sd);
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        java.sql.Date dt = java.sql.Date.valueOf(df.format(de));
        JOptionPane.showMessageDialog(rootPane, dt);
        }
        catch(Exception e)
        {
             JOptionPane.showMessageDialog(rootPane, e);
        }

    But it showing an Exception

    Java Code:
    java.lang.illegalArgumentException

    what mistaken i did with this code.
    Can You please Help me For Correct my Mistake..,
    Thank you..,

  7. #7
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    254
    Rep Power
    3

    Question Re: How to yyyy/mm/dd into dd/mm/yyyy from MySQL to Java fromate

    i try with the fallowing code too

    Java Code:
    try {
                String s=dtpick.getText();
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
                Date d=(Date) df.parse(s);
                String r = df.format(d);
                JOptionPane.showMessageDialog(rootPane, r);
            } catch (ParseException ex) {
                Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
    it is also saying the same exception..,

    Any one guide me please..,,
    i am waiting for you response sir,
    Thank you..,

  8. #8
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    254
    Rep Power
    3

    Default Re: How to yyyy/mm/dd into dd/mm/yyyy from MySQL to Java fromate

    Anyone can Guide me..,

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: How to yyyy/mm/dd into dd/mm/yyyy from MySQL to Java fromate

    Print the stack trace of the exception (ex.printStackTrace()) so we know exactly where it is being thrown from.
    Or provide us withthe stack trace that should be in your log (looking at your second example).

    Not sure what you're trying to do on the second one as you are simply converting the date string to a date and then back to a string in the same format.

    In your first example (post #6) valueOf expects a "yyyy-mm-dd" format, yet your comment suggests it is in "dd/MM/yyyy" format.

    In any case, I don't understand why you want to convert that format?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    254
    Rep Power
    3

    Default Re: How to yyyy/mm/dd into dd/mm/yyyy from MySQL to Java fromate

    Thank you for your response sir!

    Sir!
    in India users Mostly using the "dd/MM/yyyy" format

    In my project when user can select the date by my date picker (as dd/MM/yyyy), have to insert into database (Example; DOB) from dd/MM/yyyy to yyyy/MM/dd format
    &
    if user can retrieve the information have to shown in dd/MM/yyyy format for user friendly only (Example; DOB) from database yyyy/MM/dd to dd/MM/yyyy format

    Can you guide me sir!

    Now i wants to convert "dd/MM/yyyy" into "yyyy/MM/dd" format please..,

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How to yyyy/mm/dd into dd/mm/yyyy from MySQL to Java fromate

    Quote Originally Posted by raj.mscking@gmail.com View Post
    in India users Mostly using the "dd/MM/yyyy" format

    In my project when user can select the date by my date picker (as dd/MM/yyyy), have to insert into database (Example; DOB) from dd/MM/yyyy to yyyy/MM/dd format
    &
    if user can retrieve the information have to shown in dd/MM/yyyy format for user friendly only (Example; DOB) from database yyyy/MM/dd to dd/MM/yyyy format

    Can you guide me sir!

    Now i wants to convert "dd/MM/yyyy" into "yyyy/MM/dd" format please..,
    Quote Originally Posted by Tolls View Post
    you are confusing the data with how that data is displayed.
    You're still confusing that. Dates don't have formats. A Date may be formatted to obtain a String representation.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: How to yyyy/mm/dd into dd/mm/yyyy from MySQL to Java fromate

    What Darryl says.
    Your database (assuming the column is a DATE column) does not have a format for the date (as far as you are concerned).
    Just turn your String into a java.sql.Date and use that.
    In addition a Java Date object does not have a date format. It's just a 'long' that represents a point in time. When you display it you give it whatever format you want.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    254
    Rep Power
    3

    Default Re: How to yyyy/mm/dd into dd/mm/yyyy from MySQL to Java fromate

    ok sir!
    i will try now as you said,
    Thank you..,

  14. #14
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    254
    Rep Power
    3

    Default Re: How to yyyy/mm/dd into dd/mm/yyyy from MySQL to Java fromate

    I got it sir!
    it is working well as you said,
    thank you sir..,

Similar Threads

  1. Replies: 4
    Last Post: 03-04-2012, 11:26 PM
  2. problem to convert MM/dd to yyyy-MM-dd
    By newbiejava in forum New To Java
    Replies: 6
    Last Post: 08-31-2010, 09:39 AM
  3. I wish to split a string xxx/yyyy/zzz so that ...
    By nmvictor in forum New To Java
    Replies: 10
    Last Post: 10-23-2009, 09:47 AM
  4. Java and MySQL
    By Abder-Rahman in forum JDBC
    Replies: 6
    Last Post: 04-21-2009, 09:55 AM
  5. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 01:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •