Results 1 to 10 of 10

Thread: Date operations

  1. #1
    SAR
    SAR is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default Date operations

    Hi
    I need to convert a string to Date to build a query. Following are the values which i am recieving from front end

    String date = 06/21/2010

    while running the query on database table the date should be in following format


    2010-06-21 18:18:02.732

    Here is what i tried
    Date = Date.replace('/','-');
    String pattern = "yyyy-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);

    String dateStartRange = sdf.format(sdf.parse(Date))
    + " 00:00:00.0";
    String dateEndRange = sdf.format(sdf.parse(Date))
    + " 23:59:59.0";


    Function call sdf.format(sdf.parse(Date)) is not returning correct values

    please advise

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default

    not returning correct values
    Please show the actual code and what was returned.

  3. #3
    SAR
    SAR is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    The value of dateStartRange in statement
    String dateStartRange = sdf.format(sdf.parse(Date))
    + " 00:00:00.0"; is returned as 0013-03-02 00:00:00.0

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default

    Please show the actual code and what was returned.

    Sorry, I meant the all of the code showing the input, formatting and output.
    You've left off the definition and values of sdf and Date.
    Something that can be compiled and executed to show the problem.

  5. #5
    SAR
    SAR is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    Hi Norm

    import java.text.SimpleDateFormat;
    import java.util.Date;

    String Date = "06/21/2010";
    Date = Date.replace('/','-');
    String pattern = "yyyy-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);

    String dateStartRange = sdf.format(sdf.parse(Date))
    + " 00:00:00.0";
    String dateEndRange = sdf.format(sdf.parse(Date))
    + " 23:59:59.0";
    ****************************
    I am expecting the value of dateStartRange = 2010-06-21 00:00:00.0 and value of dateEndRange = 2010-06-21 23:59:59.0

    But these are coming out to be
    dateStartRange= "0013-03-02 00:00:00.0"
    dateEndRange= "0013-03-02 23:59:59.0"

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default

    You need separate patterns for the input and for the output.

  7. #7
    SAR
    SAR is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    hmm i am not sure i understand this, how can i set separate patterns for inpit and output

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default

    One to parse the input and one to format the output

  9. #9
    SAR
    SAR is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    Thnx that solved my problem

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    You shouldn't be using Strings for Dates in a query anyway.
    That's Bad Practice...unless the date in the db isn't a DATE, which is almost always itself a mistake.

Similar Threads

  1. Bitwise operations with long
    By NewJavaBean in forum New To Java
    Replies: 2
    Last Post: 04-07-2009, 07:37 PM
  2. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 08:10 AM
  3. Illegal Arithmetic Operations?
    By Cruor in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 04:46 PM
  4. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM
  5. String operations..
    By sireesha in forum New To Java
    Replies: 4
    Last Post: 12-14-2007, 02:04 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
  •