How can I utilize SimpleDateFormat with Calendar?

Denys S. picture Denys S. · Apr 11, 2011 · Viewed 87.1k times · Source

I've got GregorianCalendar instances and need to use SimpleDateFormat (or maybe something that can be used with calendar but that provides required #fromat() feature) to get needed output. Please, suggest work arounds as good as permanent solutions.

Answer

janhink picture janhink · Apr 11, 2011

Try this:

Calendar cal = new GregorianCalendar();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
dateFormat.setTimeZone(cal.getTimeZone());
System.out.println(dateFormat.format(cal.getTime()));