如何将/转换/转换为String? [英] How to convert / cast long to String?

查看:184
本文介绍了如何将/转换/转换为String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚创建了示例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);

选择日期后,我需要将该长值转换为String,以便我可以轻松存储数据库中某处的日期值。
我是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?另外我想从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);

编辑:

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

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

这篇关于如何将/转换/转换为String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