Set string or integer to "current year" on a BeforeInsertEntityListener

Hello All,

Having trouble finding the right code syntax for setting a string or integer to the current year for a BeforeInsertEntityListener. Any help would be appreciated.

Hi!

Where do you want to put this value?

Hi Danill,
Ultimately, this current year integer value will update an entry in a database table.
Here is what I have so far, but I keep getting a ‘cannot find symbol’ error on the new Date() section below:


DateTime curDate = new Date(); 
        Integer thisYear = EXTRACT(YEAR, curDate); 
        String curryear = Integer.toString(thisYear); 

Thanks for the help.

So the main problem is to extract a year from the given Date instance?

Hi Danill,

That’s correct. For one, I cannot seem to get the current date. Once I am able to determine the current date, i will need to extract the year from the date for later use to update a field.

I suggest that you use java.util.Calendar. Here is an example:

Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());

int year = calendar.get(Calendar.YEAR);

Hi Danill,

Thanks again for your help. I tried the above example, but i am recieving the following error when trying to run:

:app-core:compileJavaC:\Users\RayL\studio-projects\InvoicingNumber\modules\core\src\com\company\invoicingnumber\listener\InvoiceEntityListener.java:32: error: incompatible types: com.company.invoicingnumber.listener.Date cannot be converted to java.util.Date
calendar.setTime(new Date());
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
FAILED

Here is a full example of my code:


package com.company.invoicingnumber.listener;

import com.company.invoicingnumber.entity.Company;
import com.company.invoicingnumber.entity.Invoice;
import com.haulmont.cuba.core.Persistence;
import com.haulmont.cuba.core.listener.BeforeInsertEntityListener;
import org.eclipse.persistence.jpa.jpql.parser.DateTime;
import org.springframework.stereotype.Component;
import java.util.*;
import javax.inject.Inject;

@Component("invoicingnumber_InvoiceEntityListener")
public class InvoiceEntityListener implements BeforeInsertEntityListener<Invoice> {

    @Inject
    private Persistence persistence;

    @Override
    public void onBeforeInsert(Invoice entity) {
        // check if company is set
        Company company = entity.getCompany();
        if (company == null)
            throw new RuntimeException("Company is not set");

        // get managed instance of Company
        company = persistence.getEntityManager().reloadNN(company);
        Integer count = company.getInvoiceCount();
        String prefix = company.getPrefix();
        count += 1;
        String newnum = Integer.toString(count);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int year = calendar.get(Calendar.YEAR);
        String curryear = Integer.toString(year);
        String numlftpad = "000".substring(newnum.length()) + newnum;
        String sep = "-";
        String prefixyear = prefix.concat(curryear);
        String prenum = prefixyear.concat(sep);
        String finalnum = prenum.concat(numlftpad);
        // save new number to new Entry and System Counter
        entity.setNumber(finalnum);
        company.setInvoiceCount(count);
    }
}

thanks again for the help :slight_smile:

I thought that you use defaut java.util.Date class. What is the need to use your own implementation?

Hi Danill,
Happy to report it is working now!! I mistakenly was under the impression that

import java.util.*;

was a wildcard for both

import java.util.Date;

and

import java.util.Calendar;

As soon, as I removed that import entry, and replaced with the following:

import java.util.Date;
import  java.util.Calendar;

Everything worked!!
Thanks again for the help!