下面列出了java.sql.Timestamp#compareTo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @see org.kuali.kfs.module.purap.document.service.PaymentRequestService#isInvoiceDateAfterToday(java.sql.Date)
*/
@Override
@NonTransactional
public boolean isInvoiceDateAfterToday(Date invoiceDate) {
// Check invoice date to make sure it is today or before
Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR, 11);
now.set(Calendar.MINUTE, 59);
now.set(Calendar.SECOND, 59);
now.set(Calendar.MILLISECOND, 59);
Timestamp nowTime = new Timestamp(now.getTimeInMillis());
Calendar invoiceDateC = Calendar.getInstance();
invoiceDateC.setTime(invoiceDate);
// set time to midnight
invoiceDateC.set(Calendar.HOUR, 0);
invoiceDateC.set(Calendar.MINUTE, 0);
invoiceDateC.set(Calendar.SECOND, 0);
invoiceDateC.set(Calendar.MILLISECOND, 0);
Timestamp invoiceDateTime = new Timestamp(invoiceDateC.getTimeInMillis());
return ((invoiceDateTime.compareTo(nowTime)) > 0);
}
@Override
public int compare(Object o1, Object o2) {
if (o1 == o2) {
return 0;
}
if (o1 == null) {
return -1;
}
if (o2 == null) {
return 1;
}
Timestamp d1 = convertFrom(o1);
Timestamp d2 = convertFrom(o2);
return d1.compareTo(d2);
}
/**
* @see org.kuali.kfs.module.purap.document.service.PurapService#isDateMoreThanANumberOfDaysAway(java.sql.Date, int)
*/
@Override
public boolean isDateMoreThanANumberOfDaysAway(Date compareDate, int daysAway) {
LOG.debug("isDateMoreThanANumberOfDaysAway() started");
Date todayAtMidnight = dateTimeService.getCurrentSqlDateMidnight();
Calendar daysAwayCalendar = dateTimeService.getCalendar(todayAtMidnight);
daysAwayCalendar.add(Calendar.DATE, daysAway);
Timestamp daysAwayTime = new Timestamp(daysAwayCalendar.getTime().getTime());
Calendar compareCalendar = dateTimeService.getCalendar(compareDate);
compareCalendar.set(Calendar.HOUR, 0);
compareCalendar.set(Calendar.MINUTE, 0);
compareCalendar.set(Calendar.SECOND, 0);
compareCalendar.set(Calendar.MILLISECOND, 0);
compareCalendar.set(Calendar.AM_PM, Calendar.AM);
Timestamp compareTime = new Timestamp(compareCalendar.getTime().getTime());
return (compareTime.compareTo(daysAwayTime) > 0);
}
public int compare(Object gradebook, Object otherGradebook) {
Timestamp modDate1 = ((Gradebook) gradebook).getLastUpdated();
Timestamp modDate2 = ((Gradebook) otherGradebook).getLastUpdated();
if(modDate1.equals(modDate2)) {
return compareTitles((Gradebook) gradebook, (Gradebook)otherGradebook);
}
return modDate2.compareTo(modDate1);
}
/**
* Determines if the disbursement date is past the number of days old (configured in system parameter) in which actions can take
* place
*
* @return true if actions are allowed on disbursement, false otherwise
*/
public boolean isDisbursementActionAllowed() {
if (paymentGroup.getDisbursementDate() == null) {
if (PdpConstants.PaymentStatusCodes.EXTRACTED.equals(paymentGroup.getPaymentStatus().getCode())) {
return false;
}
return true;
}
String daysStr = SpringContext.getBean(ParameterService.class).getParameterValueAsString(PaymentDetail.class, PdpParameterConstants.DISBURSEMENT_CANCELLATION_DAYS);
int days = Integer.valueOf(daysStr);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, (days * -1));
c.set(Calendar.HOUR, 12);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
c.set(Calendar.AM_PM, Calendar.AM);
Timestamp lastDisbursementActionDate = new Timestamp(c.getTimeInMillis());
Calendar c2 = Calendar.getInstance();
c2.setTime(paymentGroup.getDisbursementDate());
c2.set(Calendar.HOUR, 11);
c2.set(Calendar.MINUTE, 59);
c2.set(Calendar.SECOND, 59);
c2.set(Calendar.MILLISECOND, 59);
c2.set(Calendar.AM_PM, Calendar.PM);
Timestamp disbursementDate = new Timestamp(c2.getTimeInMillis());
// date is equal to or after lastActionDate Allowed
return ((disbursementDate.compareTo(lastDisbursementActionDate)) >= 0);
}
/**
*
* This method calculates the difference in days between the two timestamps provided.
*
* This method is used instead of KfsDateUtils.getDifferenceInDays() because there is a rounding issue within the timestamp that exists which must be dealt with to
* prevent improper calculations. This issue is similar to the problems that exist with adding and subtracting doubles and the inherently bad way that Java handles
* numbers.
*
* The approach used within KfsDateUtils does not offer enough accuracy to calculate the difference consistently and accurately.
*
* @param t1
* @param t2
* @return The difference in days between the two given timestamps.
*/
public static long getDifferenceInDays (Timestamp t1, Timestamp t2) {
// Make sure the result is always > 0
if (t1.compareTo (t2) < 0) {
Timestamp tmp = t1;
t1 = t2;
t2 = tmp;
}
// Timestamps mix milli and nanoseconds in the API, so we have to separate the two
long diffSeconds = (t1.getTime () / 1000) - (t2.getTime () / 1000);
// For normals dates, we have millisecond precision
int nano1 = ((int) t1.getTime () % 1000) * 1000000;
nano1 = t1.getNanos ();
int nano2 = ((int) t2.getTime () % 1000) * 1000000;
nano2 = t2.getNanos ();
int diffNanos = nano1 - nano2;
if (diffNanos < 0) {
// Borrow one second
diffSeconds --;
diffNanos += 1000000000;
}
// mix nanos and millis again
Timestamp result = new Timestamp ((diffSeconds * 1000) + (diffNanos / 1000000));
// setNanos() with a value of in the millisecond range doesn't affect the value of the time field
// while milliseconds in the time field will modify nanos! Damn, this API is a *mess*
result.setNanos (diffNanos);
return result.getDate();
}
public int compare(FuzzySearchResult a, FuzzySearchResult b) {
int a1 = a.getSimilarity();
int b1 = b.getSimilarity();
if (a1 < b1) {
return 1;
} else if (a1 == b1) {
if (sortStrategy == TMPreferenceConstants.DEFAULT_DB_PRECEDENCE) {
// sort by default database
String dbName = b.getDbName();
if (dbName.equals(deffaultTm)) {
return -1;
}
} else if (sortStrategy == TMPreferenceConstants.DATE_REVERSE_PRECEDENCE) {
// sort by update date
String t1Str = a.getTu().getChangeDate();
String t2Str = b.getTu().getChangeDate();
if (t1Str.length() != 0 && t2Str.length() != 0) {
Timestamp t1 = DateUtils.getTimestampFromUTC(t1Str);
Timestamp t2 = DateUtils.getTimestampFromUTC(t2Str);
return t2.compareTo(t1);
} else if (t1Str.length() == 0 && t2Str.length() != 0) {
return -1;
}
}
return 0;
} else {
return -1;
}
}
public int compareObject(Timestamp o1, Timestamp o2) {
if (o1 == null && o2 == null) {
return 0;
} else if (o1 != null) {
return o1.compareTo(o2);
} else {
return -1;
}
}
private static void findUpcomingRenewals(Timestamp now, Course course, Set<Course> result, boolean skip, Set<Long> participatingCourseIds, Set<Long> courseIdchecked) {
if (!courseIdchecked.add(course.getId())) {
return;
}
if (!skip) {
boolean valid;
Date today = new Date(now.getTime());
if (course.isExpires()) {
if (today.compareTo(DateCalc.addInterval(course.getStop(), course.getValidityPeriod(), course.getValidityPeriodDays())) > 0) {
valid = false;
} else {
valid = true;
}
} else {
valid = true;
}
if (valid) {
if ((course.isSelfRegistration() && ((course.getParticipationDeadline() == null && today.compareTo(course.getStop()) <= 0) ||
(course.getParticipationDeadline() != null && now.compareTo(course.getParticipationDeadline()) <= 0)))
|| ((!course.isSelfRegistration() &&
today.compareTo(course.getStop()) <= 0) &&
(participatingCourseIds == null || participatingCourseIds.contains(course.getId())))) {
result.add(course);
}
}
}
Collection<Course> renewals = course.getRenewals();
if (renewals != null && renewals.size() > 0) {
Iterator<Course> it = renewals.iterator();
while (it.hasNext()) {
findUpcomingRenewals(now, it.next(), result, false, participatingCourseIds, courseIdchecked);
}
}
}
public int compare(Object gradebook, Object otherGradebook) {
Timestamp modDate1 = ((Gradebook) gradebook).getLastUpdated();
Timestamp modDate2 = ((Gradebook) otherGradebook).getLastUpdated();
if(modDate1.equals(modDate2)) {
return compareTitles((Gradebook) gradebook, (Gradebook)otherGradebook);
}
return modDate1.compareTo(modDate2);
}
public boolean execute(Timestamp object, Object context)
{
MutableComparableReference<Timestamp> result = (MutableComparableReference<Timestamp>) context;
if(result.isNull() || (object.compareTo(result.getValue()) > 0))
{
result.replace(object);
}
return false;
}
public boolean execute(Timestamp object, Object context)
{
MutableComparableReference<Timestamp> result = (MutableComparableReference<Timestamp>) context;
if(result.isNull() || object.compareTo(result.getValue()) < 0)
{
result.replace(object);
}
return false;
}
public int compare(Hashtable<String, String> a, Hashtable<String, String> b) {
Integer a1 = Integer.parseInt(a.get("similarity"));
Integer b1 = Integer.parseInt(b.get("similarity"));
if (a1 < b1) {
return 1;
} else if (a1 == b1) {
if (matchSortStrategry == TMPreferenceConstants.DEFAULT_DB_PRECEDENCE) {
// sort by default database
String dbName = b.get("dbName");
if (dbName.equals(tmDbOperatorManager.getDefaultDbName())) {
return -1;
}
} else if (matchSortStrategry == TMPreferenceConstants.DATE_REVERSE_PRECEDENCE) {
// sort by update date
String t1Str = a.get("tgtChangeDate");
String t2Str = b.get("tgtChangeDate");
if (t1Str.length() != 0 && t2Str.length() != 0) {
Timestamp t1 = DateUtils.getTimestampFromUTC(t1Str);
Timestamp t2 = DateUtils.getTimestampFromUTC(t2Str);
return t2.compareTo(t1);
} else if (t1Str.length() == 0 && t2Str.length() != 0) {
return -1;
}
}
return 0;
} else {
return -1;
}
}
public int compare(Hashtable<String, String> a, Hashtable<String, String> b) {
Integer a1 = Integer.parseInt(a.get("similarity"));
Integer b1 = Integer.parseInt(b.get("similarity"));
if (a1 < b1) {
return 1;
} else if (a1 == b1) {
if (matchSortStrategry == TMPreferenceConstants.DEFAULT_DB_PRECEDENCE) {
// sort by default database
String dbName = b.get("dbName");
if (dbName.equals(tmDbOperatorManager.getDefaultDbName())) {
return -1;
}
} else if (matchSortStrategry == TMPreferenceConstants.DATE_REVERSE_PRECEDENCE) {
// sort by update date
String t1Str = a.get("tgtChangeDate");
String t2Str = b.get("tgtChangeDate");
if (t1Str.length() != 0 && t2Str.length() != 0) {
Timestamp t1 = DateUtils.getTimestampFromUTC(t1Str);
Timestamp t2 = DateUtils.getTimestampFromUTC(t2Str);
return t2.compareTo(t1);
} else if (t1Str.length() == 0 && t2Str.length() != 0) {
return -1;
}
}
return 0;
} else {
return -1;
}
}
public int compare(FuzzySearchResult a, FuzzySearchResult b) {
int a1 = a.getSimilarity();
int b1 = b.getSimilarity();
if (a1 < b1) {
return 1;
} else if (a1 == b1) {
if (sortStrategy == TMPreferenceConstants.DEFAULT_DB_PRECEDENCE) {
// sort by default database
String dbName = b.getDbName();
if (dbName.equals(deffaultTm)) {
return -1;
}
} else if (sortStrategy == TMPreferenceConstants.DATE_REVERSE_PRECEDENCE) {
// sort by update date
String t1Str = a.getTu().getChangeDate();
String t2Str = b.getTu().getChangeDate();
if (t1Str.length() != 0 && t2Str.length() != 0) {
Timestamp t1 = DateUtils.getTimestampFromUTC(t1Str);
Timestamp t2 = DateUtils.getTimestampFromUTC(t2Str);
return t2.compareTo(t1);
} else if (t1Str.length() == 0 && t2Str.length() != 0) {
return -1;
}
}
return 0;
} else {
return -1;
}
}
/**
* Compares two Timestamps with the expected result.
*
* @param ts1 the first Timestamp
* @param ts2 the second Timestamp
* @param expect the expected relation between ts1 and ts2; 0 if
* ts1 equals to ts2, or 1 if ts1 is after ts2, or -1 if ts1 is
* before ts2.
*/
private void compareTimestamps(Timestamp ts1, Timestamp ts2, int expected) {
boolean expectedResult = expected > 0;
boolean result = ts1.after(ts2);
if (result != expectedResult) {
errln("ts1.after(ts2) returned " + result
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
expectedResult = expected < 0;
result = ts1.before(ts2);
if (result != expectedResult) {
errln("ts1.before(ts2) returned " + result
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
expectedResult = expected == 0;
result = ts1.equals(ts2);
if (result != expectedResult) {
errln("ts1.equals(ts2) returned " + result
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
int x = ts1.compareTo(ts2);
int y = (x > 0) ? 1 : (x < 0) ? -1 : 0;
if (y != expected) {
errln("ts1.compareTo(ts2) returned " + x + ", expected "
+ relation(expected, "") + "0"
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
long t1 = ts1.getTime();
long t2 = ts2.getTime();
int z = (t1 > t2) ? 1 : (t1 < t2) ? -1 : 0;
if (z == 0) {
int n1 = ts1.getNanos();
int n2 = ts2.getNanos();
z = (n1 > n2) ? 1 : (n1 < n2) ? -1 : 0;
}
if (z != expected) {
errln("ts1.getTime() " + relation(z, "==") + " ts2.getTime(), expected "
+ relation(expected, "==")
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
}
private boolean ge(Timestamp value, Timestamp filter) {
return filter != null ? filter.compareTo(value) <= 0 : true;
}
/**
* Compares two Timestamps with the expected result.
*
* @param ts1 the first Timestamp
* @param ts2 the second Timestamp
* @param expect the expected relation between ts1 and ts2; 0 if
* ts1 equals to ts2, or 1 if ts1 is after ts2, or -1 if ts1 is
* before ts2.
*/
private void compareTimestamps(Timestamp ts1, Timestamp ts2, int expected) {
boolean expectedResult = expected > 0;
boolean result = ts1.after(ts2);
if (result != expectedResult) {
errln("ts1.after(ts2) returned " + result
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
expectedResult = expected < 0;
result = ts1.before(ts2);
if (result != expectedResult) {
errln("ts1.before(ts2) returned " + result
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
expectedResult = expected == 0;
result = ts1.equals(ts2);
if (result != expectedResult) {
errln("ts1.equals(ts2) returned " + result
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
int x = ts1.compareTo(ts2);
int y = (x > 0) ? 1 : (x < 0) ? -1 : 0;
if (y != expected) {
errln("ts1.compareTo(ts2) returned " + x + ", expected "
+ relation(expected, "") + "0"
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
long t1 = ts1.getTime();
long t2 = ts2.getTime();
int z = (t1 > t2) ? 1 : (t1 < t2) ? -1 : 0;
if (z == 0) {
int n1 = ts1.getNanos();
int n2 = ts2.getNanos();
z = (n1 > n2) ? 1 : (n1 < n2) ? -1 : 0;
}
if (z != expected) {
errln("ts1.getTime() " + relation(z, "==") + " ts2.getTime(), expected "
+ relation(expected, "==")
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
}
/**
* This method calculates the per diem amount for a given period of time at the rate provided. The per diem amount is
* calculated as described below.
*
* For same day trips:
* - Per diem is equal to 1/2 of the per diem rate provided if the difference in time between the start and end time is
* greater than 12 hours. An additional 1/4 of a day is added back to the amount if the trip lasted past 7:00pm.
* - If the same day trip is less than 12 hours, the per diem amount will be zero.
*
* For multiple day trips:
* - Per diem amount is equal to the full rate times the number of full days of travel. A full day is equal to any day
* during the trip that is not the first day or last day of the trip.
* - For the first day of the trip,
* if the travel starts before noon, you receive a full day per diem,
* if the travel starts between noon and 5:59pm, you get a half day per diem,
* if the travel starts after 6:00pm, you only receive a quarter day per diem
* - For the last day of the trip,
* if the travel ends before 6:00am, you only receive a quarter day per diem,
* if the travel ends between 6:00am and noon, you receive a half day per diem,
* if the travel ends after noon, you receive a full day per diem
*
* @param stateDateTime The starting date and time of the period the per diem amount is calculated for.
* @param endDateTime The ending date and time of the period the per diema mount is calculated for.
* @param rate The per diem rate used to calculate the per diem amount.
* @return The per diem amount for the period specified, at the rate given.
*
* @see org.kuali.kfs.fp.document.service.DisbursementVoucherTravelService#calculatePerDiemAmount(org.kuali.kfs.fp.businessobject.DisbursementVoucherNonEmployeeTravel)
*/
@Override
public KualiDecimal calculatePerDiemAmount(Timestamp startDateTime, Timestamp endDateTime, KualiDecimal rate) {
KualiDecimal perDiemAmount = KualiDecimal.ZERO;
KualiDecimal perDiemRate = new KualiDecimal(rate.doubleValue());
// make sure we have the fields needed
if (perDiemAmount == null || startDateTime == null || endDateTime == null) {
LOG.error("Per diem amount, Start date/time, and End date/time must all be given.");
throw new RuntimeException("Per diem amount, Start date/time, and End date/time must all be given.");
}
// check end time is after start time
if (endDateTime.compareTo(startDateTime) <= 0) {
LOG.error("End date/time must be after start date/time.");
throw new RuntimeException("End date/time must be after start date/time.");
}
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(startDateTime);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(endDateTime);
double diffDays = KfsDateUtils.getDifferenceInDays(startDateTime, endDateTime);
double diffHours = KfsDateUtils.getDifferenceInHours(startDateTime, endDateTime);
// same day travel
if (diffDays == 0) {
// no per diem for only 12 hours or less
if (diffHours > 12) {
// half day of per diem
perDiemAmount = perDiemRate.divide(new KualiDecimal(2));
// add in another 1/4 of a day if end time past 7:00
if (timeInPerDiemPeriod(endCalendar, 19, 0, 23, 59)) {
perDiemAmount = perDiemAmount.add(perDiemRate.divide(new KualiDecimal(4)));
}
}
}
// multiple days of travel
else {
// must at least have 7 1/2 hours to get any per diem
if (diffHours >= 7.5) {
// per diem for whole days
perDiemAmount = perDiemRate.multiply(new KualiDecimal(diffDays - 1));
// per diem for first day
if (timeInPerDiemPeriod(startCalendar, 0, 0, 11, 59)) { // Midnight to noon
perDiemAmount = perDiemAmount.add(perDiemRate);
}
else if (timeInPerDiemPeriod(startCalendar, 12, 0, 17, 59)) { // Noon to 5:59pm
perDiemAmount = perDiemAmount.add(perDiemRate.divide(new KualiDecimal(2)));
}
else if (timeInPerDiemPeriod(startCalendar, 18, 0, 23, 59)) { // 6:00pm to Midnight
perDiemAmount = perDiemAmount.add(perDiemRate.divide(new KualiDecimal(4)));
}
// per diem for end day
if (timeInPerDiemPeriod(endCalendar, 0, 1, 6, 0)) { // Midnight to 6:00am
perDiemAmount = perDiemAmount.add(perDiemRate.divide(new KualiDecimal(4)));
}
else if (timeInPerDiemPeriod(endCalendar, 6, 1, 12, 0)) { // 6:00am to noon
perDiemAmount = perDiemAmount.add(perDiemRate.divide(new KualiDecimal(2)));
}
else if (timeInPerDiemPeriod(endCalendar, 12, 01, 23, 59)) { // Noon to midnight
perDiemAmount = perDiemAmount.add(perDiemRate);
}
}
}
return perDiemAmount;
}
/**
* Compares two Timestamps with the expected result.
*
* @param ts1 the first Timestamp
* @param ts2 the second Timestamp
* @param expect the expected relation between ts1 and ts2; 0 if
* ts1 equals to ts2, or 1 if ts1 is after ts2, or -1 if ts1 is
* before ts2.
*/
private void compareTimestamps(Timestamp ts1, Timestamp ts2, int expected) {
boolean expectedResult = expected > 0;
boolean result = ts1.after(ts2);
if (result != expectedResult) {
errln("ts1.after(ts2) returned " + result
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
expectedResult = expected < 0;
result = ts1.before(ts2);
if (result != expectedResult) {
errln("ts1.before(ts2) returned " + result
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
expectedResult = expected == 0;
result = ts1.equals(ts2);
if (result != expectedResult) {
errln("ts1.equals(ts2) returned " + result
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
int x = ts1.compareTo(ts2);
int y = (x > 0) ? 1 : (x < 0) ? -1 : 0;
if (y != expected) {
errln("ts1.compareTo(ts2) returned " + x + ", expected "
+ relation(expected, "") + "0"
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
long t1 = ts1.getTime();
long t2 = ts2.getTime();
int z = (t1 > t2) ? 1 : (t1 < t2) ? -1 : 0;
if (z == 0) {
int n1 = ts1.getNanos();
int n2 = ts2.getNanos();
z = (n1 > n2) ? 1 : (n1 < n2) ? -1 : 0;
}
if (z != expected) {
errln("ts1.getTime() " + relation(z, "==") + " ts2.getTime(), expected "
+ relation(expected, "==")
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
}