How can I utilize SimpleDateFormat with Calendar?

JavaDateCalendarFormattingSimpledateformat

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

Try this:

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

Solution 2 - Java

eQui's answer is missing a 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()));

Solution 3 - Java

Calendar.getTime() returns a Date which can be used with SimpleDateFormat.

Solution 4 - Java

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

Solution 5 - Java

java.time

I recommend that you use java.time, the modern Java date and time API, for your date and time work. So not GregorianCalendar. Since a GregorianCalendar was holding all of date, time of day and time zone, the general modern substitute for it is ZonedDateTime.

You didn’t specify what needed output would be. I am assuming that we want output for a human user. So use Java’s built in localized format for the user’s locale:

private static final DateTimeFormatter FORMATTER
		= DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
				.withLocale(Locale.forLanguageTag("es"));

I specified Spanish language just as an example. If you want to use the JVM’s default locale, you may either specify Locale.getDefault(Locale.Category.FORMAT) or leave out the call to withLocale() completely. Now formatting a ZonedDateTime is straightforward (and simpler than it was with a GregorianCalendar):

	ZonedDateTime zdt = ZonedDateTime.of(
			2011, 4, 11, 19, 11, 15, 0, ZoneId.of("Australia/Perth"));
	System.out.println(zdt.format(FORMATTER));

Output from this example:

> 11 de abril de 2011, 19:11:15 AWST

In case you only need dates and no time of day or time zone, you need two changes:

  1. Use LocalDate instead of ZonedDateTime.
  2. Use DateTimeFormatter.ofLocalizedDate() instead of .ofLocalizedDateTime().

What if I really got a GregorianCalendar?

If you got a GregorianCalendar from a legacy API not yet upgraded to java.time, convert to ZonedDateTime:

	GregorianCalendar cal = new GregorianCalendar(
			TimeZone.getTimeZone(ZoneId.of("Australia/Perth")));
	cal.set(2011, Calendar.APRIL, 11, 19, 11, 15);
	ZonedDateTime zdt = cal.toZonedDateTime();

Then proceed as before. Output will be the same.

Oracle tutorial: Date Time explaining how to use java.time.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDenys S.View Question on Stackoverflow
Solution 1 - JavajanhinkView Answer on Stackoverflow
Solution 2 - JavaJamesKingstonView Answer on Stackoverflow
Solution 3 - JavaReverend GonzoView Answer on Stackoverflow
Solution 4 - JavaThomasView Answer on Stackoverflow
Solution 5 - JavaOle V.V.View Answer on Stackoverflow