📌  相关文章
📜  Java中的 LogRecord getInstant() 方法及示例

📅  最后修改于: 2022-05-13 01:54:47.650000             🧑  作者: Mango

Java中的 LogRecord getInstant() 方法及示例

Java.lang.reflect.LogRecordgetInstant()方法用于获取事件发生的这一时刻,这有助于即时记录日志事件。

句法:

public Instant getInstant()

参数:此方法不接受任何内容。

Return :此方法返回事件发生的时刻

下面的程序说明了 getInstant() 方法:
方案一:

// Java program to illustrate getInstant() method
  
import java.time.Instant;
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");
        logRecord
            .setInstant(
                Instant.parse(
                    "1994-04-12T11:54:23.89Z"));
  
        // get the instant time
        Instant instant = logRecord.getInstant();
  
        System.out.println("Event Time = "
                           + instant.toString());
    }
}
输出:
Event Time = 1994-04-12T11:54:23.890Z

方案二:

// Java program to illustrate getInstant() method
  
import java.time.Instant;
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");
        logRecord.setInstant(Instant.now());
  
        // get the instant time
        Instant instant = logRecord.getInstant();
  
        System.out.println("Event Time = "
                           + instant.toString());
    }
}
输出:
Event Time = 2019-10-20T19:41:57.803594Z

参考资料: https: Java/util/logging/LogRecord.html#getInstant()