Java Program To Print Date And Time

01. Example:

public class fetchDateSQL {
  
  public static void main(String[] args) {
    long millis = System.currentTimeMillis();
    java.sql.Date date = new java.sql.Date(millis);
    System.out.println("Today Date: "+date);
  }
  
}

Output:

Table of Contents

Today Date: 2022-12-24


02. Example:

import java.util.*;

class getRealtimeDateAndTime {
  
  public static void main(String args[]) {
    
    int day, month, year;
    int second, minute, hour;
    
    GregorianCalendar date = new GregorianCalendar();
    day = date.get(Calendar.DAY_OF_MONTH);
    month = date.get(Calendar.MONTH);
    year = date.get(Calendar.YEAR);
    second = date.get(Calendar.SECOND);
    minute = date.get(Calendar.MINUTE);
    hour = date.get(Calendar.HOUR);
    
    System.out.println("Current date is: " + day + "/" + (month + 1) + "/" + year);
    System.out.println("Current time is: " + hour + ": " + minute + " : " + second);
  }
}

Output:

Current date is: 24/12/2022
Current time is: 6: 52 : 11

Leave a comment