Java Date Time - Java Non ISO Calendar System








LocalDate uses the ISO calendar system, which is the Gregorian calendar.

Java Date-Time API also support other calendars such as Thai Buddhist calendar, Hijrah calendar, Minguo calendar, and Japanese calendar.

The non-ISO calendar-related classes are defined in the java.time.chrono package.

For each available non-ISO calendar system there is a custom-named Chronology and custom-named Date class.

Custom-named Chronology class represents the calendar system whereas custom-named Date class represents a date in the custom calendar system.

Each custom-named Chronology class contains an INSTANCE constant that represents a singleton instance of that class.

Example

The following code shows to get the current date in the Thai Buddhist calendar:

import java.time.chrono.ThaiBuddhistChronology;
import java.time.chrono.ThaiBuddhistDate;
/*from w  w w. j a va  2  s. co  m*/
public class Main {

  public static void main(String[] args) {
    ThaiBuddhistChronology thaiBuddhistChrono = ThaiBuddhistChronology.INSTANCE;
    ThaiBuddhistDate now = thaiBuddhistChrono.dateNow();
    ThaiBuddhistDate now2 = ThaiBuddhistDate.now();
    System.out.println("Current Date  in Thai  Buddhist: " + now);
    System.out.println("Current Date  in Thai  Buddhist: " + now2);
  }
}

The code above generates the following result.





Example 2

We can convert dates in one calendar system to another using from() method.

The following code shows how to convert ISO date to Thai Buddhist date and vice versa.

import java.time.LocalDate;
import java.time.chrono.ThaiBuddhistDate;
// ww  w . j  a v  a  2s .co  m
public class Main {

  public static void main(String[] args) {
    ThaiBuddhistDate thaiBuddhistNow = ThaiBuddhistDate.now();
    LocalDate isoNow = LocalDate.now();
    System.out.println("Thai  Buddhist Current Date: " + thaiBuddhistNow);
    System.out.println("ISO  Current Date: " + isoNow);

    // Convert Thai Buddhist date to ISO date and vice versa
    ThaiBuddhistDate thaiBuddhistNow2 = ThaiBuddhistDate.from(isoNow);
    LocalDate isoNow2 = LocalDate.from(thaiBuddhistNow);
    System.out.println("Thai  Buddhist Current Date  from  ISO:  "
        + thaiBuddhistNow2);
    System.out.println("ISO  Current Date  from  Thai  Buddhist: " + isoNow2);

  }
}

The code above generates the following result.





Example 3

The following code shows how to convert date to different Date system.

/* w  w w. j ava2s .c o  m*/
/*
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
import java.time.*;
import java.time.chrono.*;
import java.time.format.*;
import java.time.temporal.*;
import java.util.Locale;
import java.io.PrintStream;

/*
 * Convert LocalDate -> ChronoLocalDate -> String and back.
 */

public class Main {

  /**
   * Converts a LocalDate (ISO) value to a ChronoLocalDate date using the
   * provided Chronology, and then formats the ChronoLocalDate to a String using
   * a DateTimeFormatter with a SHORT pattern based on the Chronology and the
   * current Locale.
   *
   * @param localDate
   *          - the ISO date to convert and format.
   * @param chrono
   *          - an optional Chronology. If null, then IsoChronology is used.
   */
  public static String toString(LocalDate localDate, Chronology chrono) {
    if (localDate != null) {
      Locale locale = Locale.getDefault(Locale.Category.FORMAT);
      ChronoLocalDate cDate;
      if (chrono == null) {
        chrono = IsoChronology.INSTANCE;
      }
      try {
        cDate = chrono.date(localDate);
      } catch (DateTimeException ex) {
        System.err.println(ex);
        chrono = IsoChronology.INSTANCE;
        cDate = localDate;
      }
      String pattern = "M/d/yyyy GGGGG";
      DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);
      return dateFormatter.format(cDate);
    } else {
      return "";
    }
  }

  /**
   * Parses a String to a ChronoLocalDate using a DateTimeFormatter with a short
   * pattern based on the current Locale and the provided Chronology, then
   * converts this to a LocalDate (ISO) value.
   *
   * @param text
   *          - the input date text in the SHORT format expected for the
   *          Chronology and the current Locale.
   *
   * @param chrono
   *          - an optional Chronology. If null, then IsoChronology is used.
   */
  public static LocalDate fromString(String text, Chronology chrono) {
    if (text != null && !text.isEmpty()) {
      Locale locale = Locale.getDefault(Locale.Category.FORMAT);
      if (chrono == null) {
        chrono = IsoChronology.INSTANCE;
      }
      String pattern = "M/d/yyyy GGGGG";
      DateTimeFormatter df = new DateTimeFormatterBuilder().parseLenient()
          .appendPattern(pattern).toFormatter().withChronology(chrono)
          .withDecimalStyle(DecimalStyle.of(locale));
      TemporalAccessor temporal = df.parse(text);
      ChronoLocalDate cDate = chrono.date(temporal);
      return LocalDate.from(cDate);
    }
    return null;
  }

  public static void main(String[] args) {
    LocalDate date = LocalDate.of(1996, Month.OCTOBER, 29);
    System.out.printf("%s%n", toString(date, JapaneseChronology.INSTANCE));
    System.out.printf("%s%n", toString(date, MinguoChronology.INSTANCE));
    System.out.printf("%s%n", toString(date, ThaiBuddhistChronology.INSTANCE));
    System.out.printf("%s%n", toString(date, HijrahChronology.INSTANCE));

    System.out.printf("%s%n",
        fromString("10/29/0008 H", JapaneseChronology.INSTANCE));
    System.out.printf("%s%n",
        fromString("10/29/0085 1", MinguoChronology.INSTANCE));
    System.out.printf("%s%n",
        fromString("10/29/2539 B.E.", ThaiBuddhistChronology.INSTANCE));
    System.out.printf("%s%n",
        fromString("6/16/1417 1", HijrahChronology.INSTANCE));
  }
}

The code above generates the following result.