展开全部
看我滴~~~~~~~~
完全按照楼主所给思路来的32313133353236313431303231363533e59b9ee7ad9431333335316537:import java.util.Scanner;
/**
* Input:
*
* Enter a date int the format yyyy/mm/dd;
*
* 1999/12/25
*
* Enter a time in the format hh:mm:ss;
*
* 11:03:25
*
* What excel date do you want it converted to? 1) Windows 2) Mac
*
* 1
*
* Output:
*
* Year: 1999 Month:12 Day:25
*
* Hour:11 Minute:3 second:25
*
* Total seconds:39805.0
*
* The Windows Excel date/time is 36519.460706018515
*
* End of Processing
*
* @author xiaobo
*
* 思路:
*
* 1.计算到目前为止的闰年数
*
* leap=1+min(year-?,0)+【(year-?)/4】-【(year-?)/100】+【(year-?+300)/400】
*
* 其中【】表示向下取整,?表示1900或1904
*
* 2.计算该年到这个月为止的天数
*
* d=【-1.63+(month-1)*30.6】
*
* 3.计算总的天数
*
* result=day+(year-?)*365+leap+d
*
*/
public class CalculateSeconds {
public static void main(String []args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a date int the format yyyy/mm/dd");
String ymd = in.nextLine();
System.out.println("Enter a time in the format hh:mm:ss");
String hms = in.nextLine();
System.out
.println("What excel date do you want it converted to? 1) Windows 2) Mac");
String wm = in.nextLine();
String[] arrStr1 = ymd.split("/");
String[] arrStr2 = hms.split(":");
int year = Integer.parseInt(arrStr1[0]);
if (wm.equals("1")) {
year -= 1900;
} else if (wm.equals("0")) {
year -= 1904;
} else {
System.err.println("error");
System.exit(-1);
}
int leap = (int) (1 + Math.min(year, 0) + Math.floor(year / 4)
+ Math.floor(year / 100) + Math.floor((year + 300) / 400));
int d = (int) Math.floor(-1.63 + (Integer.parseInt(arrStr1[1]) - 1)
* 30.6);
int result = Integer.parseInt(arrStr1[2]) + year * 365 + leap + d;
System.out.println("Year:" + Integer.parseInt(arrStr1[0]) + " Month:"
+ Integer.parseInt(arrStr1[1]) + " Day:"
+ Integer.parseInt(arrStr1[2]));
System.out.println("Hour:" + Integer.parseInt(arrStr2[0]) + " Minute:"
+ Integer.parseInt(arrStr2[1]) + " second:"
+ Integer.parseInt(arrStr2[2]));
System.out.println("Total seconds:"
+ (Integer.parseInt(arrStr2[0]) * 3600
+ Integer.parseInt(arrStr2[1]) * 60 + Integer
.parseInt(arrStr2[2])));
if (wm.equals("1")) {
System.out.print("The Windows Excel date/time is ");
} else {
System.out.print("The Mac Excel date/time is ");
}
System.out.println(result + (Integer.parseInt(arrStr2[0]) * 3600
+ Integer.parseInt(arrStr2[1]) * 60 + Integer
.parseInt(arrStr2[2])) / (24 * 3600.0));
System.out.println("End of Processing");
}
}
结果如图: