简体   繁体   中英

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

I have been asked to add println statements throughout an existing project so that the dates/times of each process can be printed out as they execute.

The first thing I should mention is that I have to stick to Java 7 due to project constraints. I am aware that Java 8 has a nice little LocalDateTime.now() function to make determining the current date and time a hell of a lot easier, shame we can't use it.

Anyway. I've written a mini project to test this functionality before I build it into the existing code.

I have a class CreateDate that allows me to create a new Date object and print out the current date/time as follows:

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

}

I have another class, Printer, that creates an instance of CreateDate and calls the returnDate method as follows:

package datetest;

    public class Printer {

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

    }

However, no matter how many times I try to print out the current date/time, the date object is initialised with the same date and time, every time. I'm looking to print out different dates to specify the current time that something is being executed.

Please help! I'm relatively new to Java, so any advice you could offer would be much appreciated.

You have to "save" the object in some field - you are creating new Date object every time you call returnDate method, so:

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

}

I think you're over complicating this. The following program prints a different date each time it is run:

import java.util.Date;

public class Main {

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

If you want it factored out, then:

import java.util.Date;

public class Printer {

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

With:

public class Main {

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

This program produces

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

produces

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

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