how to access current time in java code example

Example 1: How to get current date and time in java

// get current date and timein java using Calendar.getTime() method
import java.util.Calendar;
import java.util.Date;
public class UsingCalendarClass
{
   public static void main(String[] args)
   {
      Date date = Calendar.getInstance().getTime();
      System.out.println(date);
   }
}

Example 2: java get current time in seconds

LocalDateTime date = LocalDateTime.now();
int seconds = date.toLocalTime().toSecondOfDay();

Example 3: How to get current date and time in java

// how to get current date and time in java 
// using Date class and System.currentTimeMillis() method
import java.util.Date;
public class UsingDateClass
{
   public static void main(String[] args) 
   {
      Date dt = new Date();
      System.out.println(dt);
      long milliSeconds = System.currentTimeMillis();  
      Date date = new Date(milliSeconds);
      System.out.println(date); 
   }
}

Tags:

Java Example