4 Replies Latest reply: Feb 7, 2010 1:14 AM by 843789 RSS

    Date time -- beginner question

    843789
      i need to display date in the format Feb 1, 2010 2:47 PM . but i need to get the date and the time in the above format using the java Calendar class and not Java Date class.
      Calendar calendar = Calendar.getInstance();
      system.out.println("date and time = " + calendar);
      all what i get is
      Sun Feb 07 11:32:01 IST 2010
      as the output.

      i found that theres a class called DateFormat , but i failed to format the date and time that i got *(Sun Feb 07 11:32:01 IST 2010)* to *" Feb 1, 2010 2:47 PM "* format.

      can some one help me.
        • 1. Re: Date time -- beginner question
          843789
          i found that theres a class called DateFormat , but i failed to format the date and time that i got (Sun Feb 07 11:32:01 IST 2010) to " Feb 1, 2010 2:47 PM " format.
          What was the code that you tried and which didn't work?

          Check out SimpleDateFormat - there are some examples there that include all of the date format elements you're trying to include.
          • 2. Re: Date time -- beginner question
            843789
            I tried simpledateformat
            try{     
                                    Calendar calendar = Calendar.getInstance();
                           DateFormat df = new SimpleDateFormat("MMM dd, yyyy hh:mm");
            
                                     /*
                                           Now i need the calendar Date and time to be displayed as in the above format
                                    */
                           }catch(Exception e){
                           e.printStackTrace();
                           }
            But i am unable to format the calendar date and Time to the above format ...

            any help ...
            • 3. Re: Date time -- beginner question
              843789
              Thank you !

              i was just able to format it ..
                             DateFormat df = new SimpleDateFormat("MMM dd, yyyy hh:mm");
                             
                             String daa = df.format(calendar.getTime());
              • 4. Re: Date time -- beginner question
                843789
                Great! - I was in the middle of posting to say have a look at the getTime() and format() methods.