Reputation: 337
I have a Product class and a Main class, each Product has a date variable. I'm setting the Date of every Product in the Main such as :
product1.date=setDate("2015/05/18");
The setDate() method has to take a Date or String variable as argument in that format and parse it into a Date so it can be set for the Product. I was using SimpleDateFormat but it's confusing because I want to do 2 things :
I have 2 problems:
1- How can I make it diplay only the date such as : 2015/05/10 and not the entire time zone and day of the week?
2- I'm using a toString() method in the Product class to display everything related to that object (ID, Label, price and date) how can I make the date be displayed using toString()?
Upvotes: 0
Views: 504
Reputation: 70989
If you don't care about the hours, minutes, seconds, milliseconds, and timezone, I suggest that you implement setDate to set the corresponding Date
object to be the first hour, minute, second, millisecond, of that date in the GMT timezone. Trust me, it will save you issues later on (also construct a new copy, and discard the input Date, as doing otherwise permits people to modify it at a distance)
To print out the date, use a SimpleDateFormat. The format you want is "yyyy/MM/dd" The M's are upper case because lowecase m is for minutes.
Upvotes: 1
Reputation: 5048
You can make it this way:
public String toString(){
SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd);
String str = "";
str += "Id: " + this.id + "\n";
str += "Price: " + this.price + "\n";
.
.
str += f.format(date);
return str;
}
Upvotes: 1