8 Replies Latest reply: Dec 23, 2010 4:54 PM by 796440 RSS

    Java date  appearence.

    812390
      import java.util.Calendar;

      public class DateClass {

      public static void main(String argv[]) {

      Calendar cal = Calendar.getInstance();

      String date = cal.getTime().toString();

      System.out.println("current date is " + date);

      }
      }

      Output : current date is Thu Dec 23 20:28:14 GMT 2010


      but i want the output to be printed as Thursday December 23 2010

      what kind of code do i need to write to bring this. suggestions and help would be appreciated.
        • 1. Re: Java date  appearence.
          splungebob
          Check out SimpleDateFormat:
          http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
          • 2. Re: Java date  appearence.
            796440
            http://download.oracle.com/javase/6/docs/api/java/text/DateFormat.html
            http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
            http://www.google.com/search?q=java+data+format+examples
            • 3. Re: Java date  appearence.
              812390
              hi guys thanks so much for example

              i tried to parse the date by adding these lines


              DateFormat dateFormat= new SimpleDateFormat("dd-MM-yyyy");

              Date converedDate = dateFormat.parse(date);

              but the out put is throwing an error like :
              Exception in thread "main" java.text.ParseException: Unparseable date: "Thu Dec 23 20:51:13 GMT 2010"
              at java.text.DateFormat.parse(DateFormat.java:337)
              at DateClass.main(DateClass.java:18)
              • 4. Re: Java date  appearence.
                Kayaman
                809387 wrote:
                i tried to parse the date by adding these lines


                DateFormat dateFormat= new SimpleDateFormat("dd-MM-yyyy");

                Date converedDate = dateFormat.parse(date);

                but the out put is throwing an error like :
                Exception in thread "main" java.text.ParseException: Unparseable date: "Thu Dec 23 20:51:13 GMT 2010"
                at java.text.DateFormat.parse(DateFormat.java:337)
                at DateClass.main(DateClass.java:18)
                You create your SimpleDateFormat where you say that your dates will be of the form dd-MM-yyyy, so for example "20-10-2010".
                Then you give it a String like Thu Dec 23 20:51:13 GMT 2010, which is not the form that you were promising.

                So the exception is raised because you were lying to SimpleDateFormat about the format.
                • 5. Re: Java date  appearence.
                  796440
                  809387 wrote:
                  hi guys thanks so much for example

                  i tried to parse the date by adding these lines


                  DateFormat dateFormat= new SimpleDateFormat("dd-MM-yyyy");

                  Date converedDate = dateFormat.parse(date);

                  but the out put is throwing an error like :
                  Exception in thread "main" java.text.ParseException: Unparseable date: "Thu Dec 23 20:51:13 GMT 2010"
                  at java.text.DateFormat.parse(DateFormat.java:337)
                  at DateClass.main(DateClass.java:18)
                  Does "Thu Dec 23 20:51:13 GMT 2010" look like "dd-MM-yyyy" to you? When you call parse(), the input String has to match the one you specified for the SimpleDateFormat object. When you call format(), the output String will be in the format specified for the SimpleDateFormat object.

                  I'm guessing that you thought that parse() would magically read whatever string you feed it and produce a Date object with the format you specified. It doesn't work that way. Date objects do not have formats. Only Strings have formats, not Dates. If you want to convert a String representation of a date/time from one format to a String representation of the same date/time in a different format, you need two DateFormat objects, one to parse() the original String and produce a Date, and one to format() that Date.
                  • 6. Re: Java date  appearence.
                    812390
                    Thanks kaymad and jverd. any example of such kind ? or can u post such kind of code here. thanks in advance.

                    trying since hours.
                    • 7. Re: Java date  appearence.
                      Kayaman
                      809387 wrote:
                      Thanks kaymad and jverd. any example of such kind ? or can u post such kind of code here. thanks in advance.
                      A few lines of code won't help here. You need to first understand your problem before you can start solving it. I'd recommend reading the javadocs for SimpleDateFormat (or DateFormat).
                      • 8. Re: Java date  appearence.
                        796440
                        Kayaman wrote:
                        809387 wrote:
                        Thanks kaymad and jverd. any example of such kind ? or can u post such kind of code here. thanks in advance.
                        A few lines of code won't help here. You need to first understand your problem before you can start solving it. I'd recommend reading the javadocs for SimpleDateFormat (or DateFormat).
                        And if you have a hard time understanding those documents, then google for java data format examples. You need to be able to do this kind of basic research yourself.