1import java.time.format.DateTimeFormatter;
2import java.time.LocalDateTime;
3public class CurrentDateTimeExample1 {
4 public static void main(String[] args) {
5 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
6 LocalDateTime now = LocalDateTime.now();
7 System.out.println(dtf.format(now));
8 }
9}
1// get current date and timein java using Calendar.getTime() method
2import java.util.Calendar;
3import java.util.Date;
4public class UsingCalendarClass
5{
6 public static void main(String[] args)
7 {
8 Date date = Calendar.getInstance().getTime();
9 System.out.println(date);
10 }
11}
1from datetime import date
2
3today = date.today()
4print("Today's date:", today)
5
1// Get current date and time in java 8
2import java.time.LocalDate;
3import java.time.LocalDateTime;
4import java.time.LocalTime;
5public class CurrentDateTimeJava8
6{
7 public static void main(String[] args)
8 {
9 LocalDate ld = LocalDate.now();
10 System.out.println("Current date: " + ld);
11 // get current time value
12 LocalTime lt = LocalTime.now();
13 System.out.println("Current time: " + lt);
14 // get current date-time value
15 LocalDateTime ldt = LocalDateTime.now();
16 System.out.println("Current date-time: " + ldt);
17 }
18}
1 Date currentDate = new Date();
2 SimpleDateFormat dateFormat= new SimpleDateFormat("dd/MMM/yyyy");
3 String dateOnly = dateFormat.format(currentDate);
4 dateTextView.setText(dateOnly);
1SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
2Date date = new Date();
3System.out.println(formatter.format(date));
4
5// ------------------------
6
7System.out.println(LocalDate.now());