Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert / cast long to String?

I just created sample BB app, which can allow to choose the date.

DateField curDateFld = new DateField("Choose Date: ",   System.currentTimeMillis(), DateField.DATE | DateField.FIELD_LEFT); 

After choosing the date, I need to convert that long value to String, so that I can easily store the date value somewhere in database. I am new to Java and Blackberry development.

long date = curDateFld.getDate(); 

How should I convert this long value to String? Also I want to convert back to long from String. I think for that I can use long l = Long.parseLong("myStr");?

like image 734
user225714 Avatar asked Dec 06 '09 09:12

user225714


People also ask

How do you convert primitive long to String?

One way to cast a long primitive type to String is by using String concatenation. The '+' operator in java is overloaded as a String concatenator. Anything added to a String object using the '+' operator becomes a String. In the above example, the output “4587” is a String type and is no longer a long.

Can we type cast char to String?

We can convert a char to a string object in java by using the Character. toString() method.

What converts casting from long to integer?

We can convert long to int in java using typecasting. To convert higher data type into lower, we need to perform typecasting. Typecasting in java is performed through typecast operator (datatype).

How do you pass a long String in java?

There are three main ways to convert a long value to a String in Java e.g. by using Long. toString(long value) method, by using String. valueOf(long), and by concatenating with an empty String. You can use any of these methods to convert a long data type into a String object.


2 Answers

See the reference documentation for the String class: String s = String.valueOf(date);

If your Long might be null and you don't want to get a 4-letter "null" string, you might use Objects.toString, like: String s = Objects.toString(date, null);


EDIT:

You reverse it using Long l = Long.valueOf(s); but in this direction you need to catch NumberFormatException

like image 169
Gregory Pakosz Avatar answered Oct 11 '22 13:10

Gregory Pakosz


String strLong = Long.toString(longNumber); 

Simple and works fine :-)

like image 38
Fisu Avatar answered Oct 11 '22 14:10

Fisu