2015-10-14 84 views
2

我一直要求各地現有項目中添加println語句,這樣的日期/每道工序的時間可以爲他們執行打印出來。如何在每次打電話時讓我的「Date」對象打印出不同的值?

我應該提到的第一件事是由於項目限制,我必須堅持使用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); 
    } 

} 

我有另一個類,打印機,創建CREATEDATE的一個實例,調用returnDate方法如下:

package datetest; 

    public class Printer { 

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

    } 

然而,不管我多少次打印出當前日期/時間,日期對象初始化具有相同的日期和時間,每一次。我正在尋找打印出不同的日期來指定當前正在執行的時間。

請幫忙!我對Java相對來說比較陌生,所以你可以提供的任何建議都會很感激。

回答

2

你要「拯救」的對象在某些領域 - 你正在創造新的Date對象每次調用returnDate方法的時間,所以:

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

} 
+0

抱歉,但你要告訴他,以節省內存或這在邏輯的改變呢? – dsharew

+0

所以如果我叫returnDate在我的應用程序的某些處理一起,它應該打印出與它的日期? – ct2602

+0

這將一遍又一遍地返回相同的日期 - 與OP想要的完全相反。 – Clashsoft

1

我覺得你在這複雜。

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 
1
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