java.lang.NumberFormatException:对于输入字符串:"2019-11-27" [英] java.lang.NumberFormatException: For input string: “2019-11-27”
问题描述
在我的应用中,我从API服务器获取的日期为 String 值,我想将其解析为 int ,但是出现此错误:
In my app I am getting date from an API server as String value and I want to parse it to int but I get this error:
Caused by: java.lang.NumberFormatException: For input string: "2019-11-27"
at java.lang.Integer.parseInt(Integer.java: 521)
at java.lang.Integer.parseInt(Integer.java: 556)
我正在尝试解析为 int
.我想将其传递给 BarEntry
类构造函数,它仅接受int值或将新的BarEntry浮动(int或float值,浮动).我需要它来显示图表.
I am trying to parse into an int
. I want to pass it to BarEntry
class constructor and it takes only int value or float new BarEntry(int or float values,float). I need it for showing Chart.
我的活动
try {
Response response = client.newCall(request).execute();
Log.d("Response", response.toString());
JSONObject object = new JSONObject(Objects.requireNonNull(response.body()).string());
JSONObject rates = object.getJSONObject("rates");
Iterator < String > iterator = rates.keys();
while (iterator.hasNext()) {
String keyDate = iterator.next(); // this date which i want to parse to int value
String cad = rates.getJSONObject(keyDate).getString("CAD");
@SuppressLint("DefaultLocale") String value = String.format("%.4s", cad);
float value1 = Float.parseFloat(value);
int date = Integer.parseInt(keyDate);
Log.d("TAG", date + "");
//Log.d("TAG", value1 + "");
//barEntries.add(new BarEntry(keyDate, value1));
}
} catch (IOException | JSONException e) {
e.printStackTrace();
Log.d("ChartActivity", e.toString());
}
推荐答案
LocalDate和ThreeTenABP
String dateString = "2019-11-27";
LocalDate date = LocalDate.parse(dateString);
int epochDay = (int) date.toEpochDay();
System.out.println(epochDay);
此代码段输出:
18227
文档说明:
纪元日计数是第0天的简单递增计数是1970-01-01(ISO).
The Epoch Day count is a simple incrementing count of days where day 0 is 1970-01-01 (ISO).
所以我的建议是该数字适合输入 BarEntry
构造函数.
So my suggestion is that this number is fine for feeding into your BarEntry
constructor.
toEpochDay()
返回 long
.如果您的构造函数不接受 long
,请转换为 int
.在上面的代码中,我做了一个简单的转换.风险是,如果在遥远的将来或遥远的日期发生 int
溢出,我们将得到非常错误的结果.我宁愿进行范围检查来避免这种情况:
toEpochDay()
returns a long
. If your constructor doesn’t accept a long
, convert to int
. In the code above I did a simple cast. The risk is that we will get a very wrong result in case of int
overflow for dates in the far future or the far past. I prefer to do a range check to avoid that:
long epochDayLong = date.toEpochDay();
if (epochDayLong < Integer.MIN_VALUE || epochDayLong > Integer.MAX_VALUE) {
throw new IllegalStateException("Date " + date + " is out of range");
}
int epochDay = (int) epochDayLong;
结果与以前相同.此检查与我在评论中提到的 Math.toIntExact
方法所做的检查相同(可从Android API级别24获得).
The result is the same as before. This check is the same check that the Math.toIntExact
method I mentioned in a comment does (available from Android API level 24).
我已将值 18227 转换为正常日期,并给出了日期是 1970/01/01 的年份,而在JSON中是 2019-11-27 的原因?和我该如何纠正?
I had converted this value 18227 to normal date and it gives date of this year 1970/01/01 and in JSON it's 2019-11-27 why? and how should i correct it?
让我猜,您实际上做了 new Date(18227)
.我的建议是,您完全避免使用 Date
类,并坚持使用Java.time(现代Java日期和时间API).您为何获得1970的原因是:18227是自该纪元以来的 days 天,而 Date
则是毫秒(自该纪元00:00 UTC开始)日).因此,您当天获得了00:00:18.227 UTC.上面的代码中已经有一个 LocalDate
,因此只需使用它即可.
Let me guess, you effectively did new Date(18227)
. My suggestion is that you avoid the Date
class completely and stick to java.time, the modern Java date and time API. Why you got 1970 is: 18227 is a count of days since the epoch, and Date
counts milliseconds (since 00:00 UTC on the epoch day). So you got 00:00:18.227 UTC on that day. We already have a LocalDate
in the above code, so just use that.
System.out.println(date);
2019-11-27
2019-11-27
如果您需要以相反的方式进行转换,那么当您知道如何进行转换时很容易:
Should you need to convert the opposite way, it’s easy when you know how:
LocalDate convertedBack = LocalDate.ofEpochDay(epochDay);
结果是具有相同值的 LocalDate
.
java.time在旧的和更新的Android设备上均可正常运行.它只需要至少 Java 6 .
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
- 在Java 8和更高版本以及更新的Android设备(API级别26起)中,内置了现代API.
- 在非Android Java 6和7中,获得ThreeTen反向端口,即现代类的反向端口(JSR 310的ThreeTen;请参见底部的链接).
- 在(较旧的)Android上,请使用Android版的ThreeTen Backport.称为ThreeTenABP.并确保使用子包从
org.threeten.bp
导入日期和时间类.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
-
LocalDate的文档.toEpochDay()
- Oracle教程:日期时间解释了如何使用java.time./li>
- Java规范请求(JSR)310 ,其中
java首先描述了.time
. - ThreeTen Backport项目,即
java.time
的反向端口Java 6和7(JSR-310的ThreeTen). - ThreeTenABP ,ThreeTen Backport的Android版本
- 问题:如何在Android Project中使用ThreeTenABP ,非常详尽的解释.
- Documentation of
LocalDate.toEpochDay()
- Oracle tutorial: Date Time explaining how to use java.time.
- Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
这篇关于java.lang.NumberFormatException:对于输入字符串:"2019-11-27"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!