This question already has an answer here:
- How to convert String to long in Java? 8 answers
how do you convert a string into a long.
for int you
int i = 3423;
String str;
str = str.valueOf(i);
so how do you go the other way but with long.
long lg;
String Str = "1333073704000"
lg = lg.valueOf(Str);
You can also try following,
The method for converting a string to a long is Long.parseLong. Modifying your example:
This will handle
null
Do this:
However, always check that str contains digits to prevent throwing exceptions. For instance:
would throw an exception but this
won't.
This is a common way to do it:
There is also this method:
Long.valueOf(str);
Difference is thatparseLong
returns a primitivelong
whilevalueOf
returns anew Long()
object.