Java - Convert String to long

To convert a String to long in Java, we can use:

Long.parseLong()

long l1 = Long.parseLong("999");
System.out.println(l1);  // 999

long l2 = Long.parseLong("-FF", 16);
System.out.println(l2);  // -255

long l3 = Long.parseLong("01010101", 2);
System.out.println(l3);  // 85
                    

We also can use Long.parseUnsignedLong​(...) to make sure the long is > 0.

Long.valueOf()

The difference between valueOf(...) and parseLong(...) is valueOf(...) will return a Long (wrapper class) instead of primitive long.

  • static Long valueOf​(String s): Returns a Long object holding the value of the specified String.
  • static Long valueOf​(String s, int radix): Returns a Long object holding the value extracted from the specified String when parsed with the radix given by the second argument.
long l1 = Long.valueOf("888");
System.out.println(l1);  // 101010101

long l2 = Long.valueOf("-FF", 16);
System.out.println(l2);  // -255

long l3 = Long.valueOf("01010101", 2);
System.out.println(l3);  // 85
                    

As you can see, implicit casting automatically unbox Long into long.

Deprecated: Long‘s Constructor

Long​(String s) is deprecated.

It is rarely appropriate to use this constructor. Use parseLong(String) to convert a string to a long primitive, or use valueOf(String) to convert a string to a Long object.

long l1 = new Long("123");
System.out.println(l1);  // 123

long l2 = new Long("-0x8F"); // NumberFormatException
System.out.println(l2);
                    

Long.decode()

static Long decode​(String nm): Decodes a String into a Long.

long l1 = Long.decode("321");
System.out.println(l1);  // 321

long l2 = Long.decode("-0x88");  // hex
System.out.println(l2);  // -136

long l3 = Long.decode("#F3F3F3");  // hex
System.out.println(l3);  // 15987699

long l4 = Long.decode("066");  // octal
System.out.println(l4);  // 54
                    

DecimalFormat.parse()

parse​(String source) throws ParseException: Parses text from the beginning of the given string to produce a number. The method may not use the entire text of the given string.

import java.text.DecimalFormat; import java.text.ParseException;

DecimalFormat decimalFormat = new DecimalFormat();
try {
    long l = decimalFormat.parse("54321").longValue();
    System.out.println(l);  // 54321
} catch (ParseException e) {
    System.out.println(e.getMessage());
}
                    

Since parse() method returns instance of Number, we need to call longValue() to get the long primitive value from it. The Number also has intValue() to get int primitive value, doubleValue() for double, etc.

And similar to Convert String to int, we also can use external libraries like Apache Commons NumberUtils, Spring's NumberUtils, and Google's Guava primitive Longs.

Apache Commons NumberUtils

Methods:

import org.springframework.util.NumberUtils;

long l1 = NumberUtils.toLong("365");
System.out.println(l1);  // 365

long l2 = NumberUtils.toLong(null);
System.out.println(l2);  // 0

long l3 = NumberUtils.toLong("365", 0);
System.out.println(l3);  // 365

long l4 = NumberUtils.toLong("xyz", -1);
System.out.println(l4);  // -1

long l5 = NumberUtils.toLong("", -1);
System.out.println(l5);  // -1

long l6 = NumberUtils.createLong("365");
System.out.println(l6);  // 365

long l7 = NumberUtils.createLong("-0xFF88");
System.out.println(l7);  // -65416
                    

Spring NumberUtils

Similar like in Converting String to int, we can use Spring's NumberUtils to parse String to number (in this case long).

import org.springframework.util.NumberUtils;

long i1 = NumberUtils.parseNumber("512", Long.class);
System.out.println(i1);  // 512

long i2 = NumberUtils.parseNumber("#F120", Long.class);
System.out.println(i2);  // 61728
                    

Google Guava Longs.tryParse()

The conversion also can be done using Guava’s Longs.tryParse().

Methods:

import com.google.common.primitives.Longs;

long l1 = Longs.tryParse("5123456789012346");
System.out.println(l1);  // 5123456789012346

long l2 = Longs.tryParse("-18F1", 16);
System.out.println(l2);  // -6385

long l3 = Longs.tryParse("111111111", 2);
System.out.println(l3);  // 511