如何比较日期类中的两个日期 [英] How do I compare two dates in my Date Class

查看:63
本文介绍了如何比较日期类中的两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//我正在努力弄清楚如何比较班上的两个日期。在我的DateClient中,我创建了两个日期,但不知道如何使用它们。如果你想知道我正在创建一个日期类来比较日期,以便找出//很多天前的那个日期以及它在哪一天。

//日期d1 =新日期( 2015,10,20);

//日期d2 =新日期(2000,3,12);



公共课日期{



private int year;

private int day;

private int month;



public Date(int year,int month,int day){

// this.year = year;

// this .month = month;

// this.day = day;

year = 0;

month = 0;

day = 0;

}

public int getYear(){

返回年份;

}

public void setYear(){

this.year = year;

}

public int getMonth() {

返回月份;

}

public void setMonth(){

this.month = month;

}

public int getDay(){

返回日;

}

public void setDay(){

this.day = day;

}

public String toString(){

返回(年+/+月+/+天);

}

公共布尔值等于(日期D){

boolean Same = false;

if(day == this.day&&月== this.month&& year == this.year){

Same = true;

} else Same = false;

return Same;

}

public boolean isLeapYear(int year){

if(year%4 == 0&& year%100!= 0 || year% 400 == 0){

返回true;

}否则返回false;



}

public void nextDay(){

day ++;

if(month == 1 || month == 3 || month == 5 || month) == 7 || month == 8 || month == 10&& day == 32){

month ++;

day = 1;

}否则if(month == 4 || month == 6 || month == 9 || month == 11&& day == 31){

month ++ ;

day = 1;

}否则如果(月== 12&& day == 32){

month = 1 ;

年++;

天= 1;

}否则if(month == 2){

if (isLeapYear(year)== true&& day == 30){

day = 1;

}否则if(isLeapYear(ye) ar)== false&& day == 29){

day = 1;



}

}

}

public int advanceTo(Date endDay){

int countDays = 0;

while(!= endDay){

countDay ++;

}

}



}





}

//I am having troubles trying to figure out how to compare two dates in my class. In my DateClient, I created two dates, but don't know how to use them. If you're wondering I'm creating a date class to compare the dates to find how //many days ago was that date and on what day was it.
//Date d1 = new Date(2015,10,20);
//Date d2 = new Date(2000,3,12);

public class Date {

private int year;
private int day;
private int month;

public Date(int year, int month, int day){
// this.year = year;
// this.month = month;
// this.day = day;
year=0;
month=0;
day=0;
}
public int getYear(){
return year;
}
public void setYear(){
this.year=year;
}
public int getMonth(){
return month;
}
public void setMonth(){
this.month=month;
}
public int getDay(){
return day;
}
public void setDay(){
this.day=day;
}
public String toString(){
return (year+"/"+month+"/"+day);
}
public boolean equals(Date D){
boolean Same = false;
if (day==this.day && month==this.month && year==this.year){
Same = true;
} else Same = false;
return Same;
}
public boolean isLeapYear(int year){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
return true;
} else return false;

}
public void nextDay(){
day++;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 && day == 32){
month++;
day = 1;
} else if (month == 4 || month == 6 || month == 9 || month == 11 && day == 31){
month++;
day = 1;
} else if (month == 12 && day == 32){
month = 1;
year++;
day = 1;
} else if (month == 2){
if (isLeapYear(year) == true && day == 30){
day = 1;
} else if (isLeapYear(year) == false && day == 29){
day = 1;

}
}
}
public int advanceTo(Date endDay){
int countDays = 0;
while ( != endDay){
countDay++;
}
}

}


}

<pre lang="java"><pre lang="java"><pre lang="java"><pre lang="java">

推荐答案

您可以使用 Calender.before() Calender.after() Calender.equals()就像这样:

You can use Calender.before(), Calender.after() and Calender.equals() like this way:
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);

if(cal1.after(cal2)){
    System.out.println("Date1 is after Date2");
}
if(cal1.before(cal2)){
    System.out.println("Date1 is before Date2");
}
if(cal1.equals(cal2)){
     System.out.println("Date1 is equal Date2");
}


处理日期并非易事。



首先,我记得那个Java有它的类来处理日期,在java.times包中可能是
(也许是太多的类...)

如果你真的想要手动处理它们......





通常最好保持日期的内部表示,例如,自给定日期以来的天数(例如1/1/1900)或者如果您对时间感兴趣也可以是毫秒数。



让我们称这个变量为intdate< br $>


