MPAndroid 日期格式和解析丢失精度 [英] MPAndroid Date Formatting and Parsing Loses Precision
问题描述
我将 MPAndroid 库用于我的 android 应用程序的图表.我在条目中输入了一个很长的值.我的问题是,当我格式化 x 轴的值时,它使用浮点值而不是长值,因此它会失去精度.
I'm using the MPAndroid library for the graph of my android app. I have a long value inputted to the entry. My problem is that when I format the values of x axis, it uses float value instead of long so it loses precision.
这是我的图表入口代码:
Here is my code for the entry to the graph:
String dateString = "02/13/2019(11:23:45)";
long readingDate = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy(kk:mm:ss)", Locale.US);
Date date = sdf.parse(dateString);
readingDate = date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
SensorData.add(new Entry(readingDate, 1.5);
这是我格式化 x 轴的代码:
Here is my code for formatting the x axis:
private class XAxisValueFormatter implements IAxisValueFormatter {
@Override
public String getFormattedValue(float value, AxisBase axis) {
String dateString = new SimpleDateFormat("MM/dd/yyyy(kk:mm:ss)", Locale.US).format(value);
return dateString;
}
}
我该如何解决这个问题?
How can I fix this problem?
推荐答案
一个可能的解决方案是让你的第一个点保持 0 值.
One of possible solutions would be to make your first point hold 0 value.
基本上,您需要从 Entry
的每个新 x 值中减去 start_timestamp,这样图表将从 0 值而不是时间戳开始.
Basically you need subtract start_timestamp from every new x value of Entry
so chart will be started from 0 value and not of timestamp.
例如.
让我们假设 start_timestamp
变量保存您的第一个数据点时间戳.
Let's assume start_timestamp
variable holds your first timestamp of datapoints.
要添加新条目,请使用:
To add new entry use:
SensorData.add(new Entry(readingDate - start_timestamp, 1.5);
要格式化 x 值,您只需将 start_timestamp 添加到提供的 x 值.
To format x value you just add start_timestamp to provided x value.
这篇关于MPAndroid 日期格式和解析丢失精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!