Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By Tolls

Thread: how to convert as "2012-05-03" date format only

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

    Question how to convert as "2012-05-03" date format only

    Nice To meet all

    i need a date format of "yyyy-mm-dd", only,
    To get current date from the system, and store in MySQL data base.

    i try with "import java.util.Calendar;"


    Java Code:
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    
    Date today = new Date();
    
    Date todayWithZeroTime =formatter.parse(formatter.format(today));

    but it show me the format of "Fri May 04 12:57:55 IST 2012" only,

    can Anyone guide me..,
    Thank you for your help..,

  2. #2
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: how to convert as "2012-05-03" date format only

    formatter.parse() takes text and puts it back into the default date format.

    formatter.format() formats the date according to its specifications.

    formatter.parse(formatter.format()) will take the formatted date and convert it right back to the default format.

    You don't want to parse a formatted date.

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,096
    Rep Power
    20

    Default Re: how to convert as "2012-05-03" date format only

    A Date object does not have a format.
    It is simply a long representing the number of milliseconds since the epoch (1/1/1970 UTC).
    How is your date column represented in the database?
    If it's as a DATE then there is no need to do anything other than turn the util.Date into a sql.Date.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: how to convert as "2012-05-03" date format only

    Thank you for your response sir!
    which one shall i fallow,

    formatter.format()
    or
    formatter.parse(formatter.format())

    replay please..,

  5. #5
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: how to convert as "2012-05-03" date format only

    Ahh yes. I probably didn't word my explanation very well.
    format() returns a String, not a Date
    As for Sql.... you can take over from here because I'm useless.

    Edit:
    format(date) returns your formatted date as a string
    parse(formattedDate) returns a Date
    Last edited by brynpttrsn; 05-04-2012 at 10:15 AM.

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

    Default Re: how to convert as "2012-05-03" date format only

    ok, Thank you..,

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,096
    Rep Power
    20

    Default Re: how to convert as "2012-05-03" date format only

    It might be an idea to answer my question above, because if this is going into a DATE column in MySQL there is no need to remove the time element, as setDate() will only take the date portion into the database anyway.
    brynpttrsn likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: how to convert as "2012-05-03" date format only

    i wants to create log in form for Client
    if the Client log in with that form it will be forward to my serversocket (username, password with current time)
    and i have to insert the LOG IN table with (username, password with current time and current date of server)

    when log out the user i have receive the (username, password with current time)
    then i update the LOG IN table with (current time) where (username and current date of server)

    to avoid the repeating of same user on that same day , i wants to check with date..,

    if i use the date as it is , it will never check with (where), because date will add with time stamp..,
    That's all sir!..,

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,096
    Rep Power
    20

    Default Re: how to convert as "2012-05-03" date format only

    You still haven't answered the question.
    What MySQL datatype is the current_date column?
    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
    288
    Rep Power
    3

    Default Re: how to convert as "2012-05-03" date format only

    sir! The Data Type is,
    The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format sir..,

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

    Default Re: how to convert as "2012-05-03" date format only

    I got the answer sir!
    To get the current date in MySQL have to place the method of " CURDATE()"..,
    i replace it in java,

    I lot of Thanks to you all my Brethren's,
    i am very happy now..,

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,096
    Rep Power
    20

    Default Re: how to convert as "2012-05-03" date format only

    So, since it was a DATE there was no need to convert anyway.
    You could simply have done:
    Java Code:
    java.sql.Date today = new Date();
    <yourPreparedStatement>.setDate(today);
    No conversion needed at all.

    Of course, using MySQLs own built in functionality is preferable.
    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
    288
    Rep Power
    3

    Default Re: how to convert as "2012-05-03" date format only

    Yes!
    as your idea it is working well,
    i am lucky, Today i learn new idea because of you sir,
    thank you very much sir..,

Similar Threads

  1. Replies: 6
    Last Post: 08-10-2010, 09:22 AM
  2. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  3. Replies: 0
    Last Post: 04-02-2009, 07:02 PM
  4. Convert " ü " into simple " u "
    By nitinb4u in forum New To Java
    Replies: 4
    Last Post: 02-23-2009, 08:35 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •