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);
    }

Recommended Answers

All 7 Replies

So im guessing this method is supposed to create a new Date() object through the method right?

Im guessing this is the method you want:

  public static Date toDate(String str){
        try{
            String[] a = str.split("/");
            int y = Integer.parseInt(a[2]);
            int d = Integer.parseInt(a[1]);
            int m = Integer.parseInt(a[0]);
            return new Date(y,m,d);
        } catch(Exception evt){
            return null;
        }

    }

Thanks a lot man, your code made me realize where I was stuck in. I was try to add the the static method inside the main, thanks again.

godzab : from where will your toDate() method get its string? from a constructor? if that is so , then this will not work. the static will run before the constructor , so it will not know from where to get the string the code mentions.

else , if you wish to do something like

    public static void main(String args[]) {
        String s = "2013/april/27"; // the input string
        Date.toDate(s); // no "new" here , just calling the static directly and calling the constructor from within it
        .
        .
        .

imo , the toString() method becomes redundant , as you no longer can do something like this

System.out.println("output : " + DateObject); // DateObject would be the instance created in main through the "new" keyword

im not saying it wont work , but you provided too little information on how that code should be implemented.

from where will your toDate() method get its string?

??? Nobody has any such method! There is a toDate(String str) method, and it's obvious where that gets its String.

??? Nobody has any such method! There is a toDate(String str) method, and it's obvious where that gets its String.

the static part was troubling me a bit , so i posted. it works im sure , but i felt a little more info would have been more helpful.

i felt a little more info would have been more helpful.

Yes, absolutely. We really try to discourage people from posting homework solution code without explanation or any attempt to teach.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.