【发布时间】:2013-04-04 14:46:00
【问题描述】:
我写了一个函数,但它没有给出实际的 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;
}
主类
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 不匹配。
【问题讨论】:
-
我不是 java 但
39556意味着你得到的是4/18/2008而不是5/18/2008(39586)也许这是一个线索,你可以通过代码来看看为什么它把它当作第 4 个月? -
@siddharth- 你能写一个算法来实现它吗?我想你可以.. :)
-
是的,我可以,但它会在 VB.Net/VBA/VB6/C# 中,因为我不知道 java。而且我不确定它对你有什么帮助,因为语法太不同了?
-
语法无关紧要...请提供任何你喜欢的东西!
-
我看到你已经选择了你的答案。还需要我回复吗?