I was doing my assignment till I reached this point.
Add a static Date toDate(String str) method to the DateTest class. If the method is called with a string like “23/5/2013”, it will create and return the corresponding date object. If the input string is not valid date, the method should return null. Hint: use the String.split(“/”) method to split the string in three parts and retrieve the day, month, and year. Or use the String.indexOf(“/”) and String.substring() methods to extract the three components of the date from the string.
I am stuck in this step I don't know what should I do.
DateTest:
public class DateTest {
public static void main(String args[]){
Date DateObject1 = new Date();
System.out.println(DateObject1.toString());
Date DateObject2 = new Date(0,"May",0);
System.out.println(DateObject2.toString());
Date DateObject3 = new Date(2013,"April",23);
System.out.println(DateObject3.toString());
}
}
Date:
public class Date {
private int year;
private int month;
private int day;
private String monthfullname;
public Date(int y, int m, int d){
year = ((y > 0)? y : 1);
month = ((m >= 1 && m <= 12)? m : 1);
day = (( d >= 1 && d <= 31)? d : 1);
}
public Date(){
this(1,1,1);
}
public Date(int y, String m, int d){
year = ((y > 0)? y : 1);
monthfullname = ((m != "April")? "1" : m);
day = (( d >= 1 && d <= 31)? d : 1);
}
public String toString(){
return String.format("%s %d,%d", monthfullname,day,year);
}