How can I use SimpleDateFormat with a calendar?

I have instances of GregorianCalendar and you need to use SimpleDateFormat (or maybe something that can be used with a calendar, but which provides the required #fromat () function) to get the desired result. Please offer to work as well as permanent solutions.

+59
java date formatting calendar simpledateformat
Apr 11 2018-11-11T00:
source share
4 answers

Try the following:

Calendar cal = new GregorianCalendar(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); dateFormat.setTimeZone(cal.getTimeZone()); System.out.println(dateFormat.format(cal.getTime())); 
+84
Apr 11 '11 at 13:16
source share
— -

eQui answer missing step

 Calendar cal = new GregorianCalendar(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); #---- This uses the provided calendar for the output ----- dateFormat.setCalendar(cal); System.out.println(dateFormat.format(cal.getTime())); 
+31
May 10 '12 at
source share

Calendar.getTime () returns a date that can be used with SimpleDateFormat.

+3
Apr 11
source share

Just call calendar.getTime() and pass the resulting Date object to the format method.

+3
Apr 11 '11 at 13:15
source share



All Articles