Tuesday, June 9, 2026
HomeLanguagesJavaLogRecord setInstant() method in Java with Examples

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:




// 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)

RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6895 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7018 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS