Problem to convert dd/mm/yyyy to yyyy-mm-dd in JAVA. Please help me.



  • Dear friends,



    I have the problem with date format. I receiving the date with the
    format dd/mm/yyyy. But I can upload to MySQL only in the format of
    yyyy-mm-dd.



    how should I handle this situation, for this I've created these code
    lines.But I have some problem with these line. please help me to solve
    this problem.



    rs.getString("DATA_SCAD1")// where the source from .xls files

    String pattern = "yyyy-MM-dd";

    SimpleDateFormat format = new SimpleDateFormat(pattern);

    try {

    Date date = format.parse("DATA_SCAD1");

    System.out.println(date);

    } catch (ParseException e) {

    e.printStackTrace();

    }

    System.out.println(format.format(new Date()));



    this out put gives me Tue Apr 03 00:00:00 IST 2007



    But I need the date format in yyyy-mm-dd.



    regards,

    maza



    thanks in advance.



  • I'm not your friend.

    but wouldn't it be a lot simpler to just split the string on "/" and juggle then values around and put "-" between them. 



  • @stratos said:

    I'm not your friend.

    Don't be so hostile, dear sir.

     

    But yeah, if it's a string you receive, and a string you must produce, then splitting "/", and concatenating in the right order is the simplest solution.



  • @dhromed said:

    @stratos said:

    I'm not your friend.

    Don't be so hostile, dear sir.

    Why not? He's clearly an idiot. 



  • @asuffield said:

    @dhromed said:
    @stratos said:

    I'm not your friend.

    Don't be so hostile, dear sir.

    Why not? He's clearly an idiot. 

    Don't be so hostile, dear sir.



  • To move between formats :

    SimpleDateFormat inFmt = new SimpleDateFormat("dd/MM/yyyy");

    SimpleDateFormat outFmt = new SimpleDateFormat("yyyy-mm-dd");

    String out = outFmt.format(inFmt.parse("22/11/2007"));

     

    But if you're insert into a date column in a database :

    preparedStatement.setDate(colNum, inFmt.parse("22/11/2007"));

    should be fine.



  • @dhromed said:

    @asuffield said:
    @dhromed said:
    @stratos said:

    I'm not your friend.

    Don't be so hostile, dear sir.

    Why not? He's clearly an idiot. 

    Don't be so hostile, dear sir.

    It wasn't as harshly meant as you might have interpreted it. I just found it quite weird to address people whom you have never known and will probably never meet as "friends", my more social disturbed side found this weird and plainly wrong, because he can't seriously believe everyone to read that text to be his friend, and i thus thwarted him in his attempt to casually make me his friend by stating the opposite.



  • @asuffield said:

    @dhromed said:
    @stratos said:

    I'm not your friend.

    Don't be so hostile, dear sir.

    Why not? He's clearly an idiot. 

    Well, he's using such language at the forum where TubeRodent is lurking... Probably, you're right. 8=]



  • I haven't tested this, but try:

     

                    String unparsed = rs.getString("DATA_SCAD1");
                    if (unparsed == null)
                        return;
    
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                try
                {
                    Date date = format.parse(unparsed);
                    System.out.println(format.format(date));
                }
                catch (ParseException e)
                {
                    e.printStackTrace();
                }
    


  • @Spectre said:

    @asuffield said:
    @dhromed said:
    @stratos said:

    I'm not your friend.

    Don't be so hostile, dear sir.

    Why not? He's clearly an idiot. 

    Well, he's using such language at the forum where TubeRodent is lurking... Probably, you're right. 8=]

    hahahahha good one :)

     

    http://en.wikipedia.org/wiki/Longtail 



  • @asuffield said:

    @dhromed said:
    @stratos said:

    I'm not your friend.

    Don't be so hostile, dear sir.

    Why not? He's clearly an idiot. 

     It
    would seem that way, it's the kind of thing about 10 seconds of
    thinking about or  5 seconds of googleing would find the answer

     also in the OP's example won't this line always throw and exception


    Date date = format.parse("DATA_SCAD1");

    it's been a few years since i've done any java but according to my calculations that's never going to work
     



  • Hi, i am not ur friend .. i also had the same problem..just put

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

    This will be the answer for u... bye.. ask if any doubts is thr in my id:-giri_girish86@yahoo.co.in



  • @giri said:

    Hi, i am not ur friend .. i also had the same problem..just put

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

    This will be the answer for u... bye.. ask if any doubts is thr in my id:-giri_girish86@yahoo.co.in

     

    Dude, you're like some sort of web forum necromancer, raising old threads from the dead. 



  • @giri said:

    Hi, i am not ur friend .. i also had the same problem..just put

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

    This will be the answer for u... bye.. ask if any doubts is thr in my id:-giri_girish86@yahoo.co.in

    Oh shit, the Java forums must have overflown! 



  • I also have this problem, can you send me the codes?



  • @XIU said:

    I also have this problem, can you send me the codes?

     

    Sorry, you are using teh wrong syntax  for this request.



  • <FONT size=2><FONT color=#7f0055 size=2><FONT size=2></FONT><FONT size=2>

    Date[] creationDate = </FONT><FONT color=#7f0055 size=2>new</FONT><FONT size=2> Date[20];

    </FONT><FONT size=2>

    String[] date=editplaceForm.getCreationDate();

    </FONT>for
    </FONT><FONT size=2>(</FONT><FONT color=#7f0055 size=2>int</FONT><FONT size=2> i=0;i<date.</FONT><FONT color=#0000c0 size=2>length</FONT><FONT size=2>;i++){</FONT></FONT><FONT size=2></FONT><FONT color=#0000c0 size=2>logger</FONT><FONT size=2>.log(</FONT><FONT color=#2a00ff size=2>"date is......"</FONT><FONT size=2>+date[i]);</FONT></FONT><FONT size=2>

    }

    </FONT><FONT color=#7f0055 size=2>for</FONT><FONT size=2>(</FONT><FONT color=#7f0055 size=2>int</FONT><FONT size=2> i=0;i<date.</FONT><FONT color=#0000c0 size=2>length</FONT><FONT size=2>;i++){</FONT></FONT><FONT size=2>

     

    SimpleDateFormat formatter = </FONT><FONT color=#7f0055 size=2>new</FONT><FONT size=2> SimpleDateFormat(</FONT><FONT color=#2a00ff size=2>"yyyy-MM-dd"</FONT><FONT size=2>);</FONT></FONT><FONT size=2>

    creationDate[i] = formatter.parse(date[i]);

    </FONT><FONT color=#0000c0 size=2>logger</FONT><FONT size=2>.log(</FONT><FONT color=#2a00ff size=2>"date after parsing is......"</FONT><FONT size=2>+creationDate[i]);</FONT></FONT><FONT size=2>

     

    }

    </FONT> date is......2008-03-24

    date after parsing is......Mon Mar 24 00:00:00 IST 2008

     

     

     in the form  i am giving as String format for date...

    chk

    </FONT>


  • @MasterPlanSoftware said:

    @XIU said:
    I also have this problem, can you send me the codes?

     Sorry, you are using teh wrong syntax  for this request.

    Great.  Now look what you've done.  You should've known sarcasm doesn't work on these Java nuts. 



  • @bstorer said:

    @MasterPlanSoftware said:

    @XIU said:
    I also have this problem, can you send me the codes?

     Sorry, you are using teh wrong syntax  for this request.

    Great.  Now look what you've done.  You should've known sarcasm doesn't work on these Java nuts. 

    I'm a Java nut. I parsed the invisible sarcasm tag without any problems. And I would have gone the google path, instead of asking this question here...


  •  hi, thanks for this code... it helped me a lot,

     

    thanks again.

     

    krishna


  • ♿ (Parody)

    @krishna said:

    hi, thanks for this code... it helped me a lot,

    krishna

    Hmmm, thread necromancy, or just reincarnation?



  • @boomzilla said:

    @krishna said:

    hi, thanks for this code... it helped me a lot,

    krishna

    Hmmm, thread necromancy, or just reincarnation?

     

    Neither, it just took him a while to type the code in.



  • Fascinating. Every time you think this thread hit rock bottom, someone throws it a shovel.


Log in to reply