Hesam
Hesam

Reputation: 53600

Illegal argument exception in formatting Calendar object

I have an object of calendar and I want to display it in this format: "year.month.day hour.min.sec TimeZone"

I have written following lines of code but last line throws IllegalArgumentException.

I have no idea where is my problem. Any suggestion would be appreciated. Thanks.

Calendar date = Calendar.getInstance(TimeZone.getDefault());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z");
String bookingDate = formatter.format(date);

==========

Update

==========

Log:

> 09-30 11:19:47.205: E/AndroidRuntime(19285): FATAL EXCEPTION: main
> 09-30 11:19:47.205: E/AndroidRuntime(19285): Process:
> com.passenger, PID: 19285 09-30 11:19:47.205:
> E/AndroidRuntime(19285): java.lang.IllegalArgumentException: Bad
> class: class java.util.GregorianCalendar 09-30 11:19:47.205:
> E/AndroidRuntime(19285):  at
> java.text.DateFormat.format(DateFormat.java:296) 09-30 11:19:47.205:
> E/AndroidRuntime(19285):  at java.text.Format.format(Format.java:93)
> 09-30 11:19:47.205: E/AndroidRuntime(19285):  at
> com.fragment.bookingdetail.BookingDetailFragment.sendDataToAP(BookingDetailFragment.java:1105)
> 09-30 11:19:47.205: E/AndroidRuntime(19285):  at
> com.fragment.bookingdetail.BookingDetailFragment.sendBookingToServer(BookingDetailFragment.java:782)
> 09-30 11:19:47.205: E/AndroidRuntime(19285):  at
> com.fragment.bookingdetail.BookingDetailFragment.onClick(BookingDetailFragment.java:340)

Upvotes: 2

Views: 1898

Answers (2)

Aniruddha
Aniruddha

Reputation: 4487

Try this one

Calendar date = Calendar.getInstance(TimeZone.getDefault());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z", Locale.ENGLISH);
String bookingDate = formatter.format(date.getTime());
System.out.println(bookingDate); 

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279990

Basically you can't format a Calendar object with a DateFormat. Use Calendar#getTime() and pass the resulting Date object to the format call.

Upvotes: 2

Related Questions