How to Get Today’s Date in Java

0 min read 113 words

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);
    }
}
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags