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
Ivo RaemyAnalyst ProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Your question doesn't really make sense. Please expand on your requirement
0
Jeffrey Dake Senior Director of TechnologyCommented:
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.
0
krakatoaCommented:
I have a feeling you need a cast.
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

DarrenSenior Software EngineerCommented:
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
0
CEHJCommented:
Describing the code to a goal that's being guessed at and possibly false is really a waste of time. Describe your GOAL Ivo
0
Ivo RaemyAnalyst ProgrammerAuthor Commented:
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]));
0
DarrenSenior Software EngineerCommented:
Hi,

You are going to have to perform the conversions yourself in code, I don't think that there is anything that will do this for you.

Obviously, you could write a function using templates/generics to convert the various types of object to the class you require. Although as far as I am aware you will need to know what class you are converting from.

Something like...

<T> convertParam(String param, Class paramClass) {
}

Open in new window


Thanks,

Darren
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ivo RaemyAnalyst ProgrammerAuthor Commented:
Hello Darren

Thanks for your post.

Il will write my own class.

sincerely

Ivo
0
CEHJCommented:
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)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.