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:
    @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);
                
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;List<penalty> penalties = service.findAll();
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for (Penalty penalty : penalties) {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (penalty.endDate == null) {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println("No EndDate...");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else if (penalty.endDate.before(currentDate)) {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;penalty.setRevocationOrCompletion(1);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else if (penalty.endDate.compareTo(currentDate) == 0) {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;penalty.setRevocationOrCompletion(1);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println("-------------------" + penalty.getEndDate() + " " + penalty.isRevocationOrCompletion());
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;}</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.
thumbnail
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 

&nbsp;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) &lt; currentYear) {
    // Invoke penalty
}
Thank you so much for your help though.
thumbnail
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.