Planned maintenance is scheduled for the week of June 15th - the exact date and time will be announced soon.
See More Details
Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
Difference between two Dates
Hello. I have a maven project in Liferay 6.2. There is a requirement in which I must compare an EndDate stored in Oracle DB and the current Date. In order to do so, I have created a scheduled job. I implemented the receive(Message message) method, which is the following one:
I am having some difficulties in implementing the way to compare only the YEAR of those dates and check if the Difference between them is greater than 4.
Can you please help me sort this out? Any suggestion is really appreciated.
Thank you in advance.
@Override
public void receive(Message message) throws MessageListenerException {
logger.info("Scheduler for checking dates on Penalties has started...");
ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
PenaltyService service = (PenaltyService)applicationContext.getBean("service");
Date currentDate = new Date();
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
try {
currentDate = dateFormatter.parse(dateFormatter.format(new Date() ));
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Current date is: " + currentDate);
List<penalty> penalties = service.findAll();
for (Penalty penalty : penalties) {
if (penalty.endDate == null) {
System.out.println("No EndDate...");
}
else if (penalty.endDate.before(currentDate)) {
penalty.setRevocationOrCompletion(1);
}
else if (penalty.endDate.compareTo(currentDate) == 0) {
penalty.setRevocationOrCompletion(1);
}
System.out.println("-------------------" + penalty.getEndDate() + " " + penalty.isRevocationOrCompletion());
}
}</penalty>I am having some difficulties in implementing the way to compare only the YEAR of those dates and check if the Difference between them is greater than 4.
Can you please help me sort this out? Any suggestion is really appreciated.
Thank you in advance.
Hi,You can use com.liferay.portal.kernel.util.DateUtil for getting year from Date and subtract it
DateUtil.getYear(endDate) - DateUtil.getYear(currentDate)
I had some problems with that solution. Fixed it by doing the following:
[code]@SuppressWarnings( "deprecation" )
int penaltyYear = penalty.endDate.getYear();
int currentYear = currentDate.getYear();
if ((penaltyYear + 4) < currentYear) {
// Invoke penalty
}Thank you so much for your help though.
make sure you get your timezones right, in case the "penalty" coming in a few hours early or late would be a problem.
Community
Company
Feedback