面向对象程序设计,高手帮帮忙。

2024-12-28 14:04:21
推荐回答(2个)
回答1:

楼主想要什么语言的代码呀,我这里有个java的,你看下如何

// 生成的对象为1970年1月1日

public static Date getDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format("1970-01-01");
ParsePosition pos = new ParsePosition(8);
Date currentTime_2 = formatter.parse(dateString, pos);
return currentTime_2;
}

// 两个对象变量的减法运算函数,返回的是减法的天数

public static int daysxiangcha(String dateStr1, String dateStr2) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
try {
dateStr1 = sdf.format(sdf2.parse(dateStr1));
dateStr2 = sdf.format(sdf2.parse(dateStr2));
} catch (ParseException e) {
e.printStackTrace();
}
int year1 = Integer.parseInt(dateStr1.substring(0, 4));
int month1 = Integer.parseInt(dateStr1.substring(4, 6));
int day1 = Integer.parseInt(dateStr1.substring(6, 8));
int year2 = Integer.parseInt(dateStr2.substring(0, 4));
int month2 = Integer.parseInt(dateStr2.substring(4, 6));
int day2 = Integer.parseInt(dateStr2.substring(6, 8));
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.YEAR, year1);
c1.set(Calendar.MONTH, month1 - 1);
c1.set(Calendar.DAY_OF_MONTH, day1);
Calendar c2 = Calendar.getInstance();
c2.set(Calendar.YEAR, year2);
c2.set(Calendar.MONTH, month2 - 1);
c2.set(Calendar.DAY_OF_MONTH, day2);
long mills =
c1.getTimeInMillis() > c2.getTimeInMillis()
? c1.getTimeInMillis() - c2.getTimeInMillis()
: c2.getTimeInMillis() - c1.getTimeInMillis();
return (int) (mills / 1000 / 3600 / 24);
}

回答2:

写的什么啊?