开发者

toString method in Java

开发者 https://www.devze.com 2023-02-28 03:21 出处:网络
Why the time is shown not in normal (readable) way? Method toString is missing? import java.util.Calendar;

Why the time is shown not in normal (readable) way? Method toString is missing?

import java.util.Calendar;

abstract class Ca开发者_JAVA百科lender {

    abstract void showTime();

    void time() {
        Calendar rightNow = Calendar.getInstance();
        System.out.println("Time: " + rightNow);
    }

    public String toString() {
        return "Calender [toString()=" + super.toString() + "]";
    }

}

class Calender1 extends Calender {
    public static void main (String[] args) {
        Calender1 cal = new Calender1();
        cal.time();
    }

    void showTime() {       
    }
}

Thank you. Java rookie.


You can display the current time with the following code:

DateFormat timeFormat = DateFormat.getTimeInstance();
Calendar currentDate = Calendar.getInstance();
System.out.println(timeFormat.format(currentDate.getTime()));


Not really sure what you're actually doing here.

It looks like you've written your own abstract class Calendar and inherited from it in Calendar1.

The first Calendar class you wrote inherits from object the toString of which will not be interesting / useful.

The docs for toString on a calendar are here: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html#toString()

Bottom line - don't rely on it.


Why are you using super in your base class? I'm not really sure what you are trying to do but in you base class Calendar you just need the toString() method.

And in your Calender1 class you need to create a toString() method as well. In there you would call use super.toString() this is because you are inheriting from calendar so its your super class


You are calling super with no parent class (you are calling Object.toString(), actually) This will return the name of the class and the memory address: http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#toString%28%29

You probably want something like this:

@Override
public String toString() {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
    return "Calender [toString()=" + formatter.format(rightNow.getTime()) + "]";
}


You probably tried this:

Calendar cal = Calendar.getInstance();
System.out.println(cal);

Which gives output like this:

java.util.GregorianCalendar[time=1303645990437,areFieldsSet=true,areAllFieldsSet=true,...

This is the toString method of GregorianCalendar, which gives all the nitty gritty state details of its implementation, not a readable date-time representation.

As an experiment, you could create an anonymous inner class extending GregorianCalendar with a different toString implementation:

Calendar cal = new GregorianCalendar() {        
    public String toString() {
        return getTime().toString();
    }
};
System.out.println(cal);

This prints:

Sun Apr 24 13:53:10 CEST 2011

(YMMV).

However, this is a bit kinky. Normally you would use a DateFormat as shown in several other answers, to decouple the presentation of your Calendar from its implementation.

0

精彩评论

暂无评论...
验证码 换一张
取 消