我有一个主类,它调用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

最佳答案

您将得到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");
}

10-08 06:40