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:

  • NullPointerException – if instant is null.
  • ArithmeticException – if numeric overflow would occur while calling instant.toEpochMilli().

Below programs illustrate setInstant() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


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)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.