How to convert / cast long to String?

user225714 picture user225714 · Dec 6, 2009 · Viewed 823.2k times · Source

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

Answer

Gregory Pakosz picture Gregory Pakosz · Dec 6, 2009

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