Friday, March 10, 2023
HomeSoftware EngineeringThe best way to Get Right now’s Date in Java

The best way to Get Right now’s Date in Java


If that you must get at this time’s date in Java, then you are able to do one of many following:

Choice 1 – Utilizing LocalDate

import java.time.LocalDate;

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

Choice 2 – Utilizing Calendar and SimpleDateFormat

import java.textual content.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class GetTodayDate {
    public static void essential(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);
    }
}

Choice 3 – Utilizing java.sql.Date

import java.sql.Date;

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments