Java程序将日期转换为Excel中的序列号
我已经写了一个函数,但它没有给出一个实际的O/pJava程序将日期转换为Excel中的序列号,java,excel,date,Java,Excel,Date,我已经写了一个函数,但它没有给出一个实际的O/p public int date(Object O) { if (O instanceof Date) { Date d1 = (Date) O; Calendar cal = Calendar.getInstance(); cal.setTime(d1); int dd, mm, yy; dd = cal.get(Calendar.DAY_OF_MONTH);
public int date(Object O) {
if (O instanceof Date) {
Date d1 = (Date) O;
Calendar cal = Calendar.getInstance();
cal.setTime(d1);
int dd, mm, yy;
dd = cal.get(Calendar.DAY_OF_MONTH);
mm = cal.get(Calendar.MONTH);
yy = cal.get(Calendar.YEAR);
if (dd == 29 && mm == 02 && yy == 1900)
return 60;
long nSerialDate = ((1461 * (yy + 4800 + ((mm - 14) / 12))) / 4)
+ ((367 * (mm - 2 - 12 * ((mm - 14) / 12))) / 12)
- ((3 * (((yy + 4900 + ((mm - 14) / 12)) / 100))) / 4) + dd
- 2415019 - 32075;
if (nSerialDate < 60) {
// Because of the 29-02-1900 bug, any serial date
// under 60 is one off... Compensate.
nSerialDate--;
}
return (int) nSerialDate;
}
return -1;
}
输出日期------39556
Excel中的日期(2008,5,18)=39586.00
我的程序O/p实际上与Excel O/p不匹配。正确的算法已经在Apache POI中实现。 看看课堂
这个描述也很有用:正确的算法已经在ApachePOI中实现了。 看看课堂 此外,此描述可能很有用:尝试以下操作:
Date begin = new Date(0, 0, 1); // 1900 based date
Date date = new Date(108, 4, 18); // your date
// There is 2 days offset from the calculated date and what you
// expect so I added it on the result
System.out.println(2+(date.getTime()-begin.getTime())/(1000*60*60*24));
请参阅《JavaDoc&&Excel函数指南》尝试以下操作:
Date begin = new Date(0, 0, 1); // 1900 based date
Date date = new Date(108, 4, 18); // your date
// There is 2 days offset from the calculated date and what you
// expect so I added it on the result
System.out.println(2+(date.getTime()-begin.getTime())/(1000*60*60*24));
请参阅《JavaDoc&&Excel函数指南》程序中的问题是: 在程序中,变量
mm(月)
为4,O/p仅为第4个月。
(一年的第一个月是0)
要解决此问题,需要增加月值
dd = cal.get(Calendar.DAY_OF_MONTH);
mm = cal.get(Calendar.MONTH);
yy = cal.get(Calendar.YEAR);
//Solution for the issue
mm++;
if (dd == 29 && mm == 02 && yy == 1900)
return 60;
long nSerialDate = ((1461 * (yy + 4800 + ((mm - 14) / 12))) / 4)
+ ((367 * (mm - 2 - 12 * ((mm - 14) / 12))) / 12)
- ((3 * (((yy + 4900 + ((mm - 14) / 12)) / 100))) / 4) + dd
- 2415019 - 32075;
程序中的问题是: 在程序中,变量
mm(月)
为4,O/p仅为第4个月。
(一年的第一个月是0)
要解决此问题,需要增加月值
dd = cal.get(Calendar.DAY_OF_MONTH);
mm = cal.get(Calendar.MONTH);
yy = cal.get(Calendar.YEAR);
//Solution for the issue
mm++;
if (dd == 29 && mm == 02 && yy == 1900)
return 60;
long nSerialDate = ((1461 * (yy + 4800 + ((mm - 14) / 12))) / 4)
+ ((367 * (mm - 2 - 12 * ((mm - 14) / 12))) / 12)
- ((3 * (((yy + 4900 + ((mm - 14) / 12)) / 100))) / 4) + dd
- 2415019 - 32075;
你能给出返回序列号的api名称吗?你需要的方法是public static double getExcelDate(java.util.Date Date)一个简单的Apache POI下载搜索给你:你能给出返回序列号的api名称吗?你需要的方法是public static double getExcelDate(java.util.Date)对ApachePOI下载的简单搜索会告诉你:我不喜欢java,但
39556
意味着你得到的是4/18/2008
,而不是5/18/2008(39586)
,也许这是一个线索,你可以一步一步地浏览代码,看看为什么把它作为第四个月?@siddharth-你能写一个算法来实现它吗?我想你可以……)是的,我可以,但它将在VB.Net/VBA/VB6/C中,因为我不懂java。我不知道这对你有什么帮助,因为语法太不一样了。语法不重要。。。请给我任何你觉得舒服的东西!我看你已经选择了答案。你还需要我发布答案吗?我不喜欢java,但39556
意味着你得到的是4/18/2008
,而不是5/18/2008(39586)
也许这是一个线索,你可以逐步浏览代码,看看为什么把它作为第四个月?@siddharth-你能写一个算法来实现它吗?我想你可以……)是的,我可以,但它将在VB.Net/VBA/VB6/C中,因为我不懂java。我不知道这对你有什么帮助,因为语法太不一样了。语法不重要。。。请给我任何你觉得舒服的东西!我看你已经选择了答案。你还需要我发布答案吗?