SimpleDateFormat() 中的 24 小时和 java 中的毫秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22230826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
24 hour in SimpleDateFormat() and milliseconds in java
提问by Thanos
I am building a DAQ software and right now I am in the process of creating a log file. I thought of showing the time on each entry. A simple code(that cannot compile) is the following
我正在构建一个 DAQ 软件,现在我正在创建一个日志文件。我想在每个条目上显示时间。一个简单的代码(无法编译)如下
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");
}
}
A typical result of the above "code" is the following
上述“代码”的典型结果如下
[06:30:09] Acquisition Started
[06:30:09] ADC Channel 1: Read Complete
[06:30:09] Initialisation Complete
[06:30:13] Acquisition Stopped
I was wondering if there is a way to display the time in 24 hour format(i.e. 18:30:09
instead of 06:30:09
)?
我想知道是否有办法以 24 小时格式(即18:30:09
代替06:30:09
)显示时间?
Is there also a way to display milliseconds? A format like hh:mm:ss:mils
? I tried to use sss
instead of ss
but I only get a 0
for the first s
. An example output is
有没有办法显示毫秒?像这样的格式hh:mm:ss:mils
?我尝试使用sss
而不是,ss
但我只得到了0
第一个s
. 一个示例输出是
[21:50:004] Acquisition Started
[21:50:004] ADC Channel 1: Read Complete
[21:50:004] Initialisation Complete
[21:50:013] Acquisition Stopped
采纳答案by JDGuide
Try this :-
尝试这个 :-
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSS");
Also read this.Hope it will help you.
也读这个。希望它会帮助你。
回答by Rohit Jain
You should use HH
instead of hh
:
您应该使用HH
代替hh
:
SimpleDateFormat now = new SimpleDateFormat("HH:mm:ss");
回答by Basil Bourque
Time Zone
时区
The question and answers ignore the issue of time zones. Usually it is best to do logging in UTC (no time zone offset) to avoid ambiguity and to avoid confusion over Daylight Saving Time.
问题和答案忽略了时区问题。通常最好使用 UTC(无时区偏移)进行登录,以避免歧义并避免与夏令时混淆。
ISO 8601
ISO 8601
The sensible ISO 8601 standard defines a format for textual representation of date-time values. This format is especially useful in logging.
合理的 ISO 8601 标准定义了日期时间值的文本表示格式。这种格式在日志记录中特别有用。
Joda-Time
乔达时间
The Joda-Time library supports time zones, has a built-in constant for UTC time zone, and uses the ISO 8601 format and it's 24-hour clock by default.
Joda-Time 库支持时区,具有 UTC 时区的内置常量,并使用 ISO 8601 格式,默认为 24 小时制。
String output = DateTime.now( DateTimeZone.UTC ).toString();