How to convert / cast long to String?
Solution 1
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
Solution 2
String strLong = Long.toString(longNumber);
Simple and works fine :-)
Solution 3
Long.toString()
The following should work:
long myLong = 1234567890123L;
String myString = Long.toString(myLong);
Solution 4
very simple, just concatenate the long to a string.
long date = curDateFld.getDate();
String str = ""+date;
Solution 5
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);
Admin
Updated on February 23, 2021Comments
-
Admin about 3 years
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");
? -
Daniel Hepper over 12 yearsNote that
Long.valueOf(String)
returns aLong
. If you want along
, useLong.parseLong(String)
. -
Marcelo Assis about 12 yearsWhat's the differente between this way, or just concat like this: ("" + longAttr) ?
-
Hari Menon about 12 years@MarceloAssis.. concat is about 2x slower. If performance matters in you application.
-
KomodoDave over 11 yearsFor general type conversion outside the
Date
context this is the recommended practise to convertLong
toString
. -
Guillaume Husta almost 10 yearsNice, but note that
java.util.Objects
is only available since Java 7. -
Guillaume Husta almost 10 yearsWorks fine with primitive type (long), but may throw NPE with Object type (Long), even with autoboxing.
-
Guillaume Husta almost 10 yearsBefore Java 7, you could also use Commons Lang's
ObjectUtils.toString(Object,String)
which is equivalent. -> commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/…, java.lang.String) -
gMale over 9 yearsI thought
String.valueOf
was the preferred approach but I noticed that just callsLong.toString
so maybe that's the simpler way. -
Fabian Schultz over 7 yearsIt's good practice on Stack Overflow to add an explanation as to why your solution should work. For more information read How To Answer.
-
Krupal Shah over 7 yearsThis does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
-
FelixSFD over 7 years@KrupalShah A code-only answer might not be a good one, but it's still an answer. I would recommend you this post about the LQPRQ: You're doing it wrong: A plea for sanity in the Low Quality Posts queue
-
membersound over 6 yearsIs there any difference to
String.valueOf(myLong)
? -
Nero about 6 years@membersound they're the same. Basically String.valueOf(long) calls Long.toString()
-
Dhiresh Jain about 5 years@Nero I wouldn't call the same because String.valueOf() will return "null" in case of null, while Long.toString() will cause NullPointerException