繁体   English   中英

如何使我的“ Date”对象在每次调用时都打印出不同的值?

[英]How do I get my “Date” object to print out different values each time I call it?

我被要求在整个现有项目中添加println语句,以便每个进程的日期/时间在执行时都可以打印出来。

我首先要提到的是,由于项目限制,我必须坚持使用Java 7 我知道Java 8有一个不错的LocalDateTime.now()函数,使确定当前日期和时间变得容易得多,可惜我们无法使用它。

无论如何。 在将其构建到现有代码中之前,我已经编写了一个微型项目来测试此功能。

我有一个CreateDate类,它允许我创建一个新的Date对象并按如下所示打印当前日期/时间:

package datetest;

import java.util.Date;

public class CreateDate {

    public void returnDate() {
        Date date = new Date();
        System.out.println("The current date and time is: " + date);
    }

}

我还有另一个类Printer,它创建CreateDate的实例并按如下所示调用returnDate方法:

package datetest;

    public class Printer {

        public static void main(String[] args) {
            CreateDate date = new CreateDate();
            date.returnDate();
        }

    }

但是,无论我尝试打印多少次当前日期/时间,日期对象每次都使用相同的日期和时间初始化。 我希望打印出不同的日期,以指定执行某件事的当前时间。

请帮忙! 我是Java的新手,因此非常感谢您提供的任何建议。

您必须在某个字段中“保存”对象-每次调用returnDate方法时都在创建新的Date对象,因此:

public class CreateDate {

    protected Date date;

    public CreateDate() {
        this.date = new Date();
    }

    public void returnDate() {
        System.out.println("The current date and time is: " + this.date);
    }

}

我认为您已经使这一问题复杂化了。 以下程序每次运行时都会打印不同的日期:

import java.util.Date;

public class Main {

    public static void main(String[] args) {
        System.out.println("The current time is "+new Date());
    }
}

如果您希望将其排除在外,则:

import java.util.Date;

public class Printer {

    static public  void printCurrentTime() {
        System.out.println("The current time is " + new Date());
    }
}

附:

public class Main {

  public static void main(String[] args) throws Exception {
    for( int i = 0; i< 10; i++) {
        Printer.printCurrentTime();
        Thread.sleep(10000);
    }
  }
}

该程序产生

The current time is Wed Oct 14 11:10:40 BST 2015
The current time is Wed Oct 14 11:10:50 BST 2015
The current time is Wed Oct 14 11:11:00 BST 2015
The current time is Wed Oct 14 11:11:10 BST 2015
The current time is Wed Oct 14 11:11:20 BST 2015
The current time is Wed Oct 14 11:11:30 BST 2015
The current time is Wed Oct 14 11:11:40 BST 2015
The current time is Wed Oct 14 11:11:50 BST 2015
The current time is Wed Oct 14 11:12:00 BST 2015
The current time is Wed Oct 14 11:12:10 BST 2015
import java.util.Date;

public class MyDate {

    private Date date;

    @Override
    public String toString() {
        date = new Date();
        return date.toString();
    }

    public Date getDate() {
        return date;
    }

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

    public static void main(String[] args) throws InterruptedException {
        MyDate currentDate = new MyDate();
        for(int i = 0; i < 100; i++) {
            Thread.sleep(100);
            System.out.println(currentDate);
        }
    }
}

产生

Wed Oct 14 16:14:19 IST 2015
Wed Oct 14 16:14:20 IST 2015
Wed Oct 14 16:14:20 IST 2015
Wed Oct 14 16:14:20 IST 2015
Wed Oct 14 16:14:20 IST 2015
Wed Oct 14 16:14:20 IST 2015
Wed Oct 14 16:14:20 IST 2015
Wed Oct 14 16:14:20 IST 2015
Wed Oct 14 16:14:20 IST 2015
Wed Oct 14 16:14:20 IST 2015
Wed Oct 14 16:14:20 IST 2015
Wed Oct 14 16:14:21 IST 2015
Wed Oct 14 16:14:21 IST 2015
Wed Oct 14 16:14:21 IST 2015
Wed Oct 14 16:14:21 IST 2015
Wed Oct 14 16:14:21 IST 2015
Wed Oct 14 16:14:21 IST 2015
Wed Oct 14 16:14:21 IST 2015
Wed Oct 14 16:14:21 IST 2015
Wed Oct 14 16:14:21 IST 2015

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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