24 hours in simpledateformat() and milliseconds in Java
•
Java
I'm building a DAQ software, and now I'm creating a log file I want to display the time on each item A simple code (unable to compile) is as follows
import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; public class daq(){ SimpleDateFormat Now = new SimpleDateFormat("hh:mm:ss"); public void go(){ report.setProperty("INSERT","["+Now.format(new Date())+"] Acquisition Started\n"); } public void init(){ report.setProperty("INSERT","["+Now.format(new Date())+"] ADC Channel 1: Read Complete\n"); report.setProperty("INSERT","["+Now.format(new Date())+"] Initialisation Complete\n"); } public void stop(){ report.setProperty("INSERT","["+Now.format(new Date())+"] Acquisition Stopped\n"); } }
The typical results of the above "code" are as follows
[06:30:09] Acquisition Started [06:30:09] ADC Channel 1: Read Complete [06:30:09] Initialisation Complete [06:30:13] Acquisition Stopped
I wonder if there is a way to display the time in a 24-hour format (i.e. 18:30:09 instead of 06:30:09)?
Is there another way to display milliseconds? Formats like HH: mm: SS: mils? I tried to use SSS instead of SS, but I only got 0.5% of the first s The sample output is
[21:50:004] Acquisition Started [21:50:004] ADC Channel 1: Read Complete [21:50:004] Initialisation Complete [21:50:013] Acquisition Stopped
Solution
Try this:-
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSS");
Also read this. I hope it will help you
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码