How to convert / cast long to String?

853,765

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);
Share:
853,765
Admin
Author by

Admin

Updated on February 23, 2021

Comments

  • Admin
    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
    Daniel Hepper over 12 years
    Note that Long.valueOf(String) returns a Long. If you want a long, use Long.parseLong(String).
  • Marcelo Assis
    Marcelo Assis about 12 years
    What's the differente between this way, or just concat like this: ("" + longAttr) ?
  • Hari Menon
    Hari Menon about 12 years
    @MarceloAssis.. concat is about 2x slower. If performance matters in you application.
  • KomodoDave
    KomodoDave over 11 years
    For general type conversion outside the Date context this is the recommended practise to convert Long to String.
  • Guillaume Husta
    Guillaume Husta almost 10 years
    Nice, but note that java.util.Objects is only available since Java 7.
  • Guillaume Husta
    Guillaume Husta almost 10 years
    Works fine with primitive type (long), but may throw NPE with Object type (Long), even with autoboxing.
  • Guillaume Husta
    Guillaume Husta almost 10 years
    Before 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
    gMale over 9 years
    I thought String.valueOf was the preferred approach but I noticed that just calls Long.toString so maybe that's the simpler way.
  • Fabian Schultz
    Fabian Schultz over 7 years
    It'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
    Krupal Shah over 7 years
    This 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
    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
    membersound over 6 years
    Is there any difference to String.valueOf(myLong)?
  • Nero
    Nero about 6 years
    @membersound they're the same. Basically String.valueOf(long) calls Long.toString()
  • Dhiresh Jain
    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