public class StringToLong {
public static void main (String[] args) {
// String s = "fred"; // do this if you want an exception
String s = "100";
try {
long l = Long.parseLong(s);
System.out.println("long l = " + l);
} catch (NumberFormatException nfe) {
System.out.println("NumberFormatException: " + nfe.getMessage());
}
}
}
Map map = new HashMap();
map.put("name", "John");
map.put("time", "9648512236521");
map.put("age", "25");
long time = Long.valueOf((String)map.get("time")).longValue() ;
int age = Integer.valueOf((String) map.get("aget")).intValue();
System.out.println(time);
System.out.println(age);
String numberAsString = "1234";
long number = Long.valueOf(numberAsString).longValue();
String numberAsString = "1234";
Long longObject = new Long(numberAsString);
long number = longObject.longValue();
我们可以缩短为:
String numberAsString = "1234";
long number = new Long(numberAsString).longValue();
或者只是
long number = new Long("1234").longValue();
使用十进制格式:
String numberAsString = "1234";
DecimalFormat decimalFormat = new DecimalFormat("#");
try {
long number = decimalFormat.parse(numberAsString).longValue();
System.out.println("The number is: " + number);
} catch (ParseException e) {
System.out.println(numberAsString + " is not a valid number.");
}