Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

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();
      }      
}
0
rukiman
Asked:
rukiman
  • 6
  • 6
  • 5
  • +4
2 Solutions
 
girionisCommented:
Because your testStrt() is void. Change it to public String testStr()
0
 
girionisCommented:
> super.toString();

Thsi will *return* a Strign so your method should have the appropriate type.
0
 
zzynxSoftware engineerCommented:
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());
     }
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
CEHJCommented:
The code you posted wouldn't compile. You should simply override toString:

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

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

class MyDate extends Date
{    
     public String testStr()
     {
          return super.toString();
     }    
}
0
 
zzynxSoftware engineerCommented:
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()
0
 
CEHJCommented:
>>Hi CEHJ, back from the pub ;°)

LOL - that would have been quite a binge
0
 
JakobACommented:
confused.  why would that return: 'Mydata@xxxxxxx'   syntax error seems more fitting.
0
 
rukimanAuthor Commented:
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??
0
 
CEHJCommented:
I can only imagine that Object.toString has not been overridden for Date. In order to fix that and to provide what you need in terms of getHour etc, use a Calendar in MyDate
0
 
B000GTCommented:
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
0
 
zzynxSoftware engineerCommented:
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() );
     }
}
0
 
CEHJCommented:
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
0
 
zzynxSoftware engineerCommented:
I think my comment CEHJ is referring to deserves to be in a split.
0
 
girionisCommented:
I believe I was the first one who provided a valid answer.
0
 
VenabiliCommented:
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... )
0
 
zzynxSoftware engineerCommented:
>> 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.
0
 
VenabiliCommented:
Which comment? Just show it to me please :) Because neither of your porevious comments speaks for such thing...

Venabili
0
 
zzynxSoftware engineerCommented:
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.
0
 
VenabiliCommented:
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
0
 
girionisCommented:
I thought there was going to be a split...
0
 
VenabiliCommented:
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"
0
 
girionisCommented:
Thank you :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 6
  • 6
  • 5
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now