get current DateTime code example

Example 1: python get date and time

import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))

Example 2: java how to get current date

import java.time.format.DateTimeFormatter;  
import java.time.LocalDateTime;    
public class CurrentDateTimeExample1 {    
  public static void main(String[] args) {    
   DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");  
   LocalDateTime now = LocalDateTime.now();  
   System.out.println(dtf.format(now));  
  }    
}

Example 3: how to make python tell the time

import time

t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print(current_time)

Example 4: use datetime python to get runtime

from datetime import datetime

start = datetime.now()
# code ...
print(datetime.now()-start)

Example 5: get current date datetime

from datetime import date

today = date.today()
print("Today's date:", today)