使用格里高利历作为时间戳

时间:2016-08-12 00:20:53

标签: java constructor gregorian-calendar

我有一个带有电子邮件类的项目,如下所示

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;

我在想我甚至不需要构造函数中的timestamp参数。我想只要为电子邮件对象调用该承包商,就将时间戳设置为当前时间。有人可以解释一下如何做到这一点吗?

1 个答案:

答案 0 :(得分:2)

您可以使用以下方式获取当前时间

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();
相关问题