如何将长转换/转换为字符串?

2022-01-12 00:00:00 string type-conversion long-integer java

我刚刚创建了示例 BB 应用程序,它可以允许选择日期.

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

选择日期后,我需要将该长值转换为字符串,以便可以轻松地将日期值存储在数据库中的某个位置.我是 Java 和 Blackberry 开发的新手.

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

我应该如何将这个长值转换为字符串?我也想从String转换回long.我认为我可以使用 long l = Long.parseLong("myStr");?

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

推荐答案

参见String 类的参考文档:String s = String.valueOf(date);

如果您的 Long 可能为 null 并且您不想获得 4 个字母的 "null" 字符串,则可以使用 Objects.toString,如:String s = Objects.toString(date, null);

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

您使用 Long l = Long.valueOf(s); 反转它,但在这个方向上您需要捕获 NumberFormatException

You reverse it using Long l = Long.valueOf(s); but in this direction you need to catch NumberFormatException

相关文章