[英]How to convert / cast long to String?
I just created sample BB app, which can allow to choose the date.我刚刚创建了示例 BB 应用程序,它可以允许选择日期。
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.
我是 Java 和 Blackberry 开发的新手。
long date = curDateFld.getDate();
How should I convert this long value to String?我应该如何将这个长值转换为字符串? Also I want to convert back to long from String.
另外我想从String转换回long。 I think for that I can use
long l = Long.parseLong("myStr");
我认为为此我可以使用
long l = Long.parseLong("myStr");
? ?
See the reference documentation for the String class : String s = String.valueOf(date);
请参阅String 类的 参考文档:
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);
如果您的 Long 可能为空并且您不想获得 4 个字母的
"null"
字符串,您可以使用Objects.toString
,例如: String s = Objects.toString(date, null);
EDIT:编辑:
You reverse it using Long l = Long.valueOf(s);
您可以使用
Long l = Long.valueOf(s);
反转它Long l = Long.valueOf(s);
but in this direction you need to catch NumberFormatException
但在这个方向上你需要抓住
NumberFormatException
String strLong = Long.toString(longNumber);
简单且工作正常:-)
Long.toString()
The following should work:以下应该工作:
long myLong = 1234567890123L;
String myString = Long.toString(myLong);
very simple, just concatenate the long to a string.非常简单,只需将 long 连接到一个字符串。
long date = curDateFld.getDate();
String str = ""+date;
1. 1.
long date = curDateFld.getDate();
//convert long to string
String str = String.valueOf(date);
//convert string to long
date = Long.valueOf(str);
2. 2.
//convert long to string just concat long with empty string
String str = ""+date;
//convert string to long
date = Long.valueOf(str);
String logStringVal= date+"";
Can convert the long into string object, cool shortcut for converting into string...but use of String.valueOf(date);
可以将 long 转换为字符串对象,很酷的转换为字符串的快捷方式...但是使用
String.valueOf(date);
is advisable是可取的
只需这样做:
String strLong = Long.toString(longNumber);
String longString = new String(""+long);
要么
String longString = new Long(datelong).toString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.