java - 使用公历作为时间戳

标签 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;

我想我什至不需要在构造函数中使用时间戳参数。每当承包商被要求提供电子邮件对象时,我只想将时间戳设置为当前时间。有人可以解释一下如何做到这一点吗?

最佳答案

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

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();

关于java - 使用公历作为时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38908025/

相关文章:

java - 如何像谷歌翻译器一样添加自动 texview 滚动

java - 是否有维护插入顺序的 IdentityHashMap 实现?

javascript - 构造函数中Object.defineProperty的获取中的"undefined"

java - 如何将 java.util.sql 存储到 XMLGregorianCalendar

Java:GregorianCalendar 返回错误的数据

java - 困惑 : Abstraction and encapsulation are the same?

java - 使用参数创建不要将新行插入数据库表

java - 子类中扩展子类的 super() 构造函数?

iphone - +(void) 在 objective-c 类静态变量构造函数中初始化

java - 日历的 GregorianCalendar 方法将日期设置为前一天