广州北大青鸟计算机职业培训学校
互联网技术培训、软件技术培训、大数据培训、云计算培训、数据分析培训信息网
当前位置:网站首页 > 软件教程 > Java技术 > 正文

惠州​​如何将日期转换为不同的Java日期系统?_北大青鸟IT学校

作者:邓华发布时间:2021-05-30分类:Java技术浏览:784


导读:​如何将日期转换为不同的Java日期系统?相信学过Java的同学都遇到过这个问题,那么答案是什么呢?接下来一起来看看惠州北大青鸟老师是怎么回答的。

如何将日期转换为不同的Java日期系统?相信学过Java的同学都遇到过这个问题,那么答案是什么呢?接下来一起来看看惠州北大青鸟老师是怎么回答的。

实例

以下代码显示如何将日期转换为不同的Java日期系统。

/*
* 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));
 }
}

上面的代码生成以下结果。

10/29/0008 H

10/29/0085 1

10/29/2539 B.E.

6/16/1417 1

1996-10-29

1996-10-29

1996-10-29

1996-10-29

想了解更多关于Java的资讯,可以来惠州北大青鸟新方舟校区详细了解。

Java11.png

Java

标签:惠州计算机JAVA软件开发惠州计算机Java软件开发惠州计算机JAVA培训惠州计算机JAVA软件开发学校惠州计算机Java软件开发培训JAVAJava软件开发北大青鸟IT计算机学校北大青鸟IT软件学校北大青鸟IT学校


Java技术排行
标签列表
网站分类
文章归档
最近发表