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.
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); } }
Date Value: 2014-09-11
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.
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); } }
Day: 18 Month: 7 Year: 2020
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 −
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); } }
Date value: Mon Jun 25 00:00:00 IST 2007