Java中的 LogRecord setMillis() 方法及示例
Java.util.logging.LogRecord的setMillis()方法用于设置 LogRecord 中的事件时间。该事件时间从 1970 年开始以 MilliSeconds 为单位。
句法:
public void setMillis(long millis)
参数:此方法接受毫秒作为参数,它是自 1970 年以来以毫秒为单位的事件时间
Return :此方法不返回任何内容。
下面的程序说明了 setMillis() 方法:
方案一:
Java
// Java program to illustrate setMillis() method
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.LogRecord;
public class GFG {
public static void main(String[] args)
{
// Create LogRecord object
LogRecord logRecord = new LogRecord(
Level.parse("800"),
"Hi Logger");
// set event time
logRecord.setMillis(999999999900L);
// get event time and convert it into date
DateFormat simple
= new SimpleDateFormat(
"dd MMM yyyy HH:mm:ss:SSS Z");
Date result
= new Date(logRecord.getMillis());
System.out.println("Event Time "
+ simple.format(result));
}
}
Java
// Java program to illustrate setMillis() method
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.LogRecord;
public class GFG {
public static void main(String[] args)
{
// Create LogRecord object
LogRecord logRecord = new LogRecord(
Level.parse("800"),
"GFG Logger");
// set event time
logRecord.setMillis(9632736138L);
// get event time and convert it into date
DateFormat simple
= new SimpleDateFormat(
"dd MMM yyyy");
Date result
= new Date(logRecord.getMillis());
System.out.println("Event Date: "
+ simple.format(result));
}
}
输出:
Event Time 09 Sep 2001 01:46:39:900 +0000
方案二:
Java
// Java program to illustrate setMillis() method
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.LogRecord;
public class GFG {
public static void main(String[] args)
{
// Create LogRecord object
LogRecord logRecord = new LogRecord(
Level.parse("800"),
"GFG Logger");
// set event time
logRecord.setMillis(9632736138L);
// get event time and convert it into date
DateFormat simple
= new SimpleDateFormat(
"dd MMM yyyy");
Date result
= new Date(logRecord.getMillis());
System.out.println("Event Date: "
+ simple.format(result));
}
}
输出:
Event Date: 22 Apr 1970
参考资料: https: Java/util/logging/LogRecord.html#setMillis-long-