Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将日期与当前日期进行比较会导致NumberFormatException_Java_Android_String_Integer_Simpledateformat - Fatal编程技术网

Java 将日期与当前日期进行比较会导致NumberFormatException

Java 将日期与当前日期进行比较会导致NumberFormatException,java,android,string,integer,simpledateformat,Java,Android,String,Integer,Simpledateformat,我正在尝试将日期/时间(保存在SharedReferences中)与当前日期/时间进行比较 有了这个,我将检查服务最后一次运行的时间,以及是否需要再次运行 我正在将当前日期转换为整数。但是我得到了一个错误: 错误: 08-27 17:21:40.930:W/System.err(26577):java.lang.NumberFormatException:无效int:“201308271721” 完全错误: 08-27 17:21:40.930:W/System.err(26577):java.

我正在尝试将日期/时间(保存在SharedReferences中)与当前日期/时间进行比较

有了这个,我将检查服务最后一次运行的时间,以及是否需要再次运行

我正在将当前日期转换为整数。但是我得到了一个错误: 错误:

08-27 17:21:40.930:W/System.err(26577):java.lang.NumberFormatException:无效int:“201308271721”

完全错误:

08-27 17:21:40.930:W/System.err(26577):java.lang.NumberFormatException:无效int:“201308271721” 08-27 17:21:40.960:W/System.err(26577):位于java.lang.Integer.invalidit(Integer.java:138) 08-27 17:21:40.960:W/System.err(26577):位于java.lang.Integer.parse(Integer.java:378) 08-27 17:21:40.960:W/System.err(26577):位于java.lang.Integer.parseInt(Integer.java:366) 08-27 17:21:40.960:W/System.err(26577):位于java.lang.Integer.parseInt(Integer.java:332) 08-27 17:21:40.960:带系统错误(26577):在 xxx.xxx.RoosterSync.onHandleContent(RoosterSync.java:118) 08-27 17:21:40.960:W/System.err(26577):位于android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) 08-27 17:21:40.960:W/System.err(26577):位于android.os.Handler.dispatchMessage(Handler.java:99) 08-27 17:21:40.960:W/System.err(26577):位于android.os.Looper.loop(Looper.java:137) 08-27 17:21:40.960:W/System.err(26577):在android.os.HandlerThread.run(HandlerThread.java:60)上

我正在使用的(和平)代码:

Date now = new Date();
String nowAsString = new SimpleDateFormat("yyyyMMddHHmm").format(now);
dayNow= Integer.parseInt(nowAsString);
是否可以将日期字符串转换为整数


或者我做了一些愚蠢的事情,有没有更简单的方法?

这个值对于32位整数来说太长了。请改用长的

有关类型限制,请参阅

一个相关的问题是,为什么要以这种格式存储日期时间。UNIX时间戳(顺便说一句,应该是
长的
)更便于存储和操作-请参阅Calendar.getTimeInMillis


这是一种允许轻松进行日期比较和存储的格式。

此值对于32位整数来说太长。请改用长的

有关类型限制,请参阅

一个相关的问题是,为什么要以这种格式存储日期时间。UNIX时间戳(顺便说一句,应该是
长的
)更便于存储和操作-请参阅Calendar.getTimeInMillis

这是一种允许轻松进行日期比较和存储的格式。

试试看

日期日期=新日期(Long.parseLong(rowItem.getDatetime())

试试看

日期日期=新日期(Long.parseLong(rowItem.getDatetime())


你应该学习java基础知识,我觉得这是一个相当无辜的错误。不要认为它值得投票表决,你应该学习java基础知识,我觉得这是一个相当无辜的错误。我认为int可以做得更多。谢谢,我打算改用长号!我认为int可以做得更多。谢谢,我打算改用长号!