用Java程序将日期转换为序列号,就像在Excel中一样 [英] Program in Java to convert a date to serial number as done in Excel
本文介绍了用Java程序将日期转换为序列号,就像在Excel中一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经编写了一个函数,但没有给出实际的O / P ...
I have written a function but it does not give an actual 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);
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;
}
主类
Main class
p s v main(String args[]){
CommonFunctionsImpl cmp= new CommonFunctionsImpl();
Date date1 = null;
try {
date1 = new SimpleDateFormat("MM/dd/yy").parse("05/18/2008");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("date-----"+cmp.date(date1));
}
输出 date- ---- 39556
在Excel中 DATE(2008,5,18) = 39586.00
我的程序O / P实际上与Excel O / P不匹配。
My program O/P doesn't actually match with Excel O/P.
推荐答案
正确的算法已在Apache POI中实现。
看一下类 org.apache。 poi.ss.usermodel.DateUtil 。
The right algorithm is already implemented in Apache POI. Take a look at class org.apache.poi.ss.usermodel.DateUtil.
此描述也可能有用: http://support.microsoft.com/kb/214094/en-us
这篇关于用Java程序将日期转换为序列号,就像在Excel中一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文