321

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

Cory Roy
  • 5,379
  • 2
  • 28
  • 47
user225714
  • 3,211
  • 2
  • 16
  • 5

8 Answers8

409

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

Arnout Engelen
  • 6,709
  • 1
  • 25
  • 36
Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
  • 14
    Note that `Long.valueOf(String)` returns a `Long`. If you want a `long`, use `Long.parseLong(String)`. – Daniel Hepper Oct 26 '11 at 05:33
  • 1
    What's the differente between this way, or just concat like this: ("" + longAttr) ? – Marcelo Assis Feb 08 '12 at 13:05
  • 4
    @MarceloAssis.. concat is about 2x slower. If performance matters in you application. – Hari Menon Feb 25 '12 at 14:14
  • Nice, but note that ``java.util.Objects`` is only available since Java 7. – Guillaume Husta Jul 23 '14 at 09:12
  • 1
    Before Java 7, you could also use Commons Lang's ``ObjectUtils.toString(Object,String)`` which is equivalent. -> https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/ObjectUtils.html#toString(java.lang.Object, java.lang.String) – Guillaume Husta Jul 23 '14 at 09:59
231
String strLong = Long.toString(longNumber);

Simple and works fine :-)

Fisu
  • 2,534
  • 1
  • 15
  • 14
108

Long.toString()

This should work:

long myLong = 1234567890123L;
String myString = Long.toString(myLong);
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
20

very simple, just concatenate the long to a string.

long date = curDateFld.getDate(); 
String str = ""+date;
Flexo
  • 87,323
  • 22
  • 191
  • 272
MR.M
  • 217
  • 2
  • 2
10

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);
user207421
  • 305,947
  • 44
  • 307
  • 483
iKushal
  • 2,689
  • 24
  • 20
3
String logStringVal= date+"";

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

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
MBR
  • 287
  • 1
  • 4
  • 15
  • I thought `String.valueOf` was the preferred approach but I noticed that just calls `Long.toString` so maybe that's the simpler way. – gMale Nov 17 '14 at 18:39
3

Just do this:

String strLong = Long.toString(longNumber);
Anushil Kumar
  • 674
  • 8
  • 8
2
String longString = new String(""+long);

or

String longString = new Long(datelong).toString();
Nathan Meyer
  • 409
  • 1
  • 6
  • 13
  • 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](http://stackoverflow.com/help/how-to-answer). – Fabian Schultz Jan 09 '17 at 17:25
  • 1
    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](/review/low-quality-posts/14830916) – Krupal Shah Jan 09 '17 at 18:13
  • 2
    @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](http://meta.stackoverflow.com/questions/287563/youre-doing-it-wrong-a-plea-for-sanity-in-the-low-quality-posts-queue) – FelixSFD Jan 09 '17 at 18:33