java:用可选的月和日表示日期
我想用java存储带有可选月和日的日期。 我知道只存储年份的java:用可选的月和日表示日期,java,class,date,jodatime,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; } publ
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
接口作为带有和不带月份和/或月日的日期的通用类型,然后将LocalDate
、YearMonth
或Year
放入变量中(最后一个类称为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
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
}
这将产生:
我不知道它是否或如何满足您的要求
使用ChronoField
常量进行访问是低级的,因此您可能希望将TemporalAccessor
包装在一个具有良好getter的良好类中。例如:
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
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
}
您可以根据需要扩展该类。您可能需要直接接受Month
enum常量和/或YearMonth
对象的构造函数和/或返回包装在Optional
s中的类型的getter
链接:解释如何使用java.time。在不了解用例的情况下很难指导您。一个选项是使用
TemporalAccessor
界面作为带有和不带有月份和/或月日的日期的通用类型,然后输入LocalDate
、YearMonth
或Year
输入变量(最后一个类仅称为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
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
}
这将产生:
我不知道它是否或如何满足您的要求
使用ChronoField
常量进行访问是低级的,因此您可能希望将TemporalAccessor
包装在一个具有良好getter的良好类中。例如:
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
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
}
您可以根据需要扩展该类。您可能需要直接接受Month
enum常量和/或YearMonth
对象的构造函数和/或返回包装在Optional
s中的类型的getter
链接:解释如何使用java.time。做任何事情,或者满足您的要求?您的用例是什么?一般来说,我不建议使用一个类来表示多个时间概念。有一个原因是
java.time
必须将其全部拆分。@Slaw您的建议听起来不错,我可以包装所有内容:java.t类中的ime.Year、java.time.YearMonth或java.time.MonthDay可以解决问题。注意,已经有一个类封装了所有这些值(年、月和日):.解释你的业务问题,因为可能有一个比设计部分日期类更好的解决方案。你有什么需要或满足你的要求吗?你的用例是什么?一般来说,我不建议使用一个类来表示多个时间概念。这是一个原因java。时间必须把它全部拆分。@Slaw you sug手势听起来不错,我可以把所有的:java.time.Year、java.time.YearMonth或java.time.MonthDay封装在一个类中,这样就可以解决问题。注意,已经有一个类封装了所有这些值(年、月和日):。解释您的业务问题,因为可能有比设计部分日期类更好的解决方案。