user225714
user225714

Reputation: 3221

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");?

Upvotes: 322

Views: 883606

Answers (8)

Pedro Lobito
Pedro Lobito

Reputation: 98861

Long.toString()

This should work:

long myLong = 1234567890123L;
String myString = Long.toString(myLong);

Upvotes: 109

Anushil Kumar
Anushil Kumar

Reputation: 672

Just do this:

String strLong = Long.toString(longNumber);

Upvotes: 3

Nathan Meyer
Nathan Meyer

Reputation: 415

String longString = new String(""+long);

or

String longString = new Long(datelong).toString();

Upvotes: 2

iKushal
iKushal

Reputation: 2869

1.

long date = curDateFld.getDate();
//convert long to string
String str = String.valueOf(date);

//convert string to long
date = Long.valueOf(str);

2.

 //convert long to string just concat long with empty string
 String str = ""+date;
//convert string to long

date = Long.valueOf(str);

Upvotes: 10

MBR
MBR

Reputation: 297

String logStringVal= date+"";

Can convert the long into string object, cool shortcut for converting into string...but use of String.valueOf(date); is advisable

Upvotes: 3

Gregory Pakosz
Gregory Pakosz

Reputation: 70204

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

Upvotes: 413

Fisu
Fisu

Reputation: 2554

String strLong = Long.toString(longNumber);

Simple and works fine :-)

Upvotes: 233

MR.M
MR.M

Reputation: 217

very simple, just concatenate the long to a string.

long date = curDateFld.getDate(); 
String str = ""+date;

Upvotes: 20

Related Questions