下面列出了怎么用org.joda.time.DateMidnight的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public RexNode convertCall(SqlRexContext cx, SqlCall call) {
final int timeZoneIndex = getContextInformation().getRootFragmentTimeZone();
final DateTimeZone timeZone = DateTimeZone.forID(JodaDateUtility.getTimeZone(timeZoneIndex));
final LocalDateTime dateTime = new LocalDateTime(getContextInformation().getQueryStartTime(), timeZone);
final long midNightAsMillis =
new DateMidnight(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth(),
timeZone)
.withZoneRetainFields(DateTimeZone.UTC)
.getMillis();
return cx.getRexBuilder()
.makeDateLiteral(DateTimes.toDateTime(
new LocalDateTime(midNightAsMillis, DateTimeZone.UTC))
.toCalendar(null)); // null sets locale to default locale
}
public void testBigHashtable() {
Converter[] array = new Converter[] {
c1, c2, c3, c4,
};
ConverterSet set = new ConverterSet(array);
set.select(Boolean.class);
set.select(Character.class);
set.select(Byte.class);
set.select(Short.class);
set.select(Integer.class);
set.select(Long.class);
set.select(Float.class);
set.select(Double.class);
set.select(null);
set.select(Calendar.class);
set.select(GregorianCalendar.class);
set.select(DateTime.class);
set.select(DateMidnight.class);
set.select(ReadableInstant.class);
set.select(ReadableDateTime.class);
set.select(ReadWritableInstant.class); // 16
set.select(ReadWritableDateTime.class);
set.select(DateTime.class);
assertEquals(4, set.size());
}
public void testBigHashtable() {
Converter[] array = new Converter[] {
c1, c2, c3, c4,
};
ConverterSet set = new ConverterSet(array);
set.select(Boolean.class);
set.select(Character.class);
set.select(Byte.class);
set.select(Short.class);
set.select(Integer.class);
set.select(Long.class);
set.select(Float.class);
set.select(Double.class);
set.select(null);
set.select(Calendar.class);
set.select(GregorianCalendar.class);
set.select(DateTime.class);
set.select(DateMidnight.class);
set.select(ReadableInstant.class);
set.select(ReadableDateTime.class);
set.select(ReadWritableInstant.class); // 16
set.select(ReadWritableDateTime.class);
set.select(DateTime.class);
assertEquals(4, set.size());
}
@SuppressWarnings("deprecation")
@Test
public void testIsSnapshotRequired() throws Exception {
StreamService service = new StreamService();
Period period = Period.days(2);
Collection<Snapshot> snapshots = new ArrayList<>();
snapshots.add(new Snapshot(0, new DateTime(2014, 1, 1, 0, 0), null));
snapshots.add(new Snapshot(0, new DateTime(2014, 1, 2, 0, 0), null));
snapshots.add(new Snapshot(0, new DateTime(2014, 1, 3, 0, 0), null));
assertThat(service.isSnapshotRequired(
new DateMidnight(2014, 1, 3).toInstant(), period, snapshots),
is(false));
assertThat(service.isSnapshotRequired(
new DateMidnight(2014, 1, 4).toInstant(), period, snapshots),
is(false));
assertThat(service.isSnapshotRequired(
new DateMidnight(2014, 1, 5).toInstant(), period, snapshots),
is(true));
}
public CurricularYearWeeklyWorkLoadView(final DegreeCurricularPlan degreeCurricularPlan,
final ExecutionSemester executionSemester, final Set<ExecutionCourse> executionCourses) {
final ExecutionDegree executionDegree = findExecutionDegree(executionSemester, degreeCurricularPlan);
if (executionDegree != null) {
this.interval =
new Interval(new DateMidnight(getBegginingOfLessonPeriod(executionSemester, executionDegree)),
new DateMidnight(getEndOfExamsPeriod(executionSemester, executionDegree)));
final Period period = interval.toPeriod();
int extraWeek = period.getDays() > 0 ? 1 : 0;
numberOfWeeks = (period.getYears() * 12 + period.getMonths()) * 4 + period.getWeeks() + extraWeek + 1;
intervals = new Interval[numberOfWeeks];
for (int i = 0; i < numberOfWeeks; i++) {
final DateTime start = interval.getStart().plusWeeks(i);
final DateTime end = start.plusWeeks(1);
intervals[i] = new Interval(start, end);
}
this.executionCourses.addAll(executionCourses);
}
}
/**
* @see org.projectforge.web.wicket.AbstractSecuredPage#onBeforeRender()
*/
@Override
protected void onBeforeRender()
{
super.onBeforeRender();
// Restore current date (e. g. on reload or on coming back from callee page).
final MyFullCalendarConfig config = calendar.getConfig();
final DateMidnight startDate = filter.getStartDate();
if (startDate != null) {
config.setYear(startDate.getYear());
config.setMonth(startDate.getMonthOfYear() - 1);
config.setDate(startDate.getDayOfMonth());
}
config.setDefaultView(filter.getViewType().getCode());
if (refresh == true) {
refresh = false;
timesheetEventsProvider.forceReload();
birthdayEventsProvider.forceReload();
hrPlanningEventsProvider.forceReload();
setConfig();
onRefreshEventProvider();
}
}
@Override
protected void respond(final AjaxRequestTarget target) {
final Request r = target.getPage().getRequest();
final ViewType type = ViewType.forCode(r.getRequestParameters().getParameterValue("view").toString());
final DateTimeFormatter fmt = ISODateTimeFormat.dateTimeParser().withZone(PFUserContext.getDateTimeZone());
final DateMidnight start = fmt.parseDateTime(r.getRequestParameters().getParameterValue("start").toString())
.toDateMidnight();
final DateMidnight end = fmt.parseDateTime(r.getRequestParameters().getParameterValue("end").toString())
.toDateMidnight();
final DateMidnight visibleStart = fmt.parseDateTime(
r.getRequestParameters().getParameterValue("visibleStart").toString()).toDateMidnight();
final DateMidnight visibleEnd = fmt
.parseDateTime(r.getRequestParameters().getParameterValue("visibleEnd").toString()).toDateMidnight();
final View view = new View(type, start, end, visibleStart, visibleEnd);
final CalendarResponse response = new CalendarResponse(getCalendar(), target);
onViewDisplayed(view, response);
}
@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));
}
/**
* @param property bean property which must have a read method
* @return adapter with a null annotation, or null if nothing applicable
*/
static StorablePropertyAdapter selectAdapterFor(final BeanProperty property) {
final Method readMethod = property.getReadMethod();
if (readMethod == null) {
throw new IllegalArgumentException();
}
final Class propertyType = property.getType();
if (DateTime.class.isAssignableFrom(propertyType) ||
DateMidnight.class.isAssignableFrom(propertyType) ||
LocalDate.class.isAssignableFrom(propertyType) ||
LocalDateTime.class.isAssignableFrom(propertyType) ||
java.util.Date.class.isAssignableFrom(propertyType))
{
return selectAdapter(property, DateTimeAdapter.class, readMethod);
} else if (String.class.isAssignableFrom(propertyType)) {
return selectAdapter(property, TextAdapter.class, readMethod);
} // else if ...
return null;
}
/**
* test issue 109
*/
public void test109() {
Date someDate = new DateMidnight(9, 2, 21, DateTimeZone.forID("Europe/Amsterdam")).toDate();
Yaml yaml = new Yaml();
String timestamp = yaml.dump(someDate);
assertEquals("0009-02-22T23:40:28Z\n", timestamp);
// System.out.println(timestamp);
Object o = yaml.load(timestamp);
assertEquals(someDate, o);
}
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()));
}
public void testMaximumValue() {
DateMidnight dt = new DateMidnight(1570, 1, 1, GJChronology.getInstance());
while (dt.getYear() < 1590) {
dt = dt.plusDays(1);
YearMonthDay ymd = dt.toYearMonthDay();
assertEquals(dt.year().getMaximumValue(), ymd.year().getMaximumValue());
assertEquals(dt.monthOfYear().getMaximumValue(), ymd.monthOfYear().getMaximumValue());
assertEquals(dt.dayOfMonth().getMaximumValue(), ymd.dayOfMonth().getMaximumValue());
}
}
public void testLeapYearRulesConstruction() {
// 1500 not leap in Gregorian, but is leap in Julian
DateMidnight dt = new DateMidnight(1500, 2, 29, GJChronology.getInstanceUTC());
assertEquals(dt.getYear(), 1500);
assertEquals(dt.getMonthOfYear(), 2);
assertEquals(dt.getDayOfMonth(), 29);
}
public void testLeapYearRulesConstructionInvalid() {
// 1500 not leap in Gregorian, but is leap in Julian
try {
new DateMidnight(1500, 2, 30, GJChronology.getInstanceUTC());
fail();
} catch (IllegalFieldValueException ex) {
// good
}
}
public void testMaximumValue() {
DateMidnight dt = new DateMidnight(1570, 1, 1);
while (dt.getYear() < 1590) {
dt = dt.plusDays(1);
YearMonthDay ymd = dt.toYearMonthDay();
assertEquals(dt.year().getMaximumValue(), ymd.year().getMaximumValue());
assertEquals(dt.monthOfYear().getMaximumValue(), ymd.monthOfYear().getMaximumValue());
assertEquals(dt.dayOfMonth().getMaximumValue(), ymd.dayOfMonth().getMaximumValue());
}
}
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()));
}
public void testMaximumValue() {
DateMidnight dt = new DateMidnight(1570, 1, 1, GJChronology.getInstance());
while (dt.getYear() < 1590) {
dt = dt.plusDays(1);
YearMonthDay ymd = dt.toYearMonthDay();
assertEquals(dt.year().getMaximumValue(), ymd.year().getMaximumValue());
assertEquals(dt.monthOfYear().getMaximumValue(), ymd.monthOfYear().getMaximumValue());
assertEquals(dt.dayOfMonth().getMaximumValue(), ymd.dayOfMonth().getMaximumValue());
}
}
public void testLeapYearRulesConstruction() {
// 1500 not leap in Gregorian, but is leap in Julian
DateMidnight dt = new DateMidnight(1500, 2, 29, GJChronology.getInstanceUTC());
assertEquals(dt.getYear(), 1500);
assertEquals(dt.getMonthOfYear(), 2);
assertEquals(dt.getDayOfMonth(), 29);
}
public void testLeapYearRulesConstructionInvalid() {
// 1500 not leap in Gregorian, but is leap in Julian
try {
new DateMidnight(1500, 2, 30, GJChronology.getInstanceUTC());
fail();
} catch (IllegalFieldValueException ex) {
// good
}
}
public void testMaximumValue() {
DateMidnight dt = new DateMidnight(1570, 1, 1);
while (dt.getYear() < 1590) {
dt = dt.plusDays(1);
YearMonthDay ymd = dt.toYearMonthDay();
assertEquals(dt.year().getMaximumValue(), ymd.year().getMaximumValue());
assertEquals(dt.monthOfYear().getMaximumValue(), ymd.monthOfYear().getMaximumValue());
assertEquals(dt.dayOfMonth().getMaximumValue(), ymd.dayOfMonth().getMaximumValue());
}
}
@Test
public void testHistoricState() {
HistoricItem historicItem = PersistenceExtensions.historicState(item, new DateMidnight(2012, 1, 1), "test");
assertEquals("2012", historicItem.getState().toString());
historicItem = PersistenceExtensions.historicState(item, new DateMidnight(2011, 12, 31), "test");
assertEquals("2011", historicItem.getState().toString());
historicItem = PersistenceExtensions.historicState(item, new DateMidnight(2011, 1, 1), "test");
assertEquals("2011", historicItem.getState().toString());
historicItem = PersistenceExtensions.historicState(item, new DateMidnight(2000, 1, 1), "test");
assertEquals("2000", historicItem.getState().toString());
}
@Test
public void testMinimumSince() {
item.setState(new DecimalType(5000));
HistoricItem historicItem = PersistenceExtensions.minimumSince(item, new DateMidnight(1940, 1, 1), "test");
assertNotNull(historicItem);
assertEquals("5000", historicItem.getState().toString());
historicItem = PersistenceExtensions.minimumSince(item, new DateMidnight(2005, 1, 1), "test");
assertEquals("2005", historicItem.getState().toString());
assertEquals(new DateMidnight(2005, 1, 1).toDate(), historicItem.getTimestamp());
}
@Test
public void testMaximumSince() {
item.setState(new DecimalType(1));
HistoricItem historicItem = PersistenceExtensions.maximumSince(item, new DateMidnight(2012, 1, 1), "test");
assertNotNull(historicItem);
assertEquals("1", historicItem.getState().toString());
historicItem = PersistenceExtensions.maximumSince(item, new DateMidnight(2005, 1, 1), "test");
assertEquals("2012", historicItem.getState().toString());
assertEquals(new DateMidnight(2012, 1, 1).toDate(), historicItem.getTimestamp());
}
@Test
public void testAverageSince() {
item.setState(new DecimalType(3025));
DateMidnight startStored = new DateMidnight(2003, 1, 1);
DateMidnight endStored = new DateMidnight(2012, 1, 1);
long storedInterval = endStored.getMillis() - startStored.getMillis();
long recentInterval = DateTime.now().getMillis() - endStored.getMillis();
double expected = (2007.4994 * storedInterval + 2518.5 * recentInterval) / (storedInterval + recentInterval);
DecimalType average = PersistenceExtensions.averageSince(item, startStored, "test");
assertEquals(expected, average.doubleValue(), 0.01);
}
@Test public void testExtractDocumentAttributes() throws ParseException {
//Filling in a random document type name... Revisit
String documentTypeName = "SearchDocType";
String key = "givenname";
String value = "jack";
assertExtractDocumentAttributes(documentTypeName, "XMLSearchableAttribute", key, value, "<putWhateverWordsIwantInsideThisTag>" + "<" + key + ">" + "<value>" + value + "</value>" + "</" + key+ ">" + "</putWhateverWordsIwantInsideThisTag>");
// test general operation
key = "testLongKey";
value = "123458";
assertExtractDocumentAttributes(documentTypeName, "XMLSearchableAttributeStdLong", key, new BigInteger(value), "<putWhateverWordsIwantInsideThisTag>" + "<" + key + ">" + "<value>" + value + "</value>" + "</" + key+ ">" + "</putWhateverWordsIwantInsideThisTag>");
// test operation with leading and trailing spaces in xml doc content
assertExtractDocumentAttributes(documentTypeName, "XMLSearchableAttributeStdLong", key, new BigInteger(value), "<putWhateverWordsIwantInsideThisTag>" + "<" + key + ">" + "<value>" + " " + value + " " + "</value>" + "</" + key + ">" + "</putWhateverWordsIwantInsideThisTag>");
key = "testFloatKey";
value = "2568.204154796";
assertExtractDocumentAttributes(documentTypeName, "XMLSearchableAttributeStdFloat", key, new BigDecimal(value), "<putWhateverWordsIwantInsideThisTag>" + "<" + key+ ">" + "<value>" + value + "</value>" + "</" + key + ">" + "</putWhateverWordsIwantInsideThisTag>");
key = "testDateTimeKey";
value = DocumentSearchInternalUtils.getDisplayValueWithDateOnly(TestXMLSearchableAttributeDateTime.SEARCH_STORAGE_VALUE);
// value is coerced to Date without time
DateMidnight expected = TestXMLSearchableAttributeDateTime.SEARCH_STORAGE_VALUE.toDateMidnight();
assertExtractDocumentAttributes(documentTypeName, "XMLSearchableAttributeStdDateTime", key, expected, "<putWhateverWordsIwantInsideThisTag>" + "<" + key + ">" + "<value>" + value + "</value>" + "</" + key + ">" + "</putWhateverWordsIwantInsideThisTag>");
// test for kuali xstream formatted dates
value = "02/20/2007";
String returnValue = "02/20/2007";
// value is coerced to Date without time
Date d = RiceConstants.getDefaultDateFormat().parse(returnValue);
expected = new DateTime(d.getTime()).toDateMidnight();
assertExtractDocumentAttributes(documentTypeName, "XMLSearchableAttributeStdDateTime", key, expected, "<putWhateverWordsIwantInsideThisTag>" + "<" + key + ">" + "<value>" + value + "</value>" + "</" + key + ">" + "</putWhateverWordsIwantInsideThisTag>");
}
public String getPresentationInterval() {
final DateTime start = interval.getStart();
final DateTime end = interval.getEnd();
final DateMidnight dateMidnight = start.toDateMidnight();
if (dateMidnight.equals(end.toDateMidnight())) {
return String.format("%s : %s as %s", dateMidnight.toString(DATE_FORMAT), getTime(start), getTime(end));
}
return interval.toString();
}
public DateMidnight getDateDay() {
final DateTime start = interval.getStart();
final DateTime end = interval.getEnd();
final DateMidnight dateMidnight = start.toDateMidnight();
if (dateMidnight.equals(end.toDateMidnight())) {
return dateMidnight;
}
return null;
}
public String getDay() {
final DateMidnight dateDay = getDateDay();
if (dateDay == null) {
return String.format("%s - %s", interval.getStart().toString("dd/MM/yyyy"), interval.getEnd().toString("dd/MM/yyyy"));
}
return dateDay.toString("dd/MM/yyyy");
}
public Interval getCurrentWeek() {
final DateMidnight beginningOfSemester = new DateMidnight(getBegginingOfLessonPeriod());
final DateMidnight firstMonday = beginningOfSemester.withField(DateTimeFieldType.dayOfWeek(), 1);
final int currentWeek = calculateCurrentWeekOffset();
final DateMidnight start = firstMonday.plusWeeks(currentWeek);
return new Interval(start, start.plusWeeks(1));
}
public Interval getResponseWeek() {
final DateMidnight beginningOfSemester = new DateMidnight(getBegginingOfLessonPeriod());
final DateMidnight firstMonday = beginningOfSemester.withField(DateTimeFieldType.dayOfWeek(), 1);
final DateMidnight secondMonday = firstMonday.plusWeeks(1);
final DateMidnight endOfSemester = new DateMidnight(getEndOfExamsPeriod());
final DateMidnight lastMonday = endOfSemester.withField(DateTimeFieldType.dayOfWeek(), 1);
final DateMidnight endOfResponsePeriod = lastMonday.plusWeeks(2);
return (secondMonday.isEqualNow() || secondMonday.isBeforeNow()) && endOfResponsePeriod.isAfterNow() ? getPreviousWeek() : null;
}