集册 Java实例教程 Java将LocalDate转换为ChronoLocalDate并返回字符串

Java将LocalDate转换为ChronoLocalDate并返回字符串

欢马劈雪     最近更新时间:2020-01-02 10:19:05

719
Java将LocalDate转换为ChronoLocalDate并返回字符串
import java.time.*;
/*
 from NowJava.com 
*/

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 StringConverter {


    /**

     * 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);/* from N o w J a v a . c o m - 时代Java*/

            } 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", StringConverter.toString(date,

                        JapaneseChronology.INSTANCE));

        System.out.printf("%s%n",

                StringConverter.toString(date, MinguoChronology.INSTANCE));

        System.out.printf("%s%n", StringConverter.toString(date,

                ThaiBuddhistChronology.INSTANCE));

        System.out.printf("%s%n",

                StringConverter.toString(date, HijrahChronology.INSTANCE));


        System.out.printf("%s%n", StringConverter.fromString(

                "10/29/0008 H", JapaneseChronology.INSTANCE));

        System.out.printf("%s%n", StringConverter.fromString(

                "10/29/0085 1", MinguoChronology.INSTANCE));

        System.out.printf("%s%n", StringConverter.fromString(

                "10/29/2539 B.E.", ThaiBuddhistChronology.INSTANCE));

        System.out.printf("%s%n", StringConverter.fromString("6/16/1417 1",

                HijrahChronology.INSTANCE));

    }

}


展开阅读全文