private int intdate;



让我们导出这个值,以便从其他对象中使用。



public int getIntdate(){

返回intdate;

}



之后你应该有一个构造函数,它可以在年,月和日创建一个基于

的新日期:



public mydate(int year,in t月,int day){

this.intdate = ... //我留给你!!

}



另一个接受内部表示的构造函数:

public mydate(int adate){

this.intdate = adate;

}





完成后,第二天和prevday的函数应返回一个新的

对象(最好不要修改当前的那个)





public mydate nextday()

返回新的mydate(这个intdate + 1);

}



public mydate prevday(){

返回新的mydate(这。 intdate-1);

}





您可以获得未来N天的日期或在过去只需添加

a数到当前值,并返回一个新的日期





/ /通过添加正数或负数天来计算未来或过去的日期

public my date addDays(int days){

返回新的mydate(this.intdate +天);

}





迄今为止的功能提前无用。它的目的是什么?





您可以通过简单地评估内部表示的差异来计算两个日期之间的天数。 />




//返回otherdate和此实例之间的天数

//如果otherdate大于这个

public int daysTo(mydate otherdate){

返回otherdate.getIntdate() - this.intdate;

}



您可以比较仅测试内部价值的日期



//如果此日期等于另一个日期则返回

public boolean isEqual(mydate otherdate){

返回this.intdate == otherdate.getIntdate();

}



//如果此日期小于另一个日期则返回true

public boolean isLessThan(mydate otherdate){

返回此日期。 intdate< otherdate.getIntdate();

}



//如果此日期大于另一个日期则返回true

public boolean isGreaterThan(mydate otherdate){

返回this.intdate> otherdate.getIntdate();

}





我留给你的是建立内部价值给出日,月,年

并从内部价值获得返回日,月或年



public int day {

从intdate中提取并返回当天...

}



public int month {

从intdate中提取并返回月份...

}



public int year {

从intdate中提取并返回年份...

}



因此您的初始问题以这种方式解决:

mydate d1 = new mydate(2015,10,20);

mydate d2 = new mydate(2000,3,12);



int difference = d2.daysTo(d1);
Handling dates is not an easy task.

First of all, I remeber you that that Java has its classes to handle dates,
in the package java.times (perhaps too many classes ...)
If you really want to handle them "by hand" ...


Usually is better to maintain an internal representation of the date as e.g., the number of days since a given date (e.g. 1/1/1900) or the number of milliseconds if you are interested also on times.

let's call this variable "intdate"

private int intdate;

And let's export this value to be used from other objects.

public int getIntdate (){
return intdate;
}

After that you should have a constructor that creates a new date based
on year, month and day :

public mydate (int year, int month, int day) {
this.intdate = ... // I leave this to you !!
}

And another constructor that accept the internal representation :
public mydate (int adate) {
this.intdate = adate;
}


Having done that, the functions nextday and prevday should return a new
object (better not to modify the current one)


public mydate nextday ()
return new mydate (this intdate+1);
}

public mydate prevday () {
return new mydate (this.intdate-1);
}


You can obtain a date of N days in the future or in the past by simply adding
a number to the current value, and returning a new date


// calculate a date in the future or in the past by adding a positive or negative number of days
public mydate addDays (int days) {
return new mydate (this.intdate + days);
}


The function advance to date is useless. what's its purpose ?


You can calculate the number of days between two dates by simply evaluating the difference of the internal representations.


// returns the number of days between otherdate and this instance
// positive if otherdate is greater than this
public int daysTo (mydate otherdate){
return otherdate.getIntdate() - this.intdate;
}

you can compare dates just testing the internal values

// return true if this date is equal to the other date
public boolean isEqual (mydate otherdate){
return this.intdate == otherdate.getIntdate();
}

// return true if this date is less then the other date
public boolean isLessThan (mydate otherdate){
return this.intdate < otherdate.getIntdate();
}

// return true if this date is greater then the other date
public boolean isGreaterThan (mydate otherdate){
return this.intdate > otherdate.getIntdate();
}


All I leave to you is to build the internal value given day, month, year
and to obtain back day, month or year from the internal value

public int day {
extract and return the day from the intdate ...
}

public int month {
extract and return the month from the intdate ...
}

public int year {
extract and return the year from the intdate ...
}

so your initial question is solved in this way :
mydate d1 = new mydate (2015,10,20);
mydate d2 = new mydate (2000,3,12);

int difference = d2.daysTo(d1);


这篇关于如何比较日期类中的两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