[英]Java getter and setter for datetime returns null
I have a main class which calls CurrentDateTime class to set the date and time: 我有一个主类调用CurrentDateTime类来设置日期和时间:
Main class: 主要课程:
public static void main(String args[]) {
CurrentDateTime currentDateTime = new CurrentDateTime();
currentDateTime.processDateTime();
LogTracer.start();
}
The CurrentDateTime class has the code below: CurrentDateTime类的代码如下:
CurrentDateTime class 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);
}
}
Another class which is my logger will try and get the date and time: 我记录器的另一个类将尝试获取日期和时间:
LogTracer class 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");
}
From what I understand the Main class will run the processDateTime() from the CurrentDateTime class to setup the date and time. 据我所知,Main类将从CurrentDateTime类运行processDateTime()来设置日期和时间。 Then the LogTracer class will just call the getter.
然后LogTracer类将只调用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“currentDateTime.getTime()”总是显示null而不是获取正确的日期和时间。似乎无法弄清楚代码有什么问题?
Sysout: 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. 你得到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");
}
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: 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");
}
You have forgot to call the method processDateTime()
, add the constructor to CurrentDateTime.java
您忘记调用方法
processDateTime()
,将构造函数添加到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. 在LogTracer.start()方法中,您可以创建CurrentDateTime类的空实例。 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.
尝试使用一些CurrentDateTime或getter的setter,或者创建另一个以日期/时间信息为参数的构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.