1

I have Calender object as follows--

final Calendar calendar = Calendar.getInstance();

and I want to get/extract date from above Calendar object in the following format--

"Sat Jul 20 14:53:36 GMT+02:00 2013"

Can anybody please tell me How can I do this..!!

Dnyanesh M
  • 1,349
  • 4
  • 18
  • 46
  • What have you tried? [Oracle Tutorial](http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html), Related question: http://stackoverflow.com/questions/12575990/calendar-date-to-yyyy-mm-dd-format-in-java – BackSlash Jul 20 '13 at 09:27
  • possible duplicate of [Format date in java](http://stackoverflow.com/questions/4772425/format-date-in-java) – JB Nizet Jul 20 '13 at 09:28

4 Answers4

16

Try this:

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss 'GMT'Z yyyy");
    System.out.println(dateFormat.format(cal.getTime()));
amatellanes
  • 3,645
  • 2
  • 17
  • 19
4
  1. Set the calendars data set(), add(), and roll().
  2. Use the Calendars getTime()-method to get a Date-object.
  3. Use SimpleDateFormat to format the date like you want to.
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
3

Try:

    final Calendar calendar = Calendar.getInstance();
    String dat=new SimpleDateFormat("EEE MMM dd hh:mm:ss 'GMT'Z yyyy").format(calendar.getTime())
    System.out.println(dat);//"Sat Jul 20 14:53:36 GMT+02:00 2013"

Should work for you!!

rahulserver
  • 10,411
  • 24
  • 90
  • 164
2
   call getTime() by using Calendar reference

    Calendar calendar = Calendar.getInstance();
    tv.setText(calendar.getTime() + "");

     **or**

Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat(
                "EEE MMM dd hh:mm:ss 'GMT'Z yyyy");
tv.setText(dateFormat.format(calendar.getTime()));
Venky
  • 226
  • 1
  • 5