Skip to content

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); } }
Code language: Java (java)

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); } }
Code language: Java (java)

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); } }
Code language: Java (java)

See also  PHP nl2br on one line
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x