Print current time in milliseconds - Java:
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
public class PrintCurrentTime {
public static void main(String[] args) {
//Get current time in milliseconds
long currentTimeInMilliseconds = System.currentTimeMillis();
//Print current time in milliseconds
System.out.println("Current time in milliseconds: " + currentTimeInMilliseconds);
//Get current time in seconds
long currentTimeInSeconds = System.currentTimeMillis() / 1000;
//Print current time in seconds
System.out.println("Current time in seconds: " + currentTimeInSeconds);
//Get current time in minutes
long currentTimeInMinutes = System.currentTimeMillis() / (60 * 1000);
//Print current time in minutes
System.out.println("Current time in minutes: " + currentTimeInMinutes);
//Get current time in hours
long currentTimeInHours = System.currentTimeMillis() / (60 * 60 * 1000);
//Print current time in hours
System.out.println("Current time in hours: " + currentTimeInHours);
//Get current time in days
long currentTimeInDays = System.currentTimeMillis() / (24 * 60 * 60 * 1000);
//Print current time in days
System.out.println("Current time in days: " + currentTimeInDays);
//Get current time in months
long currentTimeInMonths = System.currentTimeMillis() / (30 * 24 * 60 * 60 * 1000);
//Print current time in months
System.out.println("Current time in months: " + currentTimeInMonths);
//Get current time in years
long currentTimeInYears = System.currentTimeMillis() / (365 * 24 * 60 * 60 * 1000);
//Print current time in years
System.out.println("Current time in years: " + currentTimeInYears);
//Get current date and time using Date class
Date date = new Date();
//Print date and time using Date class
System.out.println("Current date and time using Date class: " + date);
//Get current date and time using LocalDateTime class
LocalDateTime localDateTime = LocalDateTime.now();
//Print date and time using LocalDateTime class
System.out.println("Current date and time using LocalDateTime class: " + localDateTime);
//Get current date and time using Instant class
Instant instant = Instant.now();
//Print date and time using Instant class
System.out.println("Current date and time using Instant class: " + instant);
//Get current date and time using DateTimeFormatter class
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDateTime = formatter.format(localDateTime);
//Print date and time using DateTimeFormatter class
System.out.println("Current date and time using DateTimeFormatter class: " + formattedDateTime);
}
}
Top comments (0)