Link to home
Start Free TrialLog in
Avatar of Ivo Raemy
Ivo Raemy

asked on

Java class which converts object type to types like Long, Date, Integer

Hello

I am looking for a Java class which converts object type to types like Long, Date, Integer

sincerely

Ivo Raemy
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Your question doesn't really make sense. Please expand on your requirement
Do you have a String value or something that you are trying to convert to the appropriate type?   Do you have input examples of what you are trying to convert.  This could help us answer your question.
I have a feeling you need a cast.
Hi,

There is no class that does this for you as such. Although you could create your own.

The methods are available to help you convert objects to the specified class but it depends on what you know already about the class.

You could look at

class.isAssignableFrom

instanceof

Then depending on the class you could attempt to convert it using Integer.parseInt() etc...

Thanks,

Darren
Describing the code to a goal that's being guessed at and possibly false is really a waste of time. Describe your GOAL Ivo
Avatar of Ivo Raemy
Ivo Raemy

ASKER

Hallo

I want to execute a native query and get the values.

I get all numbers in format BigDecimal.


I can do conversions by my Code, However, I am sure that there a better solutions.


        Query query = getEntityManager().createNativeQuery(queryStr);

        List<Object[]> paquetValidations = query.getResultList();

         paquetValidations.forEach((paquetValidation) -> {

             //convert
             Long etudiantId = paquetValidation[0] == null ? null : Long.parseLong(String.valueOf(paquetValidation[0]));
ASKER CERTIFIED SOLUTION
Avatar of Darren
Darren
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello Darren

Thanks for your post.

Il will write my own class.

sincerely

Ivo
If they're student IDs then you're not likely to be doing arithmetic on them are you - so why not String?

String studentId = paquetValidations[0].toString();

Open in new window


Also JPA supports typed results:
https://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#createNamedQuery(java.lang.String,%20java.lang.Class)