Link to home
Start Free TrialLog in
Avatar of rukiman
rukiman

asked on

why this doesn't work

I have provided the necessary code to explain what the problem is. How come testStr() does not return the date instead it returns Mydata@xxxxxxx.

I though super.toString() should call the toString for the Date class which will be the date.


class MyDate extends Date
{      
      void testStr()
      {
            return super.toString();
      }      
}
SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> super.toString();

Thsi will *return* a Strign so your method should have the appropriate type.
public class MyDate extends Date {    

     public String testStr() {
          return super.toString();
     }

     public static void main(String[] args) {
         MyDate d = new MyDate();
         System.out.println(d.testStr());
     }
}
The code you posted wouldn't compile. You should simply override toString:

public String toString() {
         return super.toString();
}

(You don't need to do anything though - it'll call super.toString anyway)
This will compile though:

class MyDate extends Date
{    
     public String testStr()
     {
          return super.toString();
     }    
}
Hi CEHJ, back from the pub ;°)
>>You don't need to do anything though - it'll call super.toString anyway
Right.

    System.out.println(d);

will print the date even if you don't have the function textStr()
>>Hi CEHJ, back from the pub ;°)

LOL - that would have been quite a binge
Avatar of JakobA
JakobA

confused.  why would that return: 'Mydata@xxxxxxx'   syntax error seems more fitting.
Avatar of rukiman

ASKER

Thanks for your help. Ok the code I posted was a cut out of my class. Hope this code makes more sense. The idea here is to modify MyDate class to later support getHour(), getMinute(), getDay(), getSeconds() public methods as the Date class in the CLDC Java library seems to be missing them.

class MyDate extends Date
{
      public String getDayAsString()
      {
            return dateToStr();            
      }      
      
      String dateToStr()
      {
            return super.toString();
      }
      
}

Ok this code doesn't do anything useful as it is. But getDayAsString() should return the date and time! Instead it returns MyDate@776028c1  whats wrong??
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i've stopped using Date because most of it is depricated but try using Calendar so

class MyDate extends Calendar
{
     public String getDate()
     {
           return this.getDate();
     }
}

that will use the default Date toString function...or should...and will get you the date of the Calendar class. To my knowledge Calendar is better to use anyway...correct me if i'm wrong though...

- nc
A Calendar object doesn't have a getDate() function.
You probably mean:

class MyDate extends Calendar {

     public String getDateAsString() {
           return this.getTime().toString();
     }
}

or if you want to control the format:

class MyDate extends Calendar {

     public String getDateAsString() {
           return new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format( this.getTime() );
     }
}
From what is being said by rukiman, it appears toString has not been overridden for Date in CLDC, although i'd prefer if he confirm this.

rukiman, please print

new Date()

to the device and tell us the result

If that is giving output similar to that you mentioned before, use a Calendar as i mentioned. You can use a DateFormat with it as zzynx suggests too
I think my comment CEHJ is referring to deserves to be in a split.
I believe I was the first one who provided a valid answer.
Uh.... OK... another reading of the quqestion and I agree with girionis -sorry). But we have 50 points.... so the best I can do is two way split - girionis/CEHJ (sorry zzynx... I do not see to which your comment does "I can only imagine that Object.toString has not been overridden for Date. " refer to... )
>> I do not see to which your comment does
>> "I can only imagine that Object.toString has not been overridden for Date. " refer to
It was a comment of CEHJ that was referring to one of mine.
If you agree that girionis' 1st comment deserves the points, then why doesn't my first comment?

Never mind. Do as you find just.
Which comment? Just show it to me please :) Because neither of your porevious comments speaks for such thing...

Venabili
I was talking about this comment of CEHJ:
>> If that is giving output similar to that you mentioned before, use a Calendar as i mentioned.
>> You can use a DateFormat with it as zzynx suggests too
But it doesn't matter no more.
zzynx,

I see now:)
Well... as I said  -- these are 50 points. girionis gave the first correct answer and CEHJ was the one that proposed that the trouble is not implemented toString (which I believe is the answer)

Venabili
I thought there was going to be a split...
Modulo,

Sorry for not posting the recommendation clearly... I thought it was clear but there were way too many comments after my own statement:

"Uh.... OK... another reading of the quqestion and I agree with girionis -sorry). "
"so the best I can do is two way split - girionis/CEHJ"
Thank you :)