14 Replies Latest reply: Aug 31, 2007 7:39 AM by 807605 RSS

    Problem with java.tuil.Date.

    807605
      Hi all

      I am in strange problem.
      I want to convert java.util.Date object to this format :

      MM/dd/yyyy

      but the condition is i should not convert it to String type or any other type.
      My method should return Date object.
      Any body has any idea.

      Thanx in advance.
        • 1. Re: Problem with java.tuil.Date.
          807605
          I am in strange problem.
          Indeed!
          I want to convert java.util.Date object to this
          format :

          MM/dd/yyyy

          but the condition is i should not convert it to
          String type or any other type.
          That's not possible. Date by itself does not have a format. You need to format it to a String to display.
          My method should return Date object.
          Then don't bother about the format. You can always format it with a SimpleDateFormat whenever it is required for display.
          • 2. Re: Problem with java.tuil.Date.
            807605
            thanx for ur reply sir.,

            i belv now that its not possible.

            But if i have a String like

            11/20/199

            can i convert it into a Date like

            11/20/199 with the help of SimpleDateFormate method. ?
            plz suggest something if its possible?

            thanx
            • 3. Re: Problem with java.tuil.Date.
              807605
              sure, you can
              DateFormat myDateFormat = new SimpleDateFormat(strFormat);
              Date myDate = null;
              try {
                   myDate = myDateFormat.parse(strDate);
              } catch (ParseException e) {
                   System.out.println("Invalid Date Parser Exception ");
                   e.printStackTrace();
              }
              System.out.println("Finished Date Function " + myDate.getDay());
              • 4. Re: Problem with java.tuil.Date.
                807605
                thanx once again for reply sir
                i have just used your mehtod like :
                   DateFormat myDateFormat = new SimpleDateFormat("MM/dd/yyyy");
                          Date myDate = null;
                          String retString = myDateFormat.format(createdon);
                          myDate = myDateFormat.parse(retString);
                          System.out.println("Finished Date Function " + myDate.getDay());
                          System.out.println("Finished Date " + myDate);
                The results are :
                Finished Date Sat Jan 01 00:00:00 NPT 2000
                Finished Date Function 6

                But this is not what i want.
                I want Finished Date like MM/dd/yyyy exactly.
                Any further suggestion plz.
                thanx.
                • 5. Re: Problem with java.tuil.Date.
                  807605
                  a date has no visible format
                  • 6. Re: Problem with java.tuil.Date.
                    Herko_ter_Horst
                    Note that the output you're seeing is a result of the toString() method in class java.util.Date. This cannot be changed.

                    If you want the output in any other format, use SimpleDateFormat:
                    System.out.println("Finished Date " + myDateFormat.format(myDate));
                    TBH, in this bit of code you posted, you go through a lot of trouble just to end up with what you already have to start with, which is a String representation of a date in MM/dd/yyyy format...

                    What is the actual problem?
                    • 7. Re: Problem with java.tuil.Date.
                      807605
                      Thanx for ur reply sir.

                      The actual problem is i am using hibernate with struts.
                      I have a bean called News Bean.
                      In the database(SQL Server i have a datetime field called createdon.
                      Now struts actionform doesnt support java.util.Date .
                      So i have to convert this string value from actionform in java.util.Date(in MM/dd/yyyy formate) and then assigning it to my bean before saving the values in database.
                      I have done it and its working fine.
                      But now the original problem is coming while updating the things(specialy date field) .

                      When i update, the struts jsp page is showing the record from the database and the date is coming in the formate yyyy-mm-dd , which is strang as i have save it with mm/dd/yyyy formate.
                      To rectify this i m modifying the bean's getter method for date so that it should return the date with mm/dd/yyyy formate.
                      For this i am converting date to string first of all then trying to convert it into date with mm/dd/yyyy format.
                      But this is not working .
                      Here i m getting problems.
                      And unable to solve it
                      i have enough search on the net.
                      please help if u can
                      • 8. Re: Problem with java.tuil.Date.
                        807605
                        Oh !!! just use following code
                        DateFormat myDateFormat = new SimpleDateFormat("MM/dd/yyyy");
                                  Date myDate= new Date();
                                  String retString = myDateFormat.format(myDate.getTime()); //pAY ATTENTION HERE 
                                  System.out.println("Finished Date " + retString);
                                  
                        This will help u

                        Message was edited by:
                        AmitChalwade123456
                        • 9. Re: Problem with java.tuil.Date.
                          807605
                          wow Amit u r great .

                          Now i m getting Finished Date as .08/31/2007

                          Thanx.
                          Now one more thing:

                          Can i convert this string into date and this date should be look like
                          08/31/2007...?? I need it as my function is returning Date not the String.

                          I m waiting for ur responose..
                          Thanx
                          • 10. Re: Problem with java.tuil.Date.
                            807605
                            I think u can not convert string to date
                            but one thing u do , Now u have string 08/31/2007
                            split it using regex and get separate date , month, year from that string
                            and then u use setDate(), setMonth() and setYear() methods which re will return DATE to u
                            • 11. Re: Problem with java.tuil.Date.
                              807605
                              I think u can not convert string to date
                              but one thing u do , Now u have string 08/31/2007
                              split it using regex and get separate date , month,
                              year from that string
                              and then u use setDate(), setMonth() and setYear()
                              methods which re will return DATE to u
                              Well there's some bad advice.

                              Which is particularly odd because you seem to know about SimpleDateFormat.

                              Please look at the SimpleDateFormat API. There is a parse method that parses Strings into Dates. No need for mucking about with regex.
                              • 12. Re: Problem with java.tuil.Date.
                                800323
                                wow Amit u r great .

                                Now i m getting Finished Date as .08/31/2007

                                Thanx.
                                Now one more thing:

                                Can i convert this string into date and this date
                                should be look like
                                08/31/2007...?? I need it as my function is returning
                                Date not the String.

                                I m waiting for ur responose..
                                Thanx
                                The code may have given you the output you wanted; however, you are still not grasping the concept. A Date object is just a point in time and does not have a format. You can convert a Date to a String of the desired format or convert a String of some format to a Date. However, the Date object will never retain any special display format.
                                • 13. Re: Problem with java.tuil.Date.
                                  807605
                                  >
                                  Can i convert this string into date and this date
                                  should be look like
                                  In the possibly vain hope that a fourth time round will help.

                                  chahalkhushwinder.

                                  STOP talking about the format of your Date. A Date HAS NO FORMAT. A Date is just a Date.

                                  If you want a STRING representing a Date in a particular format then use SimpleDateFormat.

                                  It really seems that you do not grasp this simple yet important concept and your failure to do so probably means you're in for a nasty surprise later.
                                  • 14. Re: Problem with java.tuil.Date.
                                    807605
                                    Well there's some bad advice.

                                    Which is particularly odd because you seem to know
                                    about SimpleDateFormat.

                                    Please look at the SimpleDateFormat API. There is a
                                    parse method that parses Strings into Dates. No need
                                    for mucking about with regex.
                                    I agree . u r right