下面列出了怎么用org.joda.time.DateTimeConstants的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* @param instant millis from 1970-01-01T00:00:00Z
*/
int getDayOfWeek(long instant) {
// 1970-01-01 is day of week 4, Thursday.
long daysSince19700101;
if (instant >= 0) {
daysSince19700101 = instant / DateTimeConstants.MILLIS_PER_DAY;
} else {
daysSince19700101 = (instant - (DateTimeConstants.MILLIS_PER_DAY - 1))
/ DateTimeConstants.MILLIS_PER_DAY;
if (daysSince19700101 < -3) {
return 7 + (int) ((daysSince19700101 + 4) % 7);
}
}
return 1 + (int) ((daysSince19700101 + 3) % 7);
}
public long getDateTimeMillis(
int year, int monthOfYear, int dayOfMonth,
int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond)
throws IllegalArgumentException {
Chronology base;
if ((base = getBase()) != null) {
return base.getDateTimeMillis(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
}
FieldUtils.verifyValueBounds(DateTimeFieldType.hourOfDay(), hourOfDay, 0, 23);
FieldUtils.verifyValueBounds(DateTimeFieldType.minuteOfHour(), minuteOfHour, 0, 59);
FieldUtils.verifyValueBounds(DateTimeFieldType.secondOfMinute(), secondOfMinute, 0, 59);
FieldUtils.verifyValueBounds(DateTimeFieldType.millisOfSecond(), millisOfSecond, 0, 999);
return getDateMidnightMillis(year, monthOfYear, dayOfMonth)
+ hourOfDay * DateTimeConstants.MILLIS_PER_HOUR
+ minuteOfHour * DateTimeConstants.MILLIS_PER_MINUTE
+ secondOfMinute * DateTimeConstants.MILLIS_PER_SECOND
+ millisOfSecond;
}
long calculateFirstDayOfYearMillis(int year) {
// Java epoch is 1970-01-01 Gregorian which is 1686-04-23 Coptic.
// Calculate relative to the nearest leap year and account for the
// difference later.
int relativeYear = year - 1687;
int leapYears;
if (relativeYear <= 0) {
// Add 3 before shifting right since /4 and >>2 behave differently
// on negative numbers.
leapYears = (relativeYear + 3) >> 2;
} else {
leapYears = relativeYear >> 2;
// For post 1687 an adjustment is needed as jan1st is before leap day
if (!isLeapYear(year)) {
leapYears++;
}
}
long millis = (relativeYear * 365L + leapYears)
* (long)DateTimeConstants.MILLIS_PER_DAY;
// Adjust to account for difference between 1687-01-01 and 1686-04-23.
return millis + (365L - 112) * DateTimeConstants.MILLIS_PER_DAY;
}
long getYearDifference(long minuendInstant, long subtrahendInstant) {
int minuendYear = getYear(minuendInstant);
int subtrahendYear = getYear(subtrahendInstant);
// Inlined remainder method to avoid duplicate calls to get.
long minuendRem = minuendInstant - getYearMillis(minuendYear);
long subtrahendRem = subtrahendInstant - getYearMillis(subtrahendYear);
// Balance leap year differences on remainders.
if (subtrahendRem >= FEB_29) {
if (isLeapYear(subtrahendYear)) {
if (!isLeapYear(minuendYear)) {
subtrahendRem -= DateTimeConstants.MILLIS_PER_DAY;
}
} else if (minuendRem >= FEB_29 && isLeapYear(minuendYear)) {
minuendRem -= DateTimeConstants.MILLIS_PER_DAY;
}
}
int difference = minuendYear - subtrahendYear;
if (minuendRem < subtrahendRem) {
difference--;
}
return difference;
}
@Test
public void convertToString()
{
final PFUserDO user = new PFUserDO();
user.setTimeZone(DateHelper.EUROPE_BERLIN);
user.setLocale(Locale.GERMAN);
user.setDateFormat("dd.MM.yyyy");
PFUserContext.setUser(user);
JodaDateConverter conv = new JodaDateConverter();
DateMidnight testDate = createDate(1970, DateTimeConstants.NOVEMBER, 21, EUROPE_BERLIN);
assertEquals("21.11.1970", conv.convertToString(testDate, Locale.GERMAN));
user.setLocale(Locale.ENGLISH);
user.setDateFormat("MM/dd/yyyy");
conv = new JodaDateConverter();
assertEquals("11/21/1970", conv.convertToString(testDate, Locale.GERMAN));
user.setLocale(Locale.GERMAN);
user.setDateFormat("dd.MM.yyyy");
conv = new JodaDateConverter();
testDate = createDate(2009, DateTimeConstants.FEBRUARY, 1, EUROPE_BERLIN);
assertEquals("01.02.2009", conv.convertToString(testDate, Locale.GERMAN));
user.setLocale(Locale.ENGLISH);
user.setDateFormat("MM/dd/yyyy");
conv = new JodaDateConverter();
assertEquals("02/01/2009", conv.convertToString(testDate, Locale.GERMAN));
}
private static DateTime fixStartDate(DateTime startDate) {
if(startDate==null) {
return null;
}
int month = startDate.getMonthOfYear();
if (month <= 2) {
return startDate.withMonthOfYear(DateTimeConstants.JANUARY).withDayOfMonth(1);
} else if (month <= 4) {
return startDate.withMonthOfYear(DateTimeConstants.MARCH).withDayOfMonth(1);
} else if (month <= 6) {
return startDate.withMonthOfYear(DateTimeConstants.MAY).withDayOfMonth(1);
} else if (month <= 8) {
return startDate.withMonthOfYear(DateTimeConstants.JULY).withDayOfMonth(1);
} else if (month <= 10) {
return startDate.withMonthOfYear(DateTimeConstants.SEPTEMBER).withDayOfMonth(1);
} else {
return startDate.withMonthOfYear(DateTimeConstants.NOVEMBER).withDayOfMonth(1);
}
}
private static int jodaWDToJavaWD(int wd) {
switch (wd) {
case DateTimeConstants.MONDAY:
return Calendar.MONDAY;
case DateTimeConstants.TUESDAY:
return Calendar.TUESDAY;
case DateTimeConstants.WEDNESDAY:
return Calendar.WEDNESDAY;
case DateTimeConstants.THURSDAY:
return Calendar.THURSDAY;
case DateTimeConstants.FRIDAY:
return Calendar.FRIDAY;
case DateTimeConstants.SATURDAY:
return Calendar.SATURDAY;
case DateTimeConstants.SUNDAY:
return Calendar.SUNDAY;
}
return 0;
}
private static DateTime fixStartDate(DateTime startDate) {
if(startDate==null) {
return null;
}
int month = startDate.getMonthOfYear();
if (month <= 3) {
return startDate.withMonthOfYear(DateTimeConstants.JANUARY).withDayOfMonth(1);
} else if (month <= 6) {
return startDate.withMonthOfYear(DateTimeConstants.APRIL).withDayOfMonth(1);
} else if (month <= 9) {
return startDate.withMonthOfYear(DateTimeConstants.JULY).withDayOfMonth(1);
} else if (month <= 12) {
return startDate.withMonthOfYear(DateTimeConstants.OCTOBER).withDayOfMonth(1);
}
return startDate;
}
long getYearDifference(long minuendInstant, long subtrahendInstant) {
int minuendYear = getYear(minuendInstant);
int subtrahendYear = getYear(subtrahendInstant);
// Inlined remainder method to avoid duplicate calls to get.
long minuendRem = minuendInstant - getYearMillis(minuendYear);
long subtrahendRem = subtrahendInstant - getYearMillis(subtrahendYear);
// Balance leap year differences on remainders.
if (subtrahendRem >= FEB_29) {
if (isLeapYear(subtrahendYear)) {
if (!isLeapYear(minuendYear)) {
subtrahendRem -= DateTimeConstants.MILLIS_PER_DAY;
}
} else if (minuendRem >= FEB_29 && isLeapYear(minuendYear)) {
minuendRem -= DateTimeConstants.MILLIS_PER_DAY;
}
}
int difference = minuendYear - subtrahendYear;
if (minuendRem < subtrahendRem) {
difference--;
}
return difference;
}
public long getDateTimeMillis(
int year, int monthOfYear, int dayOfMonth,
int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond)
throws IllegalArgumentException {
Chronology base;
if ((base = getBase()) != null) {
return base.getDateTimeMillis(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
}
FieldUtils.verifyValueBounds(DateTimeFieldType.hourOfDay(), hourOfDay, 0, 23);
FieldUtils.verifyValueBounds(DateTimeFieldType.minuteOfHour(), minuteOfHour, 0, 59);
FieldUtils.verifyValueBounds(DateTimeFieldType.secondOfMinute(), secondOfMinute, 0, 59);
FieldUtils.verifyValueBounds(DateTimeFieldType.millisOfSecond(), millisOfSecond, 0, 999);
return getDateMidnightMillis(year, monthOfYear, dayOfMonth)
+ hourOfDay * DateTimeConstants.MILLIS_PER_HOUR
+ minuteOfHour * DateTimeConstants.MILLIS_PER_MINUTE
+ secondOfMinute * DateTimeConstants.MILLIS_PER_SECOND
+ millisOfSecond;
}
public static Collection<DateRange> splitAsDateRangeByWeek(LocalDate start, LocalDate end){
Set<DateRange> dates = new LinkedHashSet<DateRange>();
dates.add(new DateRange(start, start.withDayOfWeek(DateTimeConstants.SUNDAY)));
LocalDate startDateOfWeek = start.withDayOfWeek(DateTimeConstants.MONDAY).plusWeeks(1);
while(!startDateOfWeek.isAfter(end)){
LocalDate endDateOfWeek = startDateOfWeek.withDayOfWeek(DateTimeConstants.SUNDAY);
if(endDateOfWeek.isAfter(end)){
endDateOfWeek = end;
}
dates.add(new DateRange(startDateOfWeek, endDateOfWeek));
startDateOfWeek = startDateOfWeek.plusWeeks(1);
}
return dates;
}
protected int monthOfYear(int periodNumber) {
if (periodNumber == 1) {
return DateTimeConstants.JANUARY;
} else {
return DateTimeConstants.JULY;
}
}
public void testMaximumValue() {
YearMonthDay ymd1 = new YearMonthDay(1999, DateTimeConstants.FEBRUARY, 1);
DateMidnight dm1 = new DateMidnight(1999, DateTimeConstants.FEBRUARY, 1);
Chronology chrono = GregorianChronology.getInstance();
assertEquals(28, chrono.dayOfMonth().getMaximumValue(ymd1));
assertEquals(28, chrono.dayOfMonth().getMaximumValue(dm1.getMillis()));
}
@Test
public void testCanNotEditPreviousPeriodEndsSameTodayMinusDifferenceMinusOneExpiryDays() {
LocalDate periodDate = new LocalDate();
periodDate = periodDate.minusWeeks(1).withDayOfWeek(DateTimeConstants.MONDAY);
int expiryDays = Days.daysBetween(periodDate.plusDays(6), new LocalDate()).getDays() - 1;
WeeklyExpiryDayValidator weeklyExpiryDayValidator = new WeeklyExpiryDayValidator(expiryDays,
periodDate.toString(PATTERN));
assertFalse(weeklyExpiryDayValidator.canEdit());
}
@Test
public void testCanNotEditPreviousPeriodEndsSameTodayMinusDifferenceMinusOneExpiryDays() {
LocalDate periodDate = new LocalDate();
periodDate = periodDate.minusYears(1).withMonthOfYear(
DateTimeConstants.JANUARY).withDayOfMonth(1);
int expiryDays = Days.daysBetween(periodDate.plusYears(1), new LocalDate()).getDays() - 1;
YearlyExpiryDayValidator yearlyExpiryDayValidator = new YearlyExpiryDayValidator(
expiryDays,
periodDate.toString(PATTERN));
assertFalse(yearlyExpiryDayValidator.canEdit());
}
public void printTo(Writer out, ReadablePeriod period, Locale locale) throws IOException {
long valueLong = getFieldValue(period);
if (valueLong == Long.MAX_VALUE) {
return;
}
int value = (int) valueLong;
if (iFieldType >= SECONDS_MILLIS) {
value = (int) (valueLong / DateTimeConstants.MILLIS_PER_SECOND);
}
if (iPrefix != null) {
iPrefix.printTo(out, value);
}
int minDigits = iMinPrintedDigits;
if (minDigits <= 1) {
FormatUtils.writeUnpaddedInteger(out, value);
} else {
FormatUtils.writePaddedInteger(out, value, minDigits);
}
if (iFieldType >= SECONDS_MILLIS) {
int dp = (int) (Math.abs(valueLong) % DateTimeConstants.MILLIS_PER_SECOND);
if (iFieldType == SECONDS_MILLIS || dp > 0) {
out.write('.');
FormatUtils.writePaddedInteger(out, dp, 3);
}
}
if (iSuffix != null) {
iSuffix.printTo(out, value);
}
}
private static DateTime fixEndDate(DateTime endDate) {
if(endDate==null) {
return null;
}
int month = endDate.getMonthOfYear();
if (month >= 4 && month <= 9) {
return endDate.withMonthOfYear(DateTimeConstants.SEPTEMBER).withDayOfMonth(30);
}else if( month>9) {
return endDate.withYear(endDate.getYear() +1).withMonthOfYear(
DateTimeConstants.MARCH).withDayOfMonth(31);
}else{
return endDate.withMonthOfYear(
DateTimeConstants.MARCH).withDayOfMonth(31);
}
}
/**
* @param instant millis from 1970-01-01T00:00:00Z
*/
int getYear(long instant) {
// Get an initial estimate of the year, and the millis value that
// represents the start of that year. Then verify estimate and fix if
// necessary.
// Initial estimate uses values divided by two to avoid overflow.
long unitMillis = getAverageMillisPerYearDividedByTwo();
long i2 = (instant >> 1) + getApproxMillisAtEpochDividedByTwo();
if (i2 < 0) {
i2 = i2 - unitMillis + 1;
}
int year = (int) (i2 / unitMillis);
long yearStart = getYearMillis(year);
long diff = instant - yearStart;
if (diff < 0) {
year--;
} else if (diff >= DateTimeConstants.MILLIS_PER_DAY * 365L) {
// One year may need to be added to fix estimate.
long oneYear;
if (isLeapYear(year)) {
oneYear = DateTimeConstants.MILLIS_PER_DAY * 366L;
} else {
oneYear = DateTimeConstants.MILLIS_PER_DAY * 365L;
}
yearStart += oneYear;
if (yearStart <= instant) {
// Didn't go too far, so actually add one year.
year++;
}
}
return year;
}
@Override
public void addInt(int value) {
if (value > ParquetReaderUtility.DATE_CORRUPTION_THRESHOLD) {
holder.value = (value - ParquetReaderUtility.CORRECT_CORRUPT_DATE_SHIFT) * DateTimeConstants.MILLIS_PER_DAY;
} else {
holder.value = value * (long) DateTimeConstants.MILLIS_PER_DAY;
}
writer.write(holder);
setWritten();
}
@Test
public void testGetOffset_long() {
DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
assertEquals(2L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffset(TEST_TIME_SUMMER));
assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffset(TEST_TIME_WINTER));
assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getStandardOffset(TEST_TIME_SUMMER));
assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getStandardOffset(TEST_TIME_WINTER));
assertEquals(2L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffsetFromLocal(TEST_TIME_SUMMER));
assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffsetFromLocal(TEST_TIME_WINTER));
assertEquals(false, zone.isStandardOffset(TEST_TIME_SUMMER));
assertEquals(true, zone.isStandardOffset(TEST_TIME_WINTER));
}
@Override
void addNext(int start, int index) {
int intValue;
if (usingDictionary) {
intValue = pageReader.dictionaryValueReader.readInteger();
} else {
intValue = readIntLittleEndian(bytebuf, start);
}
if (intValue > ParquetReaderUtility.DATE_CORRUPTION_THRESHOLD) {
dateVector.set(index, (intValue - ParquetReaderUtility.CORRECT_CORRUPT_DATE_SHIFT) * DateTimeConstants.MILLIS_PER_DAY);
} else {
dateVector.set(index, intValue * (long) DateTimeConstants.MILLIS_PER_DAY);
}
}
@Override
void addNext(int start, int index) {
int intValue;
if (usingDictionary) {
intValue = pageReader.dictionaryValueReader.readInteger();
} else {
intValue = readIntLittleEndian(bytebuf, start);
}
if (intValue > ParquetReaderUtility.DATE_CORRUPTION_THRESHOLD) {
vector.set(index, (intValue - ParquetReaderUtility.CORRECT_CORRUPT_DATE_SHIFT) * DateTimeConstants.MILLIS_PER_DAY);
} else {
vector.set(index, intValue * (long) DateTimeConstants.MILLIS_PER_DAY);
}
}
/**
* @param instant millis from 1970-01-01T00:00:00Z
*/
int getWeekyear(long instant) {
int year = getYear(instant);
int week = getWeekOfWeekyear(instant, year);
if (week == 1) {
return getYear(instant + DateTimeConstants.MILLIS_PER_WEEK);
} else if (week > 51) {
return getYear(instant - (2 * DateTimeConstants.MILLIS_PER_WEEK));
} else {
return year;
}
}
public void testSetInto_Object() throws Exception {
MutablePeriod m = new MutablePeriod(PeriodType.yearMonthDayTime());
ReadableDurationConverter.INSTANCE.setInto(m, new Duration(
3L * DateTimeConstants.MILLIS_PER_DAY +
4L * DateTimeConstants.MILLIS_PER_MINUTE + 5L
), null);
assertEquals(0, m.getYears());
assertEquals(0, m.getMonths());
assertEquals(0, m.getWeeks());
assertEquals(0, m.getDays());
assertEquals(3 * 24, m.getHours());
assertEquals(4, m.getMinutes());
assertEquals(0, m.getSeconds());
assertEquals(5, m.getMillis());
}
private int getPreviousPeriodStart() {
int nowMonthOfYear = new LocalDate().getMonthOfYear();
if (nowMonthOfYear >= DateTimeConstants.OCTOBER) {
return PREVIOUS_PERIOD_START_OCTOBER;
} else if (nowMonthOfYear >= DateTimeConstants.JULY) {
return PREVIOUS_PERIOD_START_JULY;
} else if (nowMonthOfYear >= DateTimeConstants.APRIL) {
return PREVIOUS_PERIOD_START_APRIL;
} else {
return PREVIOUS_PERIOD_START_JANUARY;
}
}
public long getDateTimeMillis(
int year, int monthOfYear, int dayOfMonth, int millisOfDay)
throws IllegalArgumentException {
Chronology base;
if ((base = getBase()) != null) {
return base.getDateTimeMillis(year, monthOfYear, dayOfMonth, millisOfDay);
}
FieldUtils.verifyValueBounds
(DateTimeFieldType.millisOfDay(), millisOfDay, 0, DateTimeConstants.MILLIS_PER_DAY);
return getDateMidnightMillis(year, monthOfYear, dayOfMonth) + millisOfDay;
}
@Test
public void testCanEditPreviousPeriodEndsSameTodayMinusDifferencePlusTwoExpiryDays() {
LocalDate periodDate = new LocalDate();
periodDate = periodDate.minusYears(1).withMonthOfYear(
DateTimeConstants.OCTOBER).withDayOfMonth(1);
int expiryDays = Days.daysBetween(periodDate.plusYears(1), new LocalDate()).getDays() + 2;
FinancialYearOctExpiryDayValidator yearlyExpiryDayValidator =
new FinancialYearOctExpiryDayValidator(
expiryDays,
periodDate.toString(PATTERN));
assertTrue(yearlyExpiryDayValidator.canEdit());
}
@Test
public void testCanNotEditPreviousPeriodEndsSameTodayMinusDifferenceMinusOneExpiryDays() {
LocalDate periodDate = new LocalDate();
periodDate = periodDate.minusWeeks(1).withDayOfWeek(DateTimeConstants.SATURDAY);
int expiryDays = Days.daysBetween(periodDate.plusDays(6), new LocalDate()).getDays() - 1;
WeeklySaturdayExpiryDayValidator weeklyExpiryDayValidator =
new WeeklySaturdayExpiryDayValidator(expiryDays,
periodDate.toString(PATTERN));
assertFalse(weeklyExpiryDayValidator.canEdit());
}
@Test
public void testCanEditPreviousPeriodEndsSameTodayMinusDifferencePlusTwoExpiryDays() {
LocalDate periodDate = new LocalDate();
periodDate = periodDate.minusYears(1).withMonthOfYear(
DateTimeConstants.JANUARY).withDayOfMonth(1);
int expiryDays = Days.daysBetween(periodDate.plusYears(1), new LocalDate()).getDays() + 2;
YearlyExpiryDayValidator yearlyExpiryDayValidator = new YearlyExpiryDayValidator(
expiryDays,
periodDate.toString(PATTERN));
assertTrue(yearlyExpiryDayValidator.canEdit());
}
@Override
public ArrayList<DateHolder> next() {
cPeriod = cPeriod.plusYears(1);
cPeriod = cPeriod.plusMonths(1);
cPeriod = cPeriod.withDayOfYear(1).withDayOfWeek(DateTimeConstants.THURSDAY);
return generatePeriod();
}