Software Engineering

How to Get Today’s Date in Java


If you need to get today’s date in Java, then you can do one of the following:

Option 1 – Using LocalDate

import java.time.LocalDate;

public class GetTodayDate {
    public static void main(String[] args) {
        LocalDate todaysDate = LocalDate.now();
        System.out.println(todaysDate);
    }
}

Option 2 – Using Calendar and SimpleDateFormat

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

public class GetTodayDate {
    public static void main(String[] args) {
        SimpleDateFormat dtf = new SimpleDateFormat("yyyy/MM/dd");
        Calendar calendar = Calendar.getInstance();

        Date dateObj = calendar.getTime();
        String formattedDate = dtf.format(dateObj);
        System.out.println(formattedDate);
    }
}

Option 3 – Using java.sql.Date

import java.sql.Date;

public class GetTodayDate {
    public static void main(String[] args) {
        long miliseconds = System.currentTimeMillis();
        Date date = new Date(miliseconds);
        System.out.println(date);
    }
}