Angela Castello Angela Castello - 3 months ago 21
Java Question

using gregorian calendar as a time stamp

I have a project with an email class, as shown below

import java.util.Calendar;
import java.util.GregorianCalendar;

public class Email /*implements serializbale*/{

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

GregorianCalendar cal = new GregorianCalendar();


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


I was thinking that I wouldn't even need a timestamp parameter in the constructor. I want to just set timestamp to the current time whenever that contractor is called for an email object. Can someone explain how to do this?

Answer

You use as below to get current time

LocalDateTime currentTime;
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;
this.currentTime = LocalDateTime.now();