Introduction to Java: Date

Introduction to Java: Date


To get the current date is really simple in Java, you just instantiate the Date object from the java.util package.


Date currentDate = new Date();



import java.util.Date;

public class GetCurrentDate {
    public static void main(String[] args) {
        Date currentDate = new Date();
        System.out.println("Current Date: " + currentDate);
    }
}


Output:


$ javac GetCurrentDate.java
$ java GetCurrentDate

Current Date: Mon Sep 09 18:26:04 CEST 2019


We can easily format this date.


import java.util.Date;
import java.text.SimpleDateFormat;

public class FormatCurrentDate {
    public static void main(String[] args) {
        SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        Date currentDate = new Date();
        System.out.println("Current Date: " + formatter.format(currentDate));
    }
}


Output:


$ javac FormatCurrentDate.java
$ java FormatCurrentDate
Current Date: 2019-09-09 18:29:45


LocalDate
With LocalDate we only get the current date without time.


import java.time.LocalDate;

public class LocalDateExample {
    public static void main(String[] args) {
        LocalDate currentDate = LocalDate.now();

        System.out.println("Current Date : " + currentDate);
    }
}


Output:


$ javac LocalDateExample.java
$ java LocalDateExample
Current Date : 2019-09-09


LocalTime
LocalDate will only get current date, and LocalTime will only show current time without date.


import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime localTime = LocalTime.now();

        System.out.println("Current Time: " + localTime);
    }
}


Output:


$ javac LocalTimeExample.java
$ java LocalTimeExample
Current Time: 18:39:02.125


We can use DateTimeFormatter to format LocalDate and LocalTime if we want to.


import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class FormatLocalDateAndLocalTime {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");

        System.out.println("Formatted LocalDate : " + localDate.format(dateFormatter));
        System.out.println("Formatted LocalTime : " + localTime.format(timeFormatter));
    }
}


Output:



$ javac FormatLocalDateAndLocalTime.java
$ java FormatLocalDateAndLocalTime
Formatted LocalDate : 09-09-2019
Formatted LocalTime : 18:44:57


LocalDateTime
LocalDateTime is the combination of LocalDate and LocalTime, which will hold the value of both the date and time.


import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime curentDateTime = LocalDateTime.now();

        System.out.println("Current DateTime " + curentDateTime);
    }
}


Output:


$ javac LocalDateTimeExample.java
$ java LocalDateTimeExample
Current DateTime 2019-09-09T18:48:38.654


Use the DateTimeFormatter to format the date and time:


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class FormatLocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime currentDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");

        System.out.println("Current Date Time " + currentDateTime.format(formatter));
    }
}


Output:


$ javac FormatLocalDateTimeExample.java
$ java FormatLocalDateTimeExample
Current Date Time 09-09-2019 19:11:59


Share this: