Angela Castello Angela Castello - 3 months ago 18
Java Question

representing gregorian Calendar in a toString method

So i have two classes, one with a toString method and I need one of the fields it prints to be the date and time, but I'm having a hard time doing it. Here is my email class(basic fields, constructors, getters and setters:

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Comparator;
import java.util.Date;

public class Email {

private String to;
private String cc;
private String bcc;
private String subject;
private String body;
private GregorianCalendar timestamp; //time that email was created



public Email(String to, String cc, String bcc, String subject, String body) {
super();
this.to = to;
this.cc = cc;
this.bcc = bcc;
this.subject = subject;
this.body = body;
timestamp = (GregorianCalendar) new GregorianCalendar().getInstance();


}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getCc() {
return cc;
}
public void setCc(String cc) {
this.cc = cc;
}
public String getBcc() {
return bcc;
}
public void setBcc(String bcc) {
this.bcc = bcc;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}

public Date getTimestamp() {
return timestamp.getTime();
}
public void setTimestamp(GregorianCalendar timestamp) {
this.timestamp = timestamp;
}


And here is my Folder class which contains Email objects. I have taken only an except of this method to make it simpler to look at:

import java.sql.Timestamp;
import java.util.*;

public class Folder implements Comparable{

private ArrayList<Email> emails;
private String name;
private String currentSortingMethod;

//constructors, getters, setters and other methods omitted

public String toString(){
String a = "";
int j = 1;
for(int i = 0; i < emails.size(); i++){
String k = Integer.toString(j);
a += String.format("%-8s %-5s %-8s %-1s %10s", " "+ k +" ", "|" , emails.get(i).getTimestamp(), "|", emails.get(i).getSubject()+ " \n");
j = Integer.parseInt(k);
j++;
}

return(String.format("%-8s %-5s %-8s %-1s %10s", "Index", "|", "Time", "|", " Subject") + "\n" + "-------------------------------------\n" + a);
}


the toString method should print something like this

Index | Time | Subject
-------------------------------------
1 | Time | e
2 | Time | d
3 | Time | c
4 | Time | b
5 | Time | a


it works fine for the other fields, but I need the "Time" column to be filled with the time stamps of when that email object was created.
Example: 12:38PM 4/8/2012

Thank you so much for any help at all!

Answer

I ran this locally and it works.. see below. what are u getting in ur timestamp field ?

Index    |     Time     |    Subject
-------------------------------------
  1      |     Fri Aug 12 18:16:59 PDT 2016 | subject  
  2      |     Fri Aug 12 18:17:00 PDT 2016 | subject