java:表示带有可选月份和日期的日期

标签 java class date jodatime

我想在java中存储带有可选月份和日期的日期。 我知道java.time.LocalYear仅存储年份的类。 我应该创建自己的自定义类来保存带有可选月份和日期的日期吗?或者是否有任何自定义库可以解决该问题。


public class Date {

  private LocalYear year;
  private int month;
  private int day;

  public Date(LocalYear year) {
   this.year = year;
  } 
  
  public Date(LocalYear year, int month) {
   this.year = year;
   this.month = month;
  }

  public Date(LocalYear year, int month, iny day) {
   this.year = year;
   this.month = month;
   this.day = day;
  }
}

最佳答案

如果不了解您的用例,就很难指导您。一种选择是使用 TemporalAccessor 接口(interface)作为带或不带月份和/或日期的日期的通用类型,然后放置 LocalDateYearMonthYear 到变量中(最后一个类称为 Year (不是 LocalYear,尽管它符合命名方案))。例如:

    List<TemporalAccessor> dates = List.of(
            LocalDate.of(2019, Month.OCTOBER, 3), // full date
            YearMonth.of(2019, Month.NOVEMBER), // no day of month
            Year.of(2020)); // no month or day of month

我们可以用它做什么?一个例子:

    for (TemporalAccessor ta : dates) {
        System.out.println(ta);
        System.out.println("Year: " + ta.get(ChronoField.YEAR));
        if (ta.isSupported(ChronoField.MONTH_OF_YEAR)) {
            System.out.println("Month: " + ta.get(ChronoField.MONTH_OF_YEAR));
        } else {
            System.out.println("Month: undefined");
        }
        if (ta.isSupported(ChronoField.DAY_OF_MONTH)) {
            System.out.println("Day: " + ta.get(ChronoField.DAY_OF_MONTH));
        } else {
            System.out.println("Day: undefined");
        }
        System.out.println();
    }

输出:

2019-10-03
Year: 2019
Month: 10
Day: 3

2019-11
Year: 2019
Month: 11
Day: undefined

2020
Year: 2020
Month: undefined
Day: undefined

我无法判断它是否或如何满足您的要求。

使用 ChronoField 常量进行访问是低级的,因此您可能需要将 TemporalAccessor 包装在一个带有漂亮 getter 的漂亮类中。例如:

public class PartialDate {

    private TemporalAccessor date;

    public PartialDate(Year year) {
        date = year;
    }

    public PartialDate(Year year, int month) {
        date = year.atMonth(month);
    }

    public PartialDate(Year year, int month, int day) {
        date = year.atMonth(month).atDay(day);
    }

    public Year getYear() {
        return Year.from(date);
    }

    public OptionalInt getMonthValue() {
        if (date.isSupported(ChronoField.MONTH_OF_YEAR)) {
            return OptionalInt.of(date.get(ChronoField.MONTH_OF_YEAR));
        } else {
            return OptionalInt.empty();
        }
    }

    // A similar getDay method

}

您可以根据需要扩展类(class)。也许您想要直接接受 Month 枚举常量和/或 YearMonth 对象的构造函数和/或返回包装在 Optional 中的类型的 getter .

链接: Oracle tutorial: Date Time解释如何使用 java.time。

关于java:表示带有可选月份和日期的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58155682/

相关文章:

java - 如何在 Kotlin 中创建空类对象?

Java/Jython - 如何将未找到的类与特定的 jar 文件关联起来

jquery - Bootstrap 日期选择器仅显示特定月份

Javascript 获取下周二或周五的日期(最近的)

Java:优先级队列实现可按正确顺序迭代

java - 限制字符串的长度

objective-c - Objective-C中的类构成问题: Is it possible to inherit a class variable?

php - 选择(过滤)匹配一天、开始和结束过滤列表的记录

java - Apache cxf Jax-RS 2.0 拦截器

java - 使用 mysql-connector-java-5.1.24 将 Java 连接到 MySQL