How to convert / cast long to 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);
选择日期后,我需要将该长值转换为字符串,以便可以轻松地将日期值存储在数据库中的某个位置.我是 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
这篇关于如何将长转换/转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将长转换/转换为字符串?
				
        
 
            
        基础教程推荐
- 从 python 访问 JVM 2022-01-01
 - Java Swing计时器未清除 2022-01-01
 - 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
 - 不推荐使用 Api 注释的描述 2022-01-01
 - Java 实例变量在两个语句中声明和初始化 2022-01-01
 - 验证是否调用了所有 getter 方法 2022-01-01
 - 大摇大摆的枚举 2022-01-01
 - 在 Java 中创建日期的正确方法是什么? 2022-01-01
 - 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
 - 多个组件的复杂布局 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				