LogRecord setInstant() method in Java with Examples
Last Updated :
23 Oct, 2019
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:
import java.time.Instant;
import java.util.logging.Level;
import java.util.logging.LogRecord;
public class GFG {
public static void main(String[] args)
{
Instant instant
= Instant.parse( "2018-12-30T19:34:50.63Z" );
LogRecord logRecord = new LogRecord(
Level.parse( "800" ),
"Hi Logger" );
logRecord.setInstant(instant);
System.out.println(
"Event Time "
+ logRecord.getInstant()
.toString());
}
}
|
Output:
Event Time 2018-12-30T19:34:50.630Z
Program 2:
import java.time.Instant;
import java.util.logging.Level;
import java.util.logging.LogRecord;
public class GFG {
public static void main(String[] args)
{
Instant instant
= Instant.now();
LogRecord logRecord = new LogRecord(
Level.parse( "800" ),
"GFG Logger" );
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...