下面列出了java.time.LocalDate#getYear ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void test_previous() {
for (Month month : Month.values()) {
for (int i = 1; i <= month.length(false); i++) {
LocalDate date = date(2007, month, i);
for (DayOfWeek dow : DayOfWeek.values()) {
LocalDate test = (LocalDate) TemporalAdjusters.previous(dow).adjustInto(date);
assertSame(test.getDayOfWeek(), dow, date + " " + test);
if (test.getYear() == 2007) {
int dayDiff = test.getDayOfYear() - date.getDayOfYear();
assertTrue(dayDiff < 0 && dayDiff > -8, dayDiff + " " + test);
} else {
assertSame(month, Month.JANUARY);
assertTrue(date.getDayOfMonth() < 8);
assertEquals(test.getYear(), 2006);
assertSame(test.getMonth(), Month.DECEMBER);
assertTrue(test.getDayOfMonth() > 24);
}
}
}
}
}
@Test
public void test_previous() {
for (Month month : Month.values()) {
for (int i = 1; i <= month.length(false); i++) {
LocalDate date = date(2007, month, i);
for (DayOfWeek dow : DayOfWeek.values()) {
LocalDate test = (LocalDate) TemporalAdjusters.previous(dow).adjustInto(date);
assertSame(test.getDayOfWeek(), dow, date + " " + test);
if (test.getYear() == 2007) {
int dayDiff = test.getDayOfYear() - date.getDayOfYear();
assertTrue(dayDiff < 0 && dayDiff > -8, dayDiff + " " + test);
} else {
assertSame(month, Month.JANUARY);
assertTrue(date.getDayOfMonth() < 8);
assertEquals(test.getYear(), 2006);
assertSame(test.getMonth(), Month.DECEMBER);
assertTrue(test.getDayOfMonth() > 24);
}
}
}
}
}
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWom_lenient(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField womField = week.weekOfMonth();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral(':')
.appendValue(MONTH_OF_YEAR).appendLiteral(':')
.appendValue(womField).appendLiteral(':')
.appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(LENIENT);
int wom = date.get(womField);
int dow = date.get(DAY_OF_WEEK);
for (int j = wom - 10; j < wom + 10; j++) {
String str = date.getYear() + ":" + date.getMonthValue() + ":" + j + ":" + dow;
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, date.plusWeeks(j - wom), " ::" + str + ": :" + i + "::" + j);
}
date = date.plusDays(1);
}
}
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoyDow(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = week.dayOfWeek();
TemporalField woyField = week.weekOfYear();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral(':')
.appendValue(MONTH_OF_YEAR).appendLiteral(':')
.appendValue(woyField).appendLiteral(':')
.appendValue(dowField).toFormatter();
String str = date.getYear() + ":" + date.getMonthValue() + ":" +
date.get(woyField) + ":" + date.get(dowField);
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, date, " :: " + str + " " + i);
date = date.plusDays(1);
}
}
@Test
@UseDataProvider("data_weekFields")
public void test_parse_resolve_localizedWoy(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField woyField = week.weekOfYear();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral(':')
.appendValue(woyField).appendLiteral(':')
.appendValue(DAY_OF_WEEK).toFormatter();
String str = date.getYear() + ":" +
date.get(woyField) + ":" + date.get(DAY_OF_WEEK);
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(" :: " + str + " " + i, parsed, date);
date = date.plusDays(1);
}
}
@Test
@UseDataProvider("data_weekFields")
public void test_parse_resolve_localizedWomDow_lenient(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = week.dayOfWeek();
TemporalField womField = week.weekOfMonth();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral(':')
.appendValue(MONTH_OF_YEAR).appendLiteral(':')
.appendValue(womField).appendLiteral(':')
.appendValue(dowField).toFormatter().withResolverStyle(LENIENT);
int wom = date.get(womField);
int dow = date.get(dowField);
for (int j = wom - 10; j < wom + 10; j++) {
String str = date.getYear() + ":" + date.getMonthValue() + ":" + j + ":" + dow;
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(" ::" + str + ": :" + i + "::" + j, parsed, date.plusWeeks(j - wom));
}
date = date.plusDays(1);
}
}
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoyDow(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = week.dayOfWeek();
TemporalField woyField = week.weekOfYear();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral(':')
.appendValue(MONTH_OF_YEAR).appendLiteral(':')
.appendValue(woyField).appendLiteral(':')
.appendValue(dowField).toFormatter();
String str = date.getYear() + ":" + date.getMonthValue() + ":" +
date.get(woyField) + ":" + date.get(dowField);
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, date, " :: " + str + " " + i);
date = date.plusDays(1);
}
}
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWomDow_lenient(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = week.dayOfWeek();
TemporalField womField = week.weekOfMonth();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral(':')
.appendValue(MONTH_OF_YEAR).appendLiteral(':')
.appendValue(womField).appendLiteral(':')
.appendValue(dowField).toFormatter().withResolverStyle(LENIENT);
int wom = date.get(womField);
int dow = date.get(dowField);
for (int j = wom - 10; j < wom + 10; j++) {
String str = date.getYear() + ":" + date.getMonthValue() + ":" + j + ":" + dow;
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, date.plusWeeks(j - wom), " ::" + str + ": :" + i + "::" + j);
}
date = date.plusDays(1);
}
}
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWomDow_lenient(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = week.dayOfWeek();
TemporalField womField = week.weekOfMonth();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral(':')
.appendValue(MONTH_OF_YEAR).appendLiteral(':')
.appendValue(womField).appendLiteral(':')
.appendValue(dowField).toFormatter().withResolverStyle(LENIENT);
int wom = date.get(womField);
int dow = date.get(dowField);
for (int j = wom - 10; j < wom + 10; j++) {
String str = date.getYear() + ":" + date.getMonthValue() + ":" + j + ":" + dow;
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, date.plusWeeks(j - wom), " ::" + str + ": :" + i + "::" + j);
}
date = date.plusDays(1);
}
}
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWomDow_lenient(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = week.dayOfWeek();
TemporalField womField = week.weekOfMonth();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendValue(YEAR).appendLiteral(':')
.appendValue(MONTH_OF_YEAR).appendLiteral(':')
.appendValue(womField).appendLiteral(':')
.appendValue(dowField).toFormatter().withResolverStyle(LENIENT);
int wom = date.get(womField);
int dow = date.get(dowField);
for (int j = wom - 10; j < wom + 10; j++) {
String str = date.getYear() + ":" + date.getMonthValue() + ":" + j + ":" + dow;
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, date.plusWeeks(j - wom), " ::" + str + ": :" + i + "::" + j);
}
date = date.plusDays(1);
}
}
@SuppressWarnings("deprecation")
@Test(dataProvider="date")
public void test_date_parse(LocalDate date, FormatStyle dateStyle, int dateStyleOld, Locale locale) {
DateFormat old = DateFormat.getDateInstance(dateStyleOld, locale);
Date oldDate = new Date(date.getYear() - 1900, date.getMonthValue() - 1, date.getDayOfMonth());
String text = old.format(oldDate);
DateTimeFormatter f = builder.appendLocalized(dateStyle, null).toFormatter(locale);
TemporalAccessor parsed = f.parse(text, pos);
assertEquals(pos.getIndex(), text.length());
assertEquals(pos.getErrorIndex(), -1);
assertEquals(LocalDate.from(parsed), date);
}
private int diffConv30v360(final LocalDate start, final LocalDate end) {
int dayStart = start.getDayOfMonth();
int dayEnd = end.getDayOfMonth();
if (dayEnd == CalculatorConstants.MONTH_31_DAYS && dayStart >= CalculatorConstants.MONTH_30_DAYS) {
dayEnd = CalculatorConstants.MONTH_30_DAYS;
}
if (dayStart == CalculatorConstants.MONTH_31_DAYS) {
dayStart = CalculatorConstants.MONTH_30_DAYS;
}
return (end.getYear() - start.getYear()) * CalculatorConstants.YEAR_360 + (end.getMonthValue() - start.getMonthValue()) * CalculatorConstants.MONTH_30_DAYS + dayEnd - dayStart;
}
public static String formatArchivedOnDate(LocalDate date) {
if (date.getDayOfYear() == today) {
return "today";
} else if (date.getDayOfYear() == yesterday) {
return "yesterday";
} else if (date.getYear() == currentYear) {
// Don't include the year for recordings from the current year
return "on " + date.format(DATE_RECORDED_SHORT_DATE_FORMATTER);
} else {
return "on " + date.format(DATE_RECORDED_LONG_DATE_FORMATTER);
}
}
@SuppressWarnings("deprecation")
@Test(dataProvider="date")
public void test_date_parse(LocalDate date, FormatStyle dateStyle, int dateStyleOld, Locale locale) {
DateFormat old = DateFormat.getDateInstance(dateStyleOld, locale);
Date oldDate = new Date(date.getYear() - 1900, date.getMonthValue() - 1, date.getDayOfMonth());
String text = old.format(oldDate);
DateTimeFormatter f = builder.appendLocalized(dateStyle, null).toFormatter(locale);
TemporalAccessor parsed = f.parse(text, pos);
assertEquals(pos.getIndex(), text.length());
assertEquals(pos.getErrorIndex(), -1);
assertEquals(LocalDate.from(parsed), date);
}
@SuppressWarnings("deprecation")
@Test(dataProvider="date")
public void test_date_parse(LocalDate date, FormatStyle dateStyle, int dateStyleOld, Locale locale) {
DateFormat old = DateFormat.getDateInstance(dateStyleOld, locale);
Date oldDate = new Date(date.getYear() - 1900, date.getMonthValue() - 1, date.getDayOfMonth());
String text = old.format(oldDate);
DateTimeFormatter f = builder.appendLocalized(dateStyle, null).toFormatter(locale);
TemporalAccessor parsed = f.parse(text, pos);
assertEquals(pos.getIndex(), text.length());
assertEquals(pos.getErrorIndex(), -1);
assertEquals(LocalDate.from(parsed), date);
}
@SuppressWarnings("deprecation")
@Test(dataProvider="date")
public void test_date_print(LocalDate date, FormatStyle dateStyle, int dateStyleOld, Locale locale) {
DateFormat old = DateFormat.getDateInstance(dateStyleOld, locale);
Date oldDate = new Date(date.getYear() - 1900, date.getMonthValue() - 1, date.getDayOfMonth());
String text = old.format(oldDate);
DateTimeFormatter f = builder.appendLocalized(dateStyle, null).toFormatter(locale);
String formatted = f.format(date);
assertEquals(formatted, text);
}
/**
* Returns a {@code LocalGregorianCalendar.Date} converted from the given {@code isoDate}.
*
* @param isoDate the local date, not null
* @return a {@code LocalGregorianCalendar.Date}, not null
*/
private static LocalGregorianCalendar.Date toPrivateJapaneseDate(LocalDate isoDate) {
LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);
sun.util.calendar.Era sunEra = JapaneseEra.privateEraFrom(isoDate);
int year = isoDate.getYear();
if (sunEra != null) {
year -= sunEra.getSinceDate().getYear() - 1;
}
jdate.setEra(sunEra).setYear(year).setMonth(isoDate.getMonthValue()).setDayOfMonth(isoDate.getDayOfMonth());
JapaneseChronology.JCAL.normalize(jdate);
return jdate;
}
@Override
public double getDaycountFraction(final LocalDate startDate, final LocalDate endDate) {
if(startDate.isAfter(endDate)) {
return -getDaycountFraction(endDate,startDate);
}
/*
* Determine the denominator of act/act.
*/
double denominator;
final LocalDate startDatePlusOneYear = startDate.plusYears(1);
if(endDate.isAfter(startDatePlusOneYear)) {
/*
* The following method applies, if the interval spans more than one year:
* fraction = actual number of days / average number of days per year.
*/
final LocalDate startDateYearStart = startDate.withDayOfYear(1);
final LocalDate endDateYearEnd = endDate.withDayOfYear(endDate.lengthOfYear()).plusDays(1);
final double spannedYears = endDate.getYear() - startDate.getYear() + 1;
denominator = getDaycount(startDateYearStart, endDateYearEnd) / spannedYears;
}
else {
final boolean isStartLeapYear = startDate.isLeapYear();
final boolean isEndLeapYear = endDate.isLeapYear();
/*
* If the start and end span less or equal one year:
* If start and end fall in a leap year, use ACT/366.
*/
if(isStartLeapYear && isEndLeapYear)
{
denominator = 366.0;
}
else
/*
* If the start and end span less or equal one year:
* If either falls in a leap year and Feb 29th of that leap year lies in between start and end (or is equal to start or end), use ACT/366.
*/
if(isStartLeapYear || isEndLeapYear)
{
// Get February 29th of the respective leap year
final LocalDate leapYearsFeb29th = isStartLeapYear ?
LocalDate.of(startDate.getYear(), Month.FEBRUARY, 29) :
LocalDate.of(endDate.getYear(), Month.FEBRUARY, 29);
// Check position of February 29th
if(startDate.compareTo(leapYearsFeb29th) <= 0 && endDate.compareTo(leapYearsFeb29th) >= 0) {
denominator = 366.0;
}
else {
denominator = 365.0;
}
}
else {
/*
* If the start and end span less or equal one year:
* If none of the above applies, use denominator = ACT/365.
*/
denominator = 365.0;
}
}
final double daycountFraction = getDaycount(startDate, endDate) / denominator;
return daycountFraction;
}
/**
* Obtains an instance of {@code Date} from a {@link LocalDate} object
* with the same year, month and day of month value as the given
* {@code LocalDate}.
* <p>
* The provided {@code LocalDate} is interpreted as the local date
* in the local time zone.
*
* @param date a {@code LocalDate} to convert
* @return a {@code Date} object
* @exception NullPointerException if {@code date} is null
* @since 1.8
*/
@SuppressWarnings("deprecation")
public static Date valueOf(LocalDate date) {
return new Date(date.getYear() - 1900, date.getMonthValue() -1,
date.getDayOfMonth());
}
/**
* Obtains an instance of {@code Date} from a {@link LocalDate} object
* with the same year, month and day of month value as the given
* {@code LocalDate}.
* <p>
* The provided {@code LocalDate} is interpreted as the local date
* in the local time zone.
*
* @param date a {@code LocalDate} to convert
* @return a {@code Date} object
* @exception NullPointerException if {@code date} is null
* @since 1.8
*/
@SuppressWarnings("deprecation")
public static Date valueOf(LocalDate date) {
return new Date(date.getYear() - 1900, date.getMonthValue() -1,
date.getDayOfMonth());
}