
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to get a Date from year, month and day in Java?
Using the of() method
The of() method of the java.time.LocalDate class accepts the values of the year, month, and day of month as parameters, creates and returns an object of the LocalDate.
Example
import java.time.LocalDate; public class Test { public static void main(String[] args) { LocalDate date = LocalDate.of(2014, 9, 11); System.out.println("Date Value: "+date); } }
Output
Date Value: 2014-09-11
Using the GregorianCalendar class
One of the constructors of the java.util.GregorianCalendar class accepts the values of year, month and day of month as values and creates a Calendar object representing it.
Example
import java.util.*; class Test { public static void main(String args[]){ //Creating a calendar object Calendar cal = new GregorianCalendar(2020, 07, 18); int day = cal.get(Calendar.DAY_OF_MONTH); int month = cal.get(Calendar.MONTH); int year = cal.get(Calendar.YEAR); System.out.println("Day: " + day); System.out.println("Month: " + month); System.out.println("Year: " + year); } }
Output
Day: 18 Month: 7 Year: 2020
Using the SimpleDateFormat object
One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object. To parse/convert a string as a Date object −
- Instantiate this class by passing desired format string.
- Parse the date string using the parse() method.
Example
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Sample { public static void main(String args[]) throws ParseException { String date_string = "2007-25-06"; //Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MM"); //Parsing the given String to Date object Date date = formatter.parse(date_string); System.out.println("Date value: "+date); } }
Output
Date value: Mon Jun 25 00:00:00 IST 2007
- Related Articles
- How to get current day, month and year in Java 8?
- Java Program to get current date, year and month
- Finding day of week from date (day, month, year) in JavaScript
- Create date from day, month, year fields in MySQL?
- 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?
- How to get day of month, day of year and day of week in android using offset date time API class?
- How to Convert Numbers to Year/Month/Day or Date in Excel?
- Convert day of year to day of month in Java
- Java Program to Get Year from Date
- Combine three strings (day, month, year) and calculate next date PHP?
- How to get month from week number and year in Excel?
- MongoDB query to get specific month|year (not date)?
- How can we extract the Year and Month from a date in MySQL?
- How can we fetch month and day from a given date in MySQL?

Advertisements