import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class TimestampTracker {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
public static void main(String[] args) {
// Start tracking
LocalDateTime startTime = LocalDateTime.now();
long startEpoch = Instant.now().toEpochMilli();
try {
// Simulate some work
simulateWork();
// End tracking
LocalDateTime endTime = LocalDateTime.now();
long endEpoch = Instant.now().toEpochMilli();
// Calculate duration
long durationMillis = endEpoch - startEpoch;
// Print results
System.out.println("Start Time: " + startTime.format(formatter));
System.out.println("End Time: " + endTime.format(formatter));
System.out.println("Execution Duration: " + durationMillis + " ms");
} catch (Exception e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
private static void simulateWork() throws Exception {
// Replace this with your actual script logic
Thread.sleep(1000); // Simulate 1 second of work
System.out.println("Performing some operation...");
Thread.sleep(500); // Simulate another 0.5 seconds of work
}
}
Add your comment