2

We're searching for information on how to format instances of java.util.Calendar and more general information and coding hints regarding transition from using java.util.Date to java.util.Calendar.

best, phil

Philipp
  • 46
  • 4
  • Has the [official documentation](http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html) not proven useful? – Justin L. Jun 16 '10 at 07:38
  • If you really cannot avoid getting an old-fashioned `Calendar`, convert to a modern `ZonedDateTime` using `((GregorianCalendar) yourCalendar).toZonedDateTime()` and format the resulting object as shown in the answer by Arvind Kumar Avinash. – Ole V.V. Jul 15 '21 at 06:22

2 Answers2

3

My hint would be not to use either Date or Calendar. Use Joda Time instead. It's much, much nicer than the built-in classes. JSR-310 will hopefully, eventually bring something Joda-like into the main library, but for the moment Joda is your best bet.

If you must stick to Date/Calendar, see java.text.DateFormat and java.text.SimpleDateFormat. Remember that they're not thread-safe though :(

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice from the home page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API:

If you are getting an object of java.util.Date from some API, your first step should be to convert it into Instant using Date#toInstant which can be converted to other types of modern Date-Time API.

A demo with the modern Date-Time API:

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        Instant instant = Instant.now();
        System.out.println(instant);

        ZonedDateTime zdtUtc = instant.atZone(ZoneId.of("Etc/UTC"));
        System.out.println(zdtUtc);
        ZonedDateTime zdtNewYork = instant.atZone(ZoneId.of("America/New_York"));
        System.out.println(zdtNewYork);

        // Fixed offset
        OffsetDateTime odtUtc = instant.atOffset(ZoneOffset.UTC);
        System.out.println(odtUtc);
        OffsetDateTime odtWithOffset0530Hours = instant.atOffset(ZoneOffset.of("+05:30"));
        System.out.println(odtWithOffset0530Hours);

        // OffsetDateTime from ZonedDateTime
        OffsetDateTime odtNewYork = zdtNewYork.toOffsetDateTime();
        System.out.println(odtNewYork);

        // LocalDate in New York
        LocalDate todayNewYork = zdtNewYork.toLocalDate();
        System.out.println(todayNewYork);
        // Alternatively
        System.out.println(LocalDate.now(ZoneId.of("America/New_York")));

        // LocalDateTime in New York
        LocalDateTime nowNewYork = zdtNewYork.toLocalDateTime();
        System.out.println(nowNewYork);
        // Alternatively
        System.out.println(LocalDateTime.now(ZoneId.of("America/New_York")));

        // Formatted output
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMMM dd HH:mm:ss z uuuu", Locale.ENGLISH);
        System.out.println(dtf.format(zdtNewYork));
    }
}

Output:

2021-07-14T19:22:13.544911Z
2021-07-14T19:22:13.544911Z[Etc/UTC]
2021-07-14T15:22:13.544911-04:00[America/New_York]
2021-07-14T19:22:13.544911Z
2021-07-15T00:52:13.544911+05:30
2021-07-14T15:22:13.544911-04:00
2021-07-14
2021-07-14
2021-07-14T15:22:13.544911
2021-07-14T15:22:13.586971
Wed July 14 15:22:13 EDT 2021

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Some helpful answers using java.time API:

  1. Check this answer and this answer to learn how to use java.time API with JDBC.
  2. How to use ParsePostion?
  3. Timezone conversion.
  4. SimpleDateFormat does not handle fraction-of-second beyond three digits in the millisecond part correctly.
  5. How to convert LocalDate to ZonedDateTime?
  6. 'Z' is not the same as Z.
  7. Day-of-month with ordinal.
  8. The standard library does not support a formatted Date-Time object.
  9. Never use SimpleDateFormat or DateTimeFormatter without a Locale.
  10. How to check if timestamp (epoch time) is of today's or yesterday's?

* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110