- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get current day, month and year in Java 8?
The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay,Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.
Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current date from the system clock.
This class also provides various other useful methods among them −
- The getYear() method returns an integer representing the year filed in the current LocalDate object.
- The getMonth() method returns an object of the java.timeMonth class representing the month in the LocalDate object.
- The getDaYofMonth() method returns an integer representing the day in the LocalDate object.
Example
Following Java example retrieves the current date and prints the day, year and, month separately using the above specified methods.
import java.time.LocalDate; import java.time.Month; public class LocalDateJava8 { public static void main(String args[]) { //Getting the current date value LocalDate currentdate = LocalDate.now(); System.out.println("Current date: "+currentdate); //Getting the current day int currentDay = currentdate.getDayOfMonth(); System.out.println("Current day: "+currentDay); //Getting the current month Month currentMonth = currentdate.getMonth(); System.out.println("Current month: "+currentMonth); //getting the current year int currentYear = currentdate.getYear(); System.out.println("Current month: "+currentYear); } }
Output
Current date: 2019-07-24 Current day: 24 Current month: JULY Current month: 2019
- Related Articles
- How to get a Date from year, month and day in Java?
- Java Program to get current date, year and month
- How to check Leap Year in Java 8 How to get current timestamp in Java 8?
- Convert day of year to day of month in Java
- Filter the records of current day, month and year in MySQL?
- How to get last day of the current month in MySQL?
- How to get the first day of the current month in MySQL?
- How to get day of month, day of year and day of week in android using offset date time API class?
- Format MySQL date and convert to year-month-day
- How to convert year, month, and day of the month into a complete date in R?
- Get week of month and year using Java Calendar
- How to combine year, month, and day column in an R data frame?
- How to get current Time in Java 8?
- Get the last day of month in Java
- How to Convert Numbers to Year/Month/Day or Date in Excel?

Advertisements