Java 民国日期类
最后修改时间:2025 年 4 月 16 日
java.time.chrono.MinguoDate 类表示民国历中的日期。该历法主要用于台湾,年份从 1912 年(中华民国成立)开始计算。
MinguoDate 是不可变的,并且是线程安全的。它遵循与其他 Java 时间类相同的设计原则。民国历是一种太阳历,其月份结构与公历相同。
MinguoDate 类概述
MinguoDate 提供了在民国系统中创建、操作和格式化日期的方法。关键操作包括日期算术、字段访问以及转换为其他历法系统。该类处理从公元 1 年开始的日期。
public final class MinguoDate implements ChronoLocalDate, Serializable {
public static MinguoDate now();
public static MinguoDate now(ZoneId zone);
public static MinguoDate now(Clock clock);
public static MinguoDate of(int prolepticYear, int month, int dayOfMonth);
public static MinguoDate from(TemporalAccessor temporal);
public static MinguoDate parse(CharSequence text);
public int getChronology();
public int getEra();
public int getYear();
public int getMonthValue();
public int getDayOfMonth();
}
上面的代码显示了 MinguoDate 提供的一些关键方法。这些方法允许创建、访问字段以及在不同历法系统之间进行转换。该类与 Java 的标准日期时间 API 集成。
创建 MinguoDate 对象
可以采用多种方式创建 MinguoDate 对象。最常用的方法是使用 now 获取当前日期,以及使用工厂方法创建特定日期。也支持从字符串解析。
package com.zetcode;
import java.time.chrono.MinguoDate;
import java.time.ZoneId;
public class Main {
public static void main(String[] args) {
// Current date
MinguoDate today = MinguoDate.now();
System.out.println("Today in Minguo: " + today);
// Specific date
MinguoDate founding = MinguoDate.of(1, 1, 1);
System.out.println("Republic founding: " + founding);
// From system clock with zone
MinguoDate nowInTokyo = MinguoDate.now(ZoneId.of("Asia/Tokyo"));
System.out.println("Now in Tokyo: " + nowInTokyo);
// From string
MinguoDate parsed = MinguoDate.parse("Minguo 112-04-16");
System.out.println("Parsed date: " + parsed);
}
}
此示例演示了创建 MinguoDate 对象的不同方法。输出显示了民国历格式的日期。请注意,民国 1 年对应公历的 1912 年。
访问日期组件
MinguoDate 可以分解为年、月和日组成部分。这些值表示民国历中的日期。这些方法与其他 Java 日期类类似。
package com.zetcode;
import java.time.chrono.MinguoDate;
import java.time.chrono.MinguoEra;
public class Main {
public static void main(String[] args) {
MinguoDate date = MinguoDate.now();
// Get year (Minguo era)
int year = date.getYear();
System.out.println("Minguo year: " + year);
// Get month (1-12)
int month = date.getMonthValue();
System.out.println("Month: " + month);
// Get day of month
int day = date.getDayOfMonth();
System.out.println("Day: " + day);
// Get era (BEFORE_ROC or ROC)
MinguoEra era = date.getEra();
System.out.println("Era: " + era);
}
}
此示例演示了如何从 MinguoDate 中提取组成部分。纪元可以是中华民国 (ROC) 或中华民国前 (BEFORE_ROC)。大多数日期将从民国 1 年开始属于 ROC 纪元。
在日历之间转换
MinguoDate 可以与其他历法系统(如公历)互相转换。在应用程序中使用多个历法系统时,这些转换是必不可少的。
package com.zetcode;
import java.time.LocalDate;
import java.time.chrono.MinguoDate;
public class Main {
public static void main(String[] args) {
MinguoDate minguoToday = MinguoDate.now();
// Convert to LocalDate (Gregorian)
LocalDate gregorianDate = LocalDate.from(minguoToday);
System.out.println("Gregorian date: " + gregorianDate);
// Convert back to MinguoDate
MinguoDate backToMinguo = MinguoDate.from(gregorianDate);
System.out.println("Back to Minguo: " + backToMinguo);
// Specific conversion example
MinguoDate minguoNewYear = MinguoDate.of(112, 1, 1);
LocalDate gregorianNewYear = LocalDate.from(minguoNewYear);
System.out.println("Minguo 112 New Year: " + gregorianNewYear);
}
}
此示例演示了 MinguoDate 和公历日期之间的转换。转换保留了相同的绝对日期,同时更改了历法系统表示。
日期算术
MinguoDate 通过 plus 和 minus 方法支持日期算术。这些操作对于计算未来或过去的日期很有用。该类正确处理月份和年份的边界。
package com.zetcode;
import java.time.chrono.MinguoDate;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
MinguoDate today = MinguoDate.now();
// Add 1 week
MinguoDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
System.out.println("Next week: " + nextWeek);
// Subtract 3 months
MinguoDate threeMonthsAgo = today.minus(3, ChronoUnit.MONTHS);
System.out.println("Three months ago: " + threeMonthsAgo);
// Add 2 years
MinguoDate inTwoYears = today.plus(2, ChronoUnit.YEARS);
System.out.println("In two years: " + inTwoYears);
// Mixed operations
MinguoDate complex = today.plus(1, ChronoUnit.MONTHS)
.minus(10, ChronoUnit.DAYS);
System.out.println("Complex operation result: " + complex);
}
}
此示例显示了使用 MinguoDate 执行日期算术的各种方法。操作可以使用 ChronoUnit 常量来表示不同的时间单位。所有计算都遵守历法规则。
比较日期
可以比较 MinguoDate 对象来确定时间顺序。该类提供了 isBefore、isAfter 和 compareTo 方法。这些比较与其他的 Java 日期类一样工作。
package com.zetcode;
import java.time.chrono.MinguoDate;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
MinguoDate today = MinguoDate.now();
MinguoDate tomorrow = today.plus(1, ChronoUnit.DAYS);
MinguoDate yesterday = today.minus(1, ChronoUnit.DAYS);
System.out.println("Today is before tomorrow: " + today.isBefore(tomorrow));
System.out.println("Today is after yesterday: " + today.isAfter(yesterday));
System.out.println("Comparison result: " + today.compareTo(tomorrow));
// Equality check
MinguoDate sameDate = MinguoDate.of(today.getYear(),
today.getMonthValue(), today.getDayOfMonth());
System.out.println("Today equals sameDate: " + today.equals(sameDate));
}
}
此示例演示了各种比较 MinguoDate 对象的方法。比较方法会考虑完整的日期值。请注意,相等性需要所有日期组成部分完全匹配。
格式化民国日期
可以使用 DateTimeFormatter 格式化 MinguoDate。特殊模式字母可用于民国特有的字段,如纪元和年份。格式化遵循与其他 Java 日期类相同的原则。
package com.zetcode;
import java.time.chrono.MinguoDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
MinguoDate date = MinguoDate.now();
// Default format
String defaultFormat = date.toString();
System.out.println("Default format: " + defaultFormat);
// Custom format
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("GGGG y年M月d日", Locale.TAIWAN);
String customFormat = date.format(formatter);
System.out.println("Custom format: " + customFormat);
// Alternative format
DateTimeFormatter altFormatter = DateTimeFormatter
.ofPattern("'民國' y年 MM月 dd日");
String altFormat = date.format(altFormatter);
System.out.println("Alternative format: " + altFormat);
// ISO format
String isoFormat = date.format(DateTimeFormatter.ISO_DATE);
System.out.println("ISO format: " + isoFormat);
}
}
此示例显示了格式化 MinguoDate 对象的不同方法。格式化程序可以使用特定于区域设置的模式和文本。特殊模式字母处理民国历的细节。
来源
在本文中,我们介绍了 Java MinguoDate 类的基本方法和功能。了解这些概念对于在台湾官方历法系统中处理日期至关重要。
作者
列出所有Java教程。