Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

java - 如何将/转换/转换为String?(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.

(选择日期后,我需要将该long值转换为String,以便我可以轻松地将日期值存储在数据库中的某个位置。)

I am new to Java and Blackberry development.

(我是Java和Blackberry开发的新手。)

long date = curDateFld.getDate();

How should I convert this long value to String?

(我应该如何将这个长值转换为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");)

?

(?)

  ask by user225714 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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可能为null并且您不想获得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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...