请高手帮忙编写一个java程序,不胜感激! 10
8.设intyear,month,day分别表示一个日期中的年月日,试编程求a)对于任意三个整数,判断其是否为一个合法的日期值。b)给定一个日期值,计算若干天后的日期值c...
8. 设int year,month,day分别表示一个日期中的年月日,试编程求
a) 对于任意三个整数,判断其是否为一个合法的日期值。
b) 给定一个日期值,计算若干天后的日期值
c) 如果有两个日期值,计算他们相距的天数。
请高手不吝赐教,帮帮我这个菜鸟... 展开
a) 对于任意三个整数,判断其是否为一个合法的日期值。
b) 给定一个日期值,计算若干天后的日期值
c) 如果有两个日期值,计算他们相距的天数。
请高手不吝赐教,帮帮我这个菜鸟... 展开
2个回答
展开全部
import java.util.Calendar;
public class Date {
private int year;
private int month;
private int day;
public Date(int year, int month, int date){
this.year = year;
this.month = month;
this.day = date;
}
public static void main(String[] args){
boolean isValid = Date.isValidatDate(2011, 2, 29);
System.out.println("20110229 is " + (isValid?"": "not") + " a date");
Date dt = new Date(2011, 2, 28);
String nextDay = dt.getNextDate(30);
System.out.println("After 30 days of 20110228, the date is: " + nextDay);
Date dt2 = new Date(2010, 2,27);
long julianDays = Date.getJulianDay(dt, dt2);
System.out.println("Days between 20100227 and 20110228 is: " + julianDays);
}
//检查日期方法
public static boolean isValidatDate(int y, int m, int d){
//如果年月日小于0,或者月份大于12,或者日期大于31,都为false
if(m > 12 || d > 31 || y <= 0 || m < 0 || d <= 0){
return false;
}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, y);
cal.set(Calendar.MONTH, m-1);
int maxDayOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
return d <= maxDayOfMonth;
}
//获得days后的日期
public String getNextDate(int days){
Calendar cal = Calendar.getInstance();
cal.set(year, month-1, day);
cal.add(Calendar.DATE, days);
int nextYear = cal.get(Calendar.YEAR);
int nextMonth = cal.get(Calendar.MONTH);
int nextDay = cal.get(Calendar.DATE);
return nextYear +"/" + (nextMonth + 1) + "/" +nextDay;
}
//求取两个日期之间相差得天数
public static long getJulianDay(Date dt1, Date dt2){
java.util.Date date1 = new java.util.Date(dt1.year, dt1.month-1, dt1.day-1);
java.util.Date date2 = new java.util.Date(dt2.year, dt2.month-1, dt2.day-1);
long millionSecondsDiffer = Math.abs(date1.getTime() - date2.getTime());
return millionSecondsDiffer / (1000*24*60*60);
}
}
------------------
20110229 is not a date
After 30 days of 20110228, the date is: 2011/3/30
Days between 20100227 and 20110228 is: 366
public class Date {
private int year;
private int month;
private int day;
public Date(int year, int month, int date){
this.year = year;
this.month = month;
this.day = date;
}
public static void main(String[] args){
boolean isValid = Date.isValidatDate(2011, 2, 29);
System.out.println("20110229 is " + (isValid?"": "not") + " a date");
Date dt = new Date(2011, 2, 28);
String nextDay = dt.getNextDate(30);
System.out.println("After 30 days of 20110228, the date is: " + nextDay);
Date dt2 = new Date(2010, 2,27);
long julianDays = Date.getJulianDay(dt, dt2);
System.out.println("Days between 20100227 and 20110228 is: " + julianDays);
}
//检查日期方法
public static boolean isValidatDate(int y, int m, int d){
//如果年月日小于0,或者月份大于12,或者日期大于31,都为false
if(m > 12 || d > 31 || y <= 0 || m < 0 || d <= 0){
return false;
}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, y);
cal.set(Calendar.MONTH, m-1);
int maxDayOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
return d <= maxDayOfMonth;
}
//获得days后的日期
public String getNextDate(int days){
Calendar cal = Calendar.getInstance();
cal.set(year, month-1, day);
cal.add(Calendar.DATE, days);
int nextYear = cal.get(Calendar.YEAR);
int nextMonth = cal.get(Calendar.MONTH);
int nextDay = cal.get(Calendar.DATE);
return nextYear +"/" + (nextMonth + 1) + "/" +nextDay;
}
//求取两个日期之间相差得天数
public static long getJulianDay(Date dt1, Date dt2){
java.util.Date date1 = new java.util.Date(dt1.year, dt1.month-1, dt1.day-1);
java.util.Date date2 = new java.util.Date(dt2.year, dt2.month-1, dt2.day-1);
long millionSecondsDiffer = Math.abs(date1.getTime() - date2.getTime());
return millionSecondsDiffer / (1000*24*60*60);
}
}
------------------
20110229 is not a date
After 30 days of 20110228, the date is: 2011/3/30
Days between 20100227 and 20110228 is: 366
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询