ChatGPT解决这个技术问题 Extra ChatGPT

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


A
Arnout Engelen

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


Note that Long.valueOf(String) returns a Long. If you want a long, use Long.parseLong(String).
What's the differente between this way, or just concat like this: ("" + longAttr) ?
@MarceloAssis.. concat is about 2x slower. If performance matters in you application.
Nice, but note that java.util.Objects is only available since Java 7.
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)
F
Fisu
String strLong = Long.toString(longNumber);

Simple and works fine :-)


For general type conversion outside the Date context this is the recommended practise to convert Long to String.
Works fine with primitive type (long), but may throw NPE with Object type (Long), even with autoboxing.
P
Pedro Lobito

Long.toString()

The following should work:

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

Is there any difference to String.valueOf(myLong)?
@membersound they're the same. Basically String.valueOf(long) calls Long.toString()
@Nero I wouldn't call the same because String.valueOf() will return "null" in case of null, while Long.toString() will cause NullPointerException
F
Flexo

very simple, just concatenate the long to a string.

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

u
user207421

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

A
Aditya Vyas-Lakhan
String logStringVal= date+"";

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


I thought String.valueOf was the preferred approach but I noticed that just calls Long.toString so maybe that's the simpler way.
A
Anushil Kumar

Just do this:

String strLong = Long.toString(longNumber);

N
Nathan Meyer
String longString = new String(""+long);

or

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

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.
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
@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