Java Date Time - Java Predefined Date Time Format








Predefined formatters are defined as constants in the DateTimeFormatter class.

  • BASIC_ISO_DATE
    format and parse a date without using a separator between two date components.
    Example:20140209
    20140221-0600
  • ISO_DATE
    format and parse dates, times, and datetimes using the ISO separators.
    Example:2014-02-09
  • ISO_TIME
    format and parse dates, times, and datetimes using the ISO separators.
    Example:15:21:32.927
    15:21:32.123-06:00
  • ISO_DATE_TIME
    format and parse dates, times, and datetimes using the ISO separators.
    Example:2014-02-09-06:00
    2014-02-09T15:20:07.123-06:00
    2014-02-09T15:20:07.123-06:00[America/Chicago]
  • ISO_INSTANT
    format and parse an instant or a datetime object representing an instant such as a ZonedDateTime in UTC format.
    Example:2014-02-09T21:23:56.870Z
  • ISO_LOCAL_DATE
    format or parse dates without an offset.
    Example:2014-02-09
  • ISO_LOCAL_TIME
    format or parse times without an offset.
    Example:15:30:12.123
  • ISO_LOCAL_DATE_TIME
    format or parse datetimes without an offset.
    Example:2014-02-09T15:29:11.214
  • ISO_OFFSET_DATE
    format and parse dates with an offset using ISO format.
    Example:2014-02-09-06:00
  • ISO_OFFSET_TIME
    format and parse times with an offset using ISO format.
    Example:15:12:29.123-06:00
  • ISO_OFFSET_DATE_TIME
    format and parse datetimes with an offset using ISO format.
    Example:2014-02-09T15:33:07.07-06:0
  • ISO_ZONED_DATE_TIME
    format and parse a datetime with a zone id, if available.
    Example:2014-02-09T15:45:49.123-06:00
    2014-02-09T15:45:12.112-06:00[America/Chicago]
  • ISO_ORDINAL_DATE
    format and parse a date with year and day-of-year.
    Example:2014-009
  • ISO_WEEK_DATE
    format and parse week- based dates. '2014-W02-4' means the fourth day of the second week in 2014.
    Example:2014-W02-4
    2014-W02-4-06:00
  • RFC_1123_DATE_TIME
    format and parse datetimes for e-mails using the RFC1123 specification.
    Example:Thu, 9 Jan 2014 15:12:12 -060




Example

We can use the following methods from DateTimeFormatter class to format a date time value.

String format(TemporalAccessor temporal)
void formatTo(TemporalAccessor temporal, Appendable  appendable)

The following code shows how to use ISO_DATE formatter to format a LocalDate, an OffsetDateTime, and ZonedDateTime.

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
//from  ww w  .ja  v a 2s  .c  om
public class Main{
  public static void main(String[] argv){
    String ldStr = DateTimeFormatter.ISO_DATE.format(LocalDate.now());
    System.out.println(ldStr);
    String odtStr = DateTimeFormatter.ISO_DATE.format(OffsetDateTime.now());
    System.out.println(odtStr);
    String zdtStr = DateTimeFormatter.ISO_DATE.format(ZonedDateTime.now());
    System.out.println(zdtStr);
  }
}

The code above generates the following result.





Example 2

We can also format a date time object using the the format() from the date time classes.

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
//from ww  w  . j  a v a 2s.  c  o m
public class Main {
  public static void main(String[] argv) {
    LocalDate ld = LocalDate.now();
    String ldStr = ld.format(DateTimeFormatter.ISO_DATE);
    System.out.println("Local  Date: " + ldStr);

    OffsetDateTime odt = OffsetDateTime.now();
    String odtStr = odt.format(DateTimeFormatter.ISO_DATE);
    System.out.println("Offset  Datetime: " + odtStr);

    ZonedDateTime zdt = ZonedDateTime.now();
    String zdtStr = zdt.format(DateTimeFormatter.ISO_DATE);
    System.out.println("Zoned  Datetime: " + zdtStr);
  }
}

The code above generates the following result.