Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

org.ttzero.excel.util.DateUtil.toDate(double d)转换算法是秒级的,会出现比实际少一秒的情况 #232

Closed
PeZQ opened this issue Dec 21, 2021 · 7 comments
Labels

Comments

@PeZQ
Copy link

PeZQ commented Dec 21, 2021

image
double v=44248.466099537;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(v);
Date d1= org.ttzero.excel.util.DateUtil.toDate(v);
String dStr = sdf.format(d);
String d1Str=sdf.format(d1);
System.out.println(String.format("dStr=%s",dStr));
System.out.println(String.format("d1Str=%s",d1Str));

@wangguanquan
Copy link
Owner

感谢你的反馈!暂时还没有找出原因,猜想是double精度引起。

临时的可以从row中获取double值然后再使用poi的静态方法转时间。

	try (ExcelReader reader = ExcelReader.read(Paths.get("./2.xlsx"))) {
		List<Item> items = reader.sheet(0).dataRows().map(row -> {
			Item e = new Item();
			e.setBeginTime(org.apache.poi.ss.usermodel.DateUtil.getJavaDate(row.getDouble("开始时间")));
			// ... 其它列
			
			return e;
		}).collect(Collectors.toList());
	} catch (IOException e) {
		e.printStackTrace();
	}

@PeZQ
Copy link
Author

PeZQ commented Dec 21, 2021

image
int n = (int) d, m = (int) (((d - n) * SECOND_OF_DAY*1000+0.5)/1000);

@PeZQ
Copy link
Author

PeZQ commented Dec 21, 2021

感谢你的反馈!暂时还没有找出原因,猜想是double精度引起。

临时的可以从row中获取double值然后再使用poi的静态方法转时间。

	try (ExcelReader reader = ExcelReader.read(Paths.get("./2.xlsx"))) {
		List<Item> items = reader.sheet(0).dataRows().map(row -> {
			Item e = new Item();
			e.setBeginTime(org.apache.poi.ss.usermodel.DateUtil.getJavaDate(row.getDouble("开始时间")));
			// ... 其它列
			
			return e;
		}).collect(Collectors.toList());
	} catch (IOException e) {
		e.printStackTrace();
	}

把秒级的转换成毫秒级,加上0.5
int n = (int) d, m = (int) (((d - n) * SECOND_OF_DAY*1000+0.5)/1000);

@wangguanquan
Copy link
Owner

欢迎提交PR贡献你的代码!!!

步骤:

  1. fork 本项目
  2. git clone https://github.com/{your}/eec拉取fork后的代码
  3. 新建分支,最好标记issue编号,像这样 git checkout -b fix#232
  4. 修改代码,并添加测试用例
  5. 提交前运行测试代码 mvn clean test
  6. 确认无误后提交代码,并创建Pull Request,等待合并

@wangguanquan
Copy link
Owner

感觉加0.5不安全,加0.5应该会出现多1秒的情况?

@PeZQ
Copy link
Author

PeZQ commented Dec 21, 2021

感觉加0.5不安全,加0.5应该会出现多1秒的情况?

看POI的算法是毫秒级别加了0.5

@wangguanquan
Copy link
Owner

可以先提一个PR,我有空验证一下。3Q!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants