下面列出了怎么用org.joda.time.MonthDay的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Gets the anniversary date not yet passed (with delta of 1 day)
* @param date Date
* @return Next anniversary in the year
*/
public static Date getNextAnniversary(Date date) {
DateTime dateTimeNow = DateTime.now();
MonthDay monthDayNow = MonthDay.now();
MonthDay monthDayOfNextDate = MonthDay.fromDateFields(date);
if(monthDayNow.isEqual(monthDayOfNextDate)) {
DateTime inputDate = new DateTime(date);
return dateTimeNow
.withHourOfDay(inputDate.getHourOfDay())
.withMinuteOfHour(inputDate.getMinuteOfHour())
.withSecondOfMinute(inputDate.getSecondOfMinute())
.withMillisOfSecond(inputDate.getMillisOfSecond()).toDate();
} if(monthDayNow.isBefore(monthDayOfNextDate))
return new DateTime(date).withYear(dateTimeNow.getYear()).toDate();
else {
DateTime dateTimeOfNextDate = new DateTime(date).withYear(dateTimeNow.getYear()).plusYears(1);
return dateTimeOfNextDate.toDate();
}
}
/**
* Return number of days between today and the next date in a year
* @param date date for calculate delta
* @return Number of days always >= 0
*/
public static int daysBetweenTodayAnd(Date date) {
MonthDay monthDayNow = MonthDay.now();
MonthDay monthDayOfNextDate = MonthDay.fromDateFields(date);
if(monthDayNow.isEqual(monthDayOfNextDate))
return 0;
if(monthDayNow.isBefore(monthDayOfNextDate))
return Days.daysBetween(monthDayNow, monthDayOfNextDate).getDays();
else {
DateTime dateTimeNow = DateTime.now();
DateTime dateTimeOfNextDate = new DateTime(date).withYear(dateTimeNow.getYear()).plusYears(1);
return Days.daysBetween(DateTime.now(), dateTimeOfNextDate).getDays();
}
}
@Test
public void creates_instance_of_MonthDay() {
MonthDay monthDay = fixture.create(MonthDay.class);
assertThat(monthDay, notNullValue());
assertThat(monthDay.getMonthOfYear(), is(1));
assertThat(monthDay.getDayOfMonth(), is(1));
}
@Override
public MonthDay parse(String text, Locale locale) throws ParseException {
return MonthDay.parse(text);
}
@Override
public String print(MonthDay object, Locale locale) {
return object.toString();
}
@Override
public void registerFormatters(FormatterRegistry registry) {
JodaTimeConverters.registerConverters(registry);
DateTimeFormatter dateFormatter = getFormatter(Type.DATE);
DateTimeFormatter timeFormatter = getFormatter(Type.TIME);
DateTimeFormatter dateTimeFormatter = getFormatter(Type.DATE_TIME);
addFormatterForFields(registry,
new ReadablePartialPrinter(dateFormatter),
new LocalDateParser(dateFormatter),
LocalDate.class);
addFormatterForFields(registry,
new ReadablePartialPrinter(timeFormatter),
new LocalTimeParser(timeFormatter),
LocalTime.class);
addFormatterForFields(registry,
new ReadablePartialPrinter(dateTimeFormatter),
new LocalDateTimeParser(dateTimeFormatter),
LocalDateTime.class);
addFormatterForFields(registry,
new ReadableInstantPrinter(dateTimeFormatter),
new DateTimeParser(dateTimeFormatter),
ReadableInstant.class);
// In order to retain backwards compatibility we only register Date/Calendar
// types when a user defined formatter is specified (see SPR-10105)
if (this.formatters.containsKey(Type.DATE_TIME)) {
addFormatterForFields(registry,
new ReadableInstantPrinter(dateTimeFormatter),
new DateTimeParser(dateTimeFormatter),
Date.class, Calendar.class);
}
registry.addFormatterForFieldType(Period.class, new PeriodFormatter());
registry.addFormatterForFieldType(Duration.class, new DurationFormatter());
registry.addFormatterForFieldType(YearMonth.class, new YearMonthFormatter());
registry.addFormatterForFieldType(MonthDay.class, new MonthDayFormatter());
registry.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
}
public MonthDay getMonthDay() {
return monthDay;
}
public void setMonthDay(MonthDay monthDay) {
this.monthDay = monthDay;
}
@Override
public MonthDay parse(String text, Locale locale) throws ParseException {
return MonthDay.parse(text);
}
@Override
public String print(MonthDay object, Locale locale) {
return object.toString();
}
@Override
public void registerFormatters(FormatterRegistry registry) {
JodaTimeConverters.registerConverters(registry);
DateTimeFormatter dateFormatter = getFormatter(Type.DATE);
DateTimeFormatter timeFormatter = getFormatter(Type.TIME);
DateTimeFormatter dateTimeFormatter = getFormatter(Type.DATE_TIME);
addFormatterForFields(registry,
new ReadablePartialPrinter(dateFormatter),
new LocalDateParser(dateFormatter),
LocalDate.class);
addFormatterForFields(registry,
new ReadablePartialPrinter(timeFormatter),
new LocalTimeParser(timeFormatter),
LocalTime.class);
addFormatterForFields(registry,
new ReadablePartialPrinter(dateTimeFormatter),
new LocalDateTimeParser(dateTimeFormatter),
LocalDateTime.class);
addFormatterForFields(registry,
new ReadableInstantPrinter(dateTimeFormatter),
new DateTimeParser(dateTimeFormatter),
ReadableInstant.class);
// In order to retain backwards compatibility we only register Date/Calendar
// types when a user defined formatter is specified (see SPR-10105)
if (this.formatters.containsKey(Type.DATE_TIME)) {
addFormatterForFields(registry,
new ReadableInstantPrinter(dateTimeFormatter),
new DateTimeParser(dateTimeFormatter),
Date.class, Calendar.class);
}
registry.addFormatterForFieldType(Period.class, new PeriodFormatter());
registry.addFormatterForFieldType(Duration.class, new DurationFormatter());
registry.addFormatterForFieldType(YearMonth.class, new YearMonthFormatter());
registry.addFormatterForFieldType(MonthDay.class, new MonthDayFormatter());
registry.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
}
public MonthDay getMonthDay() {
return monthDay;
}
public void setMonthDay(MonthDay monthDay) {
this.monthDay = monthDay;
}
@Override
public MonthDay parse(String text, Locale locale) throws ParseException {
return MonthDay.parse(text);
}
@Override
public String print(MonthDay object, Locale locale) {
return object.toString();
}
public static void registerAdditionalFormatters(FormatterRegistry registry) {
registry.addFormatterForFieldType(YearMonth.class, new YearMonthFormatter());
registry.addFormatterForFieldType(MonthDay.class, new MonthDayFormatter());
}
@Override
public MonthDay parse(String text, Locale locale) throws ParseException {
return MonthDay.parse(text);
}
@Override
public String print(MonthDay object, Locale locale) {
return object.toString();
}
public static void registerAdditionalFormatters(FormatterRegistry registry) {
registry.addFormatterForFieldType(YearMonth.class, new YearMonthFormatter());
registry.addFormatterForFieldType(MonthDay.class, new MonthDayFormatter());
}
public MonthDay getMonthDay() {
return monthDay;
}
public void setMonthDay(MonthDay monthDay) {
this.monthDay = monthDay;
}
@Override
public MonthDay fromNonNullValue(String s) {
return MonthDay.parse(s);
}
@Override
public String toNonNullValue(MonthDay value) {
return value.toString();
}
public MonthDay getMonthDay() {
return monthDay;
}
public void setMonthDay(MonthDay monthDay) {
this.monthDay = monthDay;
}