日期时间的Java getter和setter返回null

时间:2014-09-08 08:37:56

标签: java datetime

我有一个主类调用CurrentDateTime类来设置日期和时间:

主要课程:

public static void main(String args[]) {

    CurrentDateTime currentDateTime = new CurrentDateTime();
    currentDateTime.processDateTime();

    LogTracer.start();
}

CurrentDateTime类的代码如下:

CurrentDateTime类

public class CurrentDateTime {

    private String date;
    private String time;

    public String getDate() { return date; }

    public void setDate(String date) { this.date = date; }

    public String getTime() { return this.time; }

    public void setTime(String time) { this.time = time; }

    public void processDateTime() {
        date  = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
        this.setDate(date);
        time  = new SimpleDateFormat("HH-mm-ss").format(Calendar.getInstance().getTime());
        this.setTime(time);
        System.out.println("PROCESS " + date + " : " + time);
    }

}

我记录器的另一个类将尝试获取日期和时间:

LogTracer类

public static void start() {

    CurrentDateTime currentDateTime = new CurrentDateTime();
    System.out.println("currentDateTime.getTime() " + currentDateTime.getTime());

    String logFilename = "Error_" + currentDateTime.getTime() + ".log";

    String logDir = ("C:/test/" + currentDateTime.getDate()
            + File.separator + currentDateTime.getTime() + File.separator + "log");
}

根据我的理解,Main类将从CurrentDateTime类运行processDateTime()来设置日期和时间。然后LogTracer类将只调用getter。但sysout“currentDateTime.getTime()”总是显示null而不是获取正确的日期和时间。似乎无法弄清楚代码有什么问题?

SYSOUT:

PROCESS 2014-09-08 : 16-26-10
currentDateTime.getTime() null

4 个答案:

答案 0 :(得分:2)

您将获得null,因为main中的对象引用与您的Logracer类不同。

public static void main(String args[]) {

    //currentdatetime object #1
    CurrentDateTime currentDateTime = new CurrentDateTime();
    //gettime() and getdate() not null here
    currentDateTime.processDateTime();

    LogTracer.start();
}

public static void start() {

    //currentdatetime object #2 (new object)
    CurrentDateTime currentDateTime = new CurrentDateTime();
    //gettime and getdate null here must call currentDateTime.processDateTime();
    // or pass by reference
    System.out.println("currentDateTime.getTime() " + currentDateTime.getTime());

    String logFilename = "Error_" + currentDateTime.getTime() + ".log";

    String logDir = ("C:/test/" + currentDateTime.getDate()
            + File.separator + currentDateTime.getTime() + File.separator + "log");
}

新主要和启动功能

public static void main(String args[]) {

    //currentdatetime object #1
    CurrentDateTime currentDateTime = new CurrentDateTime();
    currentDateTime.processDateTime();

    // pass object #1 to LogTracer.start();
    LogTracer.start(currentDateTime);
}

public static void start(CurrentDateTime currentDateTime) {

    //access the object that is in the parameter. same object with main method.
    System.out.println("currentDateTime.getTime() " + currentDateTime.getTime());

    String logFilename = "Error_" + currentDateTime.getTime() + ".log";

    String logDir = ("C:/test/" + currentDateTime.getDate()
            + File.separator + currentDateTime.getTime() + File.separator + "log");
}

答案 1 :(得分:1)

您必须传递参数

主要课程:

public static void main(String args[]) {

    CurrentDateTime currentDateTime = new CurrentDateTime();
    currentDateTime.processDateTime();

    LogTracer.start(currentDateTime);
}

LogTracer类:

public static void start(CurrentDateTime currentDateTime) {

    System.out.println("currentDateTime.getTime() " + currentDateTime.getTime());

    String logFilename = "Error_" + currentDateTime.getTime() + ".log";

    String logDir = ("C:/test/" + currentDateTime.getDate()
            + File.separator + currentDateTime.getTime() + File.separator + "log");
}

答案 2 :(得分:0)

您忘记调用方法processDateTime(),将构造函数添加到CurrentDateTime.java

public CurrentDateTime() {
        processDateTime();
    }

<强>输出

PROCESS 2014-09-08 : 14-15-09
PROCESS 2014-09-08 : 14-15-09
PROCESS 2014-09-08 : 14-15-09
currentDateTime.getTime() 14-15-09

答案 3 :(得分:0)

在LogTracer.start()方法中,您可以创建CurrentDateTime类的空实例。那是因为你使用默认构造函数。尝试使用一些CurrentDateTime或getter的setter,或者创建另一个以日期/时间信息为参数的构造函数。