Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    kavithav is offline Member
    Join Date
    Apr 2017
    Posts
    26
    Rep Power
    0

    Default How to convert the date in string format to mm/dd/yyyy format

    XML Code:
    String FromDate=""; 
    FromDate=Value;
    i am declaring a string variable as fromdate when seen while debugging i am able to find this date format in that fromdate variable FromDate = (java.lang.String) "Tue May 02 00:00:00 IST 2017" i need to convert this date to mm/dd/yyyy format and pass to my insert query how can i do this i tried many codes that i found but none of them worked for me and gives the same output as VisitDate = (java.lang.String) "Tue May 02 00:00:00 IST 2017"

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    First off, what database datatype is the column this value is going into?
    If it's a DATE or similar then you only need to convert it to a java.sql.Date.
    If it isn't a DATE column then I would question why you are putting dates into character columns.

    The class you need either way is SimpleDateFormat.
    Note that this is not a threadsafe class.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    kavithav is offline Member
    Join Date
    Apr 2017
    Posts
    26
    Rep Power
    0

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    FromDate is of type DateTime in my sql server database

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    OK, so all you need to do is convert that String to a Date using SimpleDateFormat:

    Java Code:
    SimpleDateFormat sdf = new SimpleDateFormat(... sort out the date format ...);
    java.sql.Date d = new java.sql.Date(sdf.parse(fromDate).toTime());
    then use that date object in your INSERT prepared statement, using setDate rather than setString.

    Edit: Actually, you might want to use java.sql.Timestamp, so it keeps it's time part.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    kavithav is offline Member
    Join Date
    Apr 2017
    Posts
    26
    Rep Power
    0

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    could you please post the complete code because it is now also not getting converted to mm/dd/yyyy format

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    kavithav is offline Member
    Join Date
    Apr 2017
    Posts
    26
    Rep Power
    0

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    is there any rule that we should post only in one forum you also cross check the questions that are posted in other forums please remember a user posts the question if he is not getting the solution once if we get the solution to our problem we wont post unnecessarily right

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    Please read this: The problems with cross-posting

    You should post links to the other forums where you have posted the same question.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    The only rule about cross-posting is to inform the sites of where else you have posted to, so we can see if it's already been answered.

    Can you post the code you are using, including the database INSERT call?
    We don't provide full solutions here, but we will help fix your code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    kavithav is offline Member
    Join Date
    Apr 2017
    Posts
    26
    Rep Power
    0

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    That is what i am saying i am unable to fix that problem with your code also

    Below is my code
    [code]
    for(int i = 2; i < RowCount; i++)

    {
    for(int j=1;j< ColCount;j++)
    {
    cell = sheet.getCellAt(j, i);
    Value= cell.getValue().toString();
    if(j==1)
    {
    Name=Value;
    }
    else if(j==2)
    {
    Firstname=Value;
    }
    else if(j==3)
    {
    Lastname=Value;
    }
    else if(j==4)
    {
    Fromdate=Value;
    SimpleDateFormat sdf = new SimpleDateFormat("MM-DD-YYYY hh:mm:ss");
    java.sql.Date d = new java.sql.Date(sdf.parse(Fromdate).getTime());
    }
    int k=st.executeUpdate("Insert into Users(Name,Firstname,Lastname,Fromdate)values('"+N ame+"','"+Firstname+"','"+Lastname+"','"+Fromdate+ "')");

    System.out.println("Data is inserted");
    }
    }
    [code]

    Above is my complete code but still i am unable to get the date in mm/dd/yyyy format
    Last edited by kavithav; 07-21-2017 at 03:03 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    Java Code:
    int k=st.executeUpdate("Insert into Users(Name,Firstname,Lastname,Fromdate)values('"+N ame+"','"+Firstname+"','"+Lastname+"','"+Fromdate+ "')");
    This needs to be a PreparedStatement.
    (Tutorial).

    You don't want to be concatenating a query like that together. It's prone to SQL injection, it can't handle apostrophes in names (eg O'Neill), and it doesn't handle dates.

    And after you have that in place, you need to be able to pass the converted Date object into the PreparedStatement, which means the variable ('d', though I would give t a better name) needs to be accessible outside of that else block.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    kavithav is offline Member
    Join Date
    Apr 2017
    Posts
    26
    Rep Power
    0

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    can you please post the complete code with where i had done the mistake because i am unable to rectify my mistake as i am new to this

    That is what i am saying i am unable to fix that problem with your code also

    Below is my code
    Java Code:
                        for(int i = 2; i < RowCount; i++)
    
                         {
                             for(int j=1;j< ColCount;j++)
                             {
                              cell = sheet.getCellAt(j, i);
                              Value=  cell.getValue().toString();
                               if(j==1)
                               {
                                Name=Value;
                               }
                               else if(j==2)
                               {
                                 Firstname=Value;
                               }
                               else if(j==3)
                               {
                                   Lastname=Value;
                               }
                               else if(j==4)
                               {
                                  Fromdate=Value;
                                  SimpleDateFormat sdf = new SimpleDateFormat("MM-DD-YYYY hh:mm:ss");
                                  java.sql.Date d = new java.sql.Date(sdf.parse(Fromdate).getTime());
                               }
                             int k=st.executeUpdate("Insert into Users(Name,Firstname,Lastname,Fromdate)values('"+Name+"','"+Firstname+"','"+Lastname+"','"+Fromdate+"')");
            
    	                 System.out.println("Data is inserted");
                            }
                        }
    Above is my complete code but still i am unable to get the date in mm/dd/yyyy format
    Last edited by Norm; 07-21-2017 at 03:19 PM. Reason: Added / to end code tag

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    The ending code tag needs a /. See post#11
    I fixed it this time.

    I don't think you need the j loop:
    Java Code:
          name =  sheet.getCellAt(1, i).getValue().toString();
          firstName =  sheet.getCellAt(2, i).getValue().toString();
    etc for rest of j values
    Last edited by Norm; 07-21-2017 at 03:24 PM.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    As I keep saying, you don't need the date in that format.
    You need a Date object, and then use a PreparedStatement (see previous links) for the INSERT, and not a concatenated SQL String.

    To use Norms idea of removing the j loop:
    Java Code:
    SimpleDateFormat sdf = new SimpleDateFormat("MM-DD-YYYY hh:mm:ss");
    java.sql.Date d = new java.sql.Date(sdf.parse(sheet.getCellAt(4, i).getValue().toString()).getTime());
    
    PreparedStatement ps = connection.prepareStatement("Your SQL done as a PreparedStatement");
    ps.setString(1, name);
    ... etc
    ps.setDate(4,d);
    ps.executeUpdate();
    
    (and close it)
    Note that the PreparedStatement should be created in a try-with-resources, but this is all documented in the tutorial link I gave.
    The above is not copy/paste code.
    Last edited by Norm; 07-21-2017 at 05:29 PM. Reason: Changed j to 4 in getCellAt & ! to 1
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #16
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    OP has been given instructions on using a PreparedStatement here: https://www.java-forums.org/new-java...readsheet.html

    He just never came back. My guess is that he didn't get it right and started using regular Statements instead.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  17. #17
    kavithav is offline Member
    Join Date
    Apr 2017
    Posts
    26
    Rep Power
    0

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    SimpleDateFormat sdf = new SimpleDateFormat("MM-DD-YYYY hh:mm:ss");
    java.sql.Date d = new java.sql.Date(sdf.parse(sheet.getCellAt(4, i).getValue().toString()).getTime());

    i had did as per you suggestion but what ever code i try the same thing is getting happened i am getting the same exception e = (java.text.ParseException) java.text.ParseException: Unparseable date: "Tue May 02 00:00:00 IST 2017"

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    Try writing a small simple program to work on the correct parse field.
    Something like these two lines.
    Java Code:
          SimpleDateFormat sdf = new SimpleDateFormat("MM-DD-YYYY hh:mm:ss");
          System.out.println(sdf.parse("Tue May 02 00:00:00 IST 2017"));
    Work on finding a parse field that works.

    Those two lines give this error:
    Exception in thread "main" java.text.ParseException: Unparseable date: "Tue May 02 00:00:00 IST 2017"
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    The code is not intended to be pasted into your code.
    It's intended to show the structure of what you need to do.
    I provided a link to SimpleDateFormat last week, and that has links to the various formatting options.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to convert the date in string format to mm/dd/yyyy format

    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. how to convert as "2012-05-03" date format only
    By raj.mscking@gmail.com in forum New To Java
    Replies: 12
    Last Post: 05-04-2012, 12:36 PM
  2. Cannot convert '20100101' to date(format = yyyyMMdd)
    By reach2sudhakar in forum New To Java
    Replies: 4
    Last Post: 10-15-2010, 09:50 AM
  3. Replies: 1
    Last Post: 10-15-2010, 08:58 AM
  4. Convert String to Time format
    By plasma in forum New To Java
    Replies: 1
    Last Post: 12-16-2009, 08:58 AM
  5. how to convert date format
    By saran123 in forum New To Java
    Replies: 5
    Last Post: 10-16-2008, 06:10 PM

Tags for this Thread

Posting Permissions

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