Denys S.
Denys S.

Reputation: 6515

How can I utilize SimpleDateFormat with Calendar?

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.

Upvotes: 76

Views: 99111

Answers (5)

Anonymous
Anonymous

Reputation: 86173

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.

Link

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

Upvotes: 1

janhink
janhink

Reputation: 5023

Try this:

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

Upvotes: 110

JamesKingston
JamesKingston

Reputation: 407

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()));

Upvotes: 39

Thomas
Thomas

Reputation: 88707

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

Upvotes: 3

Reverend Gonzo
Reverend Gonzo

Reputation: 40811

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

Upvotes: 3

Related Questions