How would one go about finding the system's date and time of access during the startup of a program?

I want to gather the date and time, and then return the information to the user along with the rest of the program's relevant functions. My goal would be to have something that I can put into my Formatter output to write to a file. Below is an extract from my code.

try
{
output = new Formatter(file);   //Create formatter object to format Summary.txt
    output.format("%s%n%s%n%s%n%s%d%n%s%.15f%n%s%.15f%n%s%.15f%n%s%.15f%n", "Michael Baird" 
        , "CSC-240 Java Programming", "Sunday, May 11th 12:00:01 MDT 2014"
        , "Count of values = ", count, "Sum of values = ", sum, "Average = "
        , average, "Maximum = ", maximum, "Minimum = ", minimum);

}

I attempted to find the answer to this question, but to no avail, so apologies if this is a duplicate.

Thank you!

有帮助吗?

解决方案

Date currentTime = Calendar.getInstance().getTime();

If you need that date format you will want to do this:

Date currentTime = Calendar.getInstance().getTime();
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMM d h:m:s a z yyyy");
System.out.println(sdf.format(currentTime));

Output will be similar to this:

Saturday, May 10 6:55:49 PM EDT 2014

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top