Java 尝试创建一个日期类,该类在实现时将日期提前一个 我不想担心闰年,但我担心我的基础是否正确。
导入javax.swing.SpringJava 尝试创建一个日期类,该类在实现时将日期提前一个 我不想担心闰年,但我担心我的基础是否正确。,java,eclipse,class,Java,Eclipse,Class,导入javax.swing.Spring public class Date { /** * A variable to hold the day value */ private int day; /** * A variable to hold the month value */ private int month; /** * A variable to hold the year valu
public class Date
{
/**
* A variable to hold the day value
*/
private int day;
/**
* A variable to hold the month value
*/
private int month;
/**
* A variable to hold the year value
*/
private int year;
/**
* the date in the form m/d/yyyy
*/
private String stringDate;
/**
* To figure out which month it is
*/
private boolean isThirtyDays, isThirtyOneDays, isTwentyEightDays;
/**
* advances the date by one day based on the month
*/
private void advance()
{
if(month == 9 || month == 4 || month == 6 || month == 11)
{
if(day == 30)
{
month++;
day = 1;
}
else
day++;
}
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12)
{
if(day == 31)
{
month++;
day = 1;
if(month == 12)
{
month = 1;
day = 1;
year++;
}
}
else
day++;
}
if(month == 2)
{
if(day == 28)
{
month++;
day = 1;
}
else
day++;
}
}
/**
* Takes the year, month and day and displays it as m/d/yyyy
* Before it does it though, it changes all the ints to strings
*/
private String toString(int yearString, int monthString, int dayString)
{
stringDate = (Integer.toString(monthString) + "/" + Integer.toString(dayString)
+ "/" + Integer.toString(yearString));
return stringDate;
}
}
我想知道的是所有的| |陈述实际上都是正确的,或者是否有一种方法需要更少的工作。我对java也是相当陌生的,我想知道在Eclipse环境中用什么方法来编写文本是最好的。为什么不使用日历
Calendar time=Calendar.getInstance()代码>
time.add(Calendar.DAY,+1)
还有其他类似的平等性检查方法<代码>数组.asList(1,3,5,7,8,10,12).contains(月)代码>。你为什么要重新发明轮子?利用乔达的时间。。。(不,您当前的代码不正确。您的month
检查应该是13,因为它是在增量之后。)@JonSkeet您认为Joda时间是绝对必要的吗?当然OP可以通过使用Calendar
和操作字段来管理好它。@JoshM:我们不知道OP需要做什么,但是Calendar
API是一个可怕的烂摊子。乔达的时间干净多了。如果涉及到任何非琐碎的日期/时间工作,我会尝试一下Joda时间。从我所学的类中对Java的了解来看,Calander API或Joda时间似乎是解决这个问题的最佳方式,但没有人提到。