ZetCode

Java ThaiBuddhistDate 类

最后修改时间:2025 年 4 月 16 日

java.time.chrono.ThaiBuddhistDate 类表示泰国佛教日历系统中的日期。 该日历比公历早 543 年。 它在泰国很常用。

ThaiBuddhistDate 是不可变的且线程安全的。 它实现了 ChronoLocalDate 接口。 该类提供了根据泰国佛教年历处理日期的方法。

ThaiBuddhistDate 类概述

ThaiBuddhistDate 提供了创建、操作和格式化日期的方法。 关键操作包括日期算术、字段访问以及与其他日历系统的转换。 该类处理从公元 1 年开始的日期。

public final class ThaiBuddhistDate implements ChronoLocalDate, Serializable {
    public static ThaiBuddhistDate now();
    public static ThaiBuddhistDate now(ZoneId zone);
    public static ThaiBuddhistDate of(int prolepticYear, int month, int dayOfMonth);
    public static ThaiBuddhistDate from(TemporalAccessor temporal);
    public int getEraValue();
    public ThaiBuddhistEra getEra();
    public int lengthOfMonth();
    public ThaiBuddhistDate plus(long amountToAdd, TemporalUnit unit);
    public ThaiBuddhistDate minus(long amountToSubtract, TemporalUnit unit);
}

上面的代码显示了 ThaiBuddhistDate 提供的关键方法。 这些方法允许在泰国佛教日历系统中创建、操作和查询日期。

创建 ThaiBuddhistDate 对象

可以通过多种方式创建 ThaiBuddhistDate 对象。 最常用的方法是 now 获取当前日期,以及用于特定日期的工厂方法。 还支持从其他日期类型进行转换。

Main.java
package com.zetcode;

import java.time.chrono.ThaiBuddhistDate;
import java.time.LocalDate;
import java.time.ZoneId;

public class Main {

    public static void main(String[] args) {
        
        // Current date
        ThaiBuddhistDate now = ThaiBuddhistDate.now();
        System.out.println("Current Thai date: " + now);
        
        // Specific date
        ThaiBuddhistDate date1 = ThaiBuddhistDate.of(2565, 4, 15);
        System.out.println("Specific Thai date: " + date1);
        
        // From LocalDate
        ThaiBuddhistDate date2 = ThaiBuddhistDate.from(LocalDate.of(2022, 4, 15));
        System.out.println("Converted from LocalDate: " + date2);
        
        // With time zone
        ThaiBuddhistDate date3 = ThaiBuddhistDate.now(ZoneId.of("Asia/Bangkok"));
        System.out.println("Current date in Bangkok: " + date3);
    }
}

此示例演示了创建 ThaiBuddhistDate 对象的不同方式。 输出显示了泰国佛教年历中的日期。 请注意与公历日期的年份差异。

访问日期组件

可以将 ThaiBuddhistDate 分解为年、月和日组件。 这些值遵循泰国佛教日历系统。 还可以访问时代。

Main.java
package com.zetcode;

import java.time.chrono.ThaiBuddhistDate;
import java.time.temporal.ChronoField;

public class Main {

    public static void main(String[] args) {

        ThaiBuddhistDate date = ThaiBuddhistDate.now();
        
        // Get year (Buddhist era)
        int year = date.get(ChronoField.YEAR);
        System.out.println("Year: " + year);
        
        // Get month
        int month = date.get(ChronoField.MONTH_OF_YEAR);
        System.out.println("Month: " + month);
        
        // Get day
        int day = date.get(ChronoField.DAY_OF_MONTH);
        System.out.println("Day: " + day);
        
        // Get era
        System.out.println("Era: " + date.getEra());
    }
}

此示例显示了如何从 ThaiBuddhistDate 中提取组件。 该年份比公历早 543 年。 月和日的值与公历一致。

日期算术

ThaiBuddhistDate 支持通过 plusminus 方法进行日期算术运算。 这些操作对于计算未来或过去的日期很有用。 该类可以正确处理月和年的边界。

Main.java
package com.zetcode;

import java.time.chrono.ThaiBuddhistDate;
import java.time.temporal.ChronoUnit;

public class Main {

    public static void main(String[] args) {

        ThaiBuddhistDate date = ThaiBuddhistDate.of(2565, 4, 15);
        
        // Add days
        ThaiBuddhistDate plusDays = date.plus(10, ChronoUnit.DAYS);
        System.out.println("10 days later: " + plusDays);
        
        // Subtract months
        ThaiBuddhistDate minusMonths = date.minus(2, ChronoUnit.MONTHS);
        System.out.println("2 months earlier: " + minusMonths);
        
        // Add years
        ThaiBuddhistDate plusYears = date.plus(1, ChronoUnit.YEARS);
        System.out.println("1 year later: " + plusYears);
        
        // Complex operation
        ThaiBuddhistDate complex = date.plus(3, ChronoUnit.MONTHS)
                                     .minus(15, ChronoUnit.DAYS);
        System.out.println("Complex operation result: " + complex);
    }
}

