Open In App

LogRecord setInstant() method in Java with Examples

The setInstant() method of java.lang.reflect.LogRecord is used to set this instant that the event occurred this is helpful to record logging events instant. An arithmeticException will be thrown if the given instant represents a point on the timeline too far in the future or past to fit in long milliseconds and nanoseconds adjustment.

Syntax:



public void setInstant(Instant instant)

Parameters: This method accepts instant which is the instant that the event occurred.

Return: This method returns nothing.



Exception: This method will throw following exceptions:

Below programs illustrate setInstant() method:
Program 1:




// Java program to illustrate
// setInstant() 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 a Instant object
        Instant instant
            = Instant.parse("2018-12-30T19:34:50.63Z");
  
        // Create LogRecord object
        LogRecord logRecord = new LogRecord(
            Level.parse("800"),
            "Hi Logger");
  
        // set Instant time
        logRecord.setInstant(instant);
        System.out.println(
            "Event Time "
            + logRecord.getInstant()
                  .toString());
    }
}

Output:
Event Time 2018-12-30T19:34:50.630Z

Program 2:




// Java program to illustrate
// setInstant() 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 a Instant object
        Instant instant
            = Instant.now();
  
        // Create LogRecord object
        LogRecord logRecord = new LogRecord(
            Level.parse("800"),
            "GFG Logger");
  
        // set Instant time
        logRecord.setInstant(instant);
  
        System.out.println0(
            "Event Time "
            + logRecord.getInstant()
                  .toString());
    }
}

Output:
Event Time 2019-10-20T19:32:50.818428ZEvent Time 09 Sep 2001 07:16:39:900 +0530

References: https://docs.oracle.com/javase/10/docs/api/java/util/logging/LogRecord.html#setInstant(java.time.Instant)


Article Tags :