Message Boards
Difference between two Dates
Nikolaos Kroustalakis, modified 3 Years ago.
Difference between two Dates
New Member Posts: 14 Join Date: 7/29/19 Recent Posts
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.
Mohammed Yasin, modified 3 Years ago.
RE: Difference between two Dates
Liferay Master Posts: 591 Join Date: 8/8/14 Recent Posts
Hi,You can use com.liferay.portal.kernel.util.DateUtil for getting year from Date and subtract it
DateUtil.getYear(endDate) - DateUtil.getYear(currentDate)
Nikolaos Kroustalakis, modified 3 Years ago.
RE: Difference between two Dates
New Member Posts: 14 Join Date: 7/29/19 Recent Posts
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.
Olaf Kock, modified 3 Years ago.
RE: Difference between two Dates
Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
make sure you get your timezones right, in case the "penalty" coming in a few hours early or late would be a problem.