此示例显示了使用 ThaiBuddhistDate 执行日期算术运算的各种方式。 操作可以使用 ChronoUnit 常量来表示不同的时间单位。 所有计算都遵循泰国佛教日历规则。

比较日期

可以比较 ThaiBuddhistDate 对象以确定时间顺序。 该类提供了 isAfterisBeforecompareTo 方法。 这些比较遵循泰国佛教年历。

Main.java
package com.zetcode;

import java.time.chrono.ThaiBuddhistDate;
import java.time.temporal.ChronoUnit;

public class Main {

    public static void main(String[] args) {

        ThaiBuddhistDate today = ThaiBuddhistDate.now();
        ThaiBuddhistDate tomorrow = today.plus(1, ChronoUnit.DAYS);
        ThaiBuddhistDate 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
        ThaiBuddhistDate sameDate = ThaiBuddhistDate.of(
            today.get(ChronoField.YEAR),
            today.get(ChronoField.MONTH_OF_YEAR),
            today.get(ChronoField.DAY_OF_MONTH));
        System.out.println("Today equals sameDate: " + today.equals(sameDate));
    }
}

此示例演示了比较 ThaiBuddhistDate 对象的各种方式。 比较方法会考虑包括年、月和日在内的完整日期。 请注意,相等性要求所有组件完全匹配。

在日历系统之间转换

ThaiBuddhistDate 可以与公历等其他日历系统相互转换。 当在应用程序中使用多个日历系统时,这些转换至关重要。

Main.java
package com.zetcode;

import java.time.LocalDate;
import java.time.chrono.ThaiBuddhistDate;
import java.time.format.DateTimeFormatter;

public class Main {

    public static void main(String[] args) {

        ThaiBuddhistDate thaiDate = ThaiBuddhistDate.now();
        
        // Convert to LocalDate (Gregorian)
        LocalDate gregorianDate = LocalDate.from(thaiDate);
        System.out.println("Gregorian date: " + gregorianDate);
        
        // Convert back to ThaiBuddhistDate
        ThaiBuddhistDate backToThai = ThaiBuddhistDate.from(gregorianDate);
        System.out.println("Back to Thai: " + backToThai);
        
        // Formatting
        DateTimeFormatter formatter = DateTimeFormatter
            .ofPattern("dd MMM yyyy GG");
        System.out.println("Formatted: " + formatter.format(thaiDate));
    }
}

此示例演示了 ThaiBuddhistDate 和公历日期之间的转换。 格式化程序显示了如何使用佛教纪元标记显示日期。 所有转换都保留了不同日历系统中的同一天。

处理日期字段

ThaiBuddhistDate 提供了查询各种日期字段的方法。 这些字段包括星期几、一年中的第几天和月份长度。 这些值遵循泰国佛教日历规则。

Main.java
package com.zetcode;

import java.time.DayOfWeek;
import java.time.chrono.ThaiBuddhistDate;
import java.time.temporal.ChronoField;

public class Main {

    public static void main(String[] args) {

        ThaiBuddhistDate date = ThaiBuddhistDate.of(2565, 4, 15);
        
        // Day of week
        DayOfWeek dow = DayOfWeek.from(date);
        System.out.println("Day of week: " + dow);
        
        // Day of year
        int doy = date.get(ChronoField.DAY_OF_YEAR);
        System.out.println("Day of year: " + doy);
        
        // Month length
        int monthLength = date.lengthOfMonth();
        System.out.println("Days in month: " + monthLength);
        
        // Year length
        int yearLength = date.lengthOfYear();
        System.out.println("Days in year: " + yearLength);
        
        // Leap year
        System.out.println("Is leap year: " + date.isLeapYear());
    }
}

此示例显示了如何从 ThaiBuddhistDate 查询各种日期字段。 星期几的值与 ISO 日历匹配。 月份和年份的长度与公历遵循相同的规则。

来源

Java ThaiBuddhistDate 类文档

在本文中,我们介绍了 Java ThaiBuddhistDate 类的基本方法和功能。 了解这些概念对于在 Java 应用程序中使用泰国佛教日历日期至关重要。

作者

我叫 Jan Bodnar,是一位经验丰富的程序员。 我从 2007 年开始撰写编程文章,至今已撰写了 1400 多篇文章和 8 本电子书。 凭借超过八年的教学经验,我致力于分享我的知识并帮助他人掌握编程概念。

列出所有Java教程