問題描述
我剛剛創建了示例 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
這篇關于如何將長轉換/轉換為字符串?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!