简体   繁体   中英

Java getter and setter for datetime returns null

I have a main class which calls CurrentDateTime class to set the date and time:

Main class:

public static void main(String args[]) {

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

    LogTracer.start();
}

The CurrentDateTime class has the code below:

CurrentDateTime class

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

}

Another class which is my logger will try and get the date and time:

LogTracer class

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");
}

From what I understand the Main class will run the processDateTime() from the CurrentDateTime class to setup the date and time. Then the LogTracer class will just call the getter. But the sysout "currentDateTime.getTime()' always shows null instead of getting the correct date and time. Can't seem to figure it out what is wrong with the code?

Sysout:

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

you are getting null because the object reference in your main is different with your Logracer class.

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");
}

new main and start function

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");
}

You have to pass the parameter

Main class:

public static void main(String args[]) {

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

    LogTracer.start(currentDateTime);
}

LogTracer class:

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");
}

You have forgot to call the method processDateTime() , add the constructor to CurrentDateTime.java

public CurrentDateTime() {
        processDateTime();
    }

output

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

Within LogTracer.start() method you create empty instance of CurrentDateTime class. That's because you use default constructor. Try to use some setter of CurrentDateTime or getter or create another constructor which takes date/time info as parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM