This post shows you how to convert String to Long in Java. Let's write a generic code in this post.
Create Generic Converter interface
/**
* Converts objects of S type to T type.
*/
public interface Converter<S, T> {
/**
* Converts the source object from S type to T type.
*
* @param source the object to convert
*
* @return the converted object
*/
T convert(S source);
}
Converts String to Integer - StringToLongConverter.java
Let's implement above Converter interface to convert String to Long in Java
/**
* Converts String to Long.
*/
public class StringToLongConverter implements Converter<String, Long> {
@Override
public Long convert(String source) {
return Long.valueOf(source);
}
}
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course