Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to long [duplicate]

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); 
like image 962
John Avatar asked Mar 30 '12 03:03

John


People also ask

Can we convert String to long in java?

Java – Convert String to long using Long. Long. valueOf(String): Converts the String to a long value. Similar to parseLong(String) method, this method also allows minus '-' as a first character in the String.

Can we typecast long to String?

We can convert long to String in java using String. valueOf() and Long. toString() methods.


2 Answers

This is a common way to do it:

long l = Long.parseLong(str); 

There is also this method: Long.valueOf(str); Difference is that parseLong returns a primitive long while valueOf returns a new Long() object.

like image 120
Cristian Avatar answered Oct 05 '22 16:10

Cristian


The method for converting a string to a long is Long.parseLong. Modifying your example:

String s = "1333073704000"; long l = Long.parseLong(s); // Now l = 1333073704000 
like image 33
Adam Mihalcin Avatar answered Oct 05 '22 16:10

Adam Mihalcin