Convertion of user-defined object to string by toString not showing correct output
-
13-12-2019 - |
Question
I have created a class CurrentDate
which shows the system date in a particular format(e.g. 29-JUN-12). The class looks like :
package getset;
import java.util.*;
import getset.Getset;
public class CurrentDate{
public static void main(String[] args){
Calendar cal = new GregorianCalendar();
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
int day = cal.get(Calendar.DAY_OF_MONTH);
String toyear=String.valueOf(year);
String newyear=toyear.substring(2,4);
String newmonth=monthvalidation(month);
System.out.println("Current date : " + day + "-" + (newmonth) + "-" + newyear);
}
private static String monthvalidation(int initmonth) {
// TODO Auto-generated method stub
//int initmonth=i;
String finalmonth="";
if(initmonth==1)
{
finalmonth="JAN";
}
if(initmonth==2)
{
finalmonth="FEB";
}
if(initmonth==3)
{
finalmonth="MAR";
}
if(initmonth==4)
{
finalmonth="APR";
}
if(initmonth==5)
{
finalmonth="MAY";
}
if(initmonth==6)
{
finalmonth="JUN";
}
if(initmonth==7)
{
finalmonth="JUL";
}
if(initmonth==8)
{
finalmonth="AUG";
}
if(initmonth==9)
{
finalmonth="SEP";
}
if(initmonth==10)
{
finalmonth="OCT";
}
if(initmonth==11)
{
finalmonth="NOV";
}
if(initmonth==12)
{
finalmonth="DEC";
}
return finalmonth;
}
}
Now when I try to convert it using toString()
it does not show what is expected. I tried:
CurrentDate date=new CurrentDate();
String sysdate=date.toString();
System.out.println(""+sysdate);
It shows something like: getset.CurrentDate@18a178a
which is not human readable expected format. What can I do to correct this?
Solution
You need to override the toString
method.
In your CurrentDate
class, add somethnig like
@Override
public String toString() {
String toyear=String.valueOf(year);
String newyear=toyear.substring(2,4);
String newmonth=monthvalidation(month);
return day + "-" + newmonth + "-" + newyear;
}
OTHER TIPS
You are not overriding the Object
implementation of toString()
To test your method, you could quickly put the logic in that you have developed into a public String toString()
method. then in your main
method, create a CurrentDate
and print the toString
.
Override toString
method in your class . Add a method like a as follows.
@Override
public String toString() {
return day + "-" + newmonth + "-" + newyear;
}
I see problem in your code.
First, you are not having fields for which you want toString()
representation.I think you need to declare fields like:
int month;
int year ;
int day;
@Override
public String toString() {
return "CurrentDate [month=" + month + ", year=" + year + ", day="
+ day + "]";
}
If you are using Eclipse IDE, just generate toString()
method, that will be a simple solution to your problem. Right now, object toString()
is invoked for your toString()
. In fact, as per Josh Bloch,
every class should override toString() for getting human readable format.
CurrentDate date=new CurrentDate();
String sysdate=date.toString();
System.out.println(""+sysdate);
In your above code you are using date object but not assigning any value , only CurrentDate class reference. so your are getting 'getset.CurrentDate@18a178a' when use toString() method.
If you want to format date to string you have to use
public static final String DATE_FORMAT_NOW = "dd-MMM-yy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);