tl;dr

 LocalDate.of( 2026 , 1 , 23 )  // Pass: ( year , month , day )

java.time

Some other Answers are correct in showing how to gather input from the user, but use the troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

LocalDate

For a date-only value without time-of-day and without time zone, use the LocalDate class.

LocalDate ld = LocalDate.of( 2026 , 1 , 23 );

Parse your input strings as integers as discussed here: How do I convert a String to an int in Java?

int y = Integer.parseInt( yearInput );
int m = Integer.parseInt( monthInput );  // 1-12 for January-December.
int d = Integer.parseInt( dayInput );

LocalDate ld = LocalDate.of( y , m , d );


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Answer from Basil Bourque on Stack Overflow
Top answer
1 of 9
10

tl;dr

 LocalDate.of( 2026 , 1 , 23 )  // Pass: ( year , month , day )

java.time

Some other Answers are correct in showing how to gather input from the user, but use the troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

LocalDate

For a date-only value without time-of-day and without time zone, use the LocalDate class.

LocalDate ld = LocalDate.of( 2026 , 1 , 23 );

Parse your input strings as integers as discussed here: How do I convert a String to an int in Java?

int y = Integer.parseInt( yearInput );
int m = Integer.parseInt( monthInput );  // 1-12 for January-December.
int d = Integer.parseInt( dayInput );

LocalDate ld = LocalDate.of( y , m , d );


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

2 of 9
9

Try the following code. I am parsing the entered String to make a Date

// To take the input
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the Date ");

String date = scanner.next();

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date2=null;
try {
    //Parsing the String
    date2 = dateFormat.parse(date);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println(date2);
🌐
Quora
quora.com › How-do-I-get-time-as-input-using-scanner-class-in-Java
How to get time as input using scanner class in Java - Quora
Answer (1 of 4): Tell the user to input a date in a format. System.out.println(“dd-mm-yyyy”); String date=new Scanner().nextLine(); SimpleDateFormat format=new SimpleDateFormat(“dd-mm-yyyy”); Date date=format.parse(date); Now your string ...
🌐
Coderanch
coderanch.com › t › 598292 › java › date-input-user-java
taking date input from user in java (Java in General forum at Coderanch)
3>finally ask him to enter the date(check if in the range between 1 - 30 or 1-31(based on the month)); 4> then concatenate in dd/mm/yyyy format( using "+" operator for string concatenation) and store into the String variable dob i was wondering if there was any better way to do it..like if a user inputs some char when scanner.nextInt() is waiting to read an integer, an exception is thrown..is there something similar where the user can be asked to input the date in some format like dd/mm/yyyy and if the input format is incorrect, throw an exception like inputMismatchException...
🌐
BeginnersBook
beginnersbook.com › 2013 › 05 › java-date
Java date
Here you can give your own format as input, it works for all formats of date. Java date and timezone – How to handle time-zones while doing date operations in a java program.
🌐
W3Schools
w3schools.com › java › java_date.asp
Java Date and Time
Java does not have a built-in Date class, but we can import the java.time package to work with the date and time API. The package includes many date and time classes.
🌐
Javatpoint
javatpoint.com › how-to-accept-date-in-java
How to Accept Date in Java - Javatpoint
How to Accept Date in Java with java tutorial, features, history, variables, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Tutorialspoint
tutorialspoint.com › java › java_date_time.htm
Java - Date and Time
It would be a bit silly if you had to supply the date multiple times to format each part. For that reason, a format string can indicate the index of the argument to be formatted. The index must immediately follow the % and it must be terminated by a $. import java.util.Date; public class DateDemo { public static void main(String args[]) { // Instantiate a Date object Date date = new Date(); // display time and date System.out.printf("%1$s %2$tB %2$td, %2$tY", "Due date:", date); } }
🌐
Sololearn
sololearn.com › en › Discuss › 1972022 › how-take-date-as-an-input-from-users-by-using-scanner-class-in-java
How take date as an input from users by using scanner class in Java? | Sololearn: Learn to code for FREE!
You just do the same thing as you would with the name 🤷‍♂️ Scanner in = new Scanner(System.in); System.out.print("Enter your birthday (dd/mm/yyyy) >>> "); String birthDate = in.nextLine();
Find elsewhere
🌐
Java-forums
java-forums.org › new-java › 48245-java-date-input-user.html
Java Date input from User,
Better yet, hard code a literal String for the user input and post the code. String ind = "PUT THE TEST DATE HERE"; //sc.nextLine(); ... I guess it depends upon what getDateInstance method does. Try creating your own SimpleDateFormat so you can specify what format you want the date to appear in.
🌐
Baeldung
baeldung.com › home › java › java dates › read date in java using scanner
Read Date in Java Using Scanner | Baeldung
January 8, 2024 - With an anterior version, we would need to close the Scanner resource manually. In this article, we parsed a Scanner input into a LocalDate. Then, we saw the equivalent code with Java’s early Date API. The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
🌐
Stack Overflow
stackoverflow.com › questions › 56969692 › how-to-get-date-as-input-from-user-using-scanner
java - How to get date as input from user using scanner? - Stack Overflow
From the date format, user need ... Scanner(System.in); String date = scanner.nextLine(); Date date1=new SimpleDateFormat("dd-MM-yyyy").parse(date); System.out.println(date1); } }...
🌐
IncludeHelp
includehelp.com › java-programs › convert-date-string-into-date-format.aspx
How to convert Date string to Date format in Java?
// Java program to get current // system date and time import java.text.SimpleDateFormat; import java.util.*; public class ConvDateString2DatePrg { public static void main(String args[]) { try { //define date format to take input SimpleDateFormat dateF = new SimpleDateFormat("dd/MM/yyyy"); Scanner ...
Top answer
1 of 2
1

Here's javadoc for next(), this is what it says:

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

Default delimiter is space and hence, it only reads 27. You need to read the whole line by using nextLine(). Following should work:

System.out.println("Enter check-in date (dd/mm/yy):");
String cindate = input.nextLine();
if(null != cindate && cindate.trim().length() > 0){
    Date date1 = myFormat.parse(cindate);
}

Update To check for null or empty string, you need to wrap the parsing code inside if condition.

2 of 2
1

Other answers addressed how to use Scanner correctly. I'll address some other issues.

java.time

You are using troublesome old date-time classes, now legacy, supplanted by the java.time classes. Avoid the java.util.Date class like the Plague.

Also, you are inappropriately trying to represent a date-only value with a date+time class.

Instead, use java.time.LocalDate. The LocalDate class represents a date-only value without time-of-day and without time zone.

To parse the user's input, specify a formatting pattern to match exactly the expected input from your user.

DateTimeFormatter f = DateTimeFormatter.parse( "dd/mm/uuuu" );

Parse user input.

LocalDate ld = LocalDate.parse( input , f );

On your business class Guest, make the member variable of type LocalDate.

To persist the LocalDate, you can serialize to text in standard ISO 8601 format. Simply call toString.

String output = guest.getLocalDate().toString();

2017-01-23


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

🌐
Scaler
scaler.com › home › topics › java convert string to date
Java Convert String to Date - Scaler Topics
July 14, 2024 - Java Program To Convert String to Date Using SimpleDateFormat Class ... Now create a SimpleDateFormat object named format. With the same pattern as the input string. For example, in the above Java code the input string is “Wed, Aug 30 2021”, ...
🌐
Reddit
reddit.com › r/programminghelp › help with using the scanner to get a date input from user
r/programminghelp on Reddit: Help with using the Scanner to get a date input from user
August 24, 2022 -

I've been working on using the Scanner to input information for a constructor. I have a majority of it down except for the Date object. I've tried using the DateTimeFormatter and the SimpleDateFormat but can't get it to work properly. I've had an unparsable date with the DateTimeFormatter and now I have a String cannot be converted to Date error. Any help for this part?

String hireDate = scanner.next();

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

Date date = formatter.parse(hireDate);

import java.text.DateFormat;

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Scanner;

public static void main(String[] args) throws ParseException {

Scanner scanner = new Scanner(System.in);

//input the following information System.out.println("Employee First Name"); String firstName = scanner.nextLine(); System.out.println("Employee Last Name"); String lastName = scanner.nextLine(); System.out.println("Employee Number"); int employeeNumber = scanner.nextInt(); System.out.println("Hire Date");

//problem area String hireDate = scanner.next(); DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date date = formatter.parse(hireDate);

Employee a = new Employee(firstName, lastName, employeeNumber, hireDate);

}}

Here is the other class I am using as well.

public class Employee {

public String firstName;
public String lastName;
public int employeeNumber;
public Date hireDate;

public Employee(String firstName, String lastName, int employeeNumber, Date hireDate){
this.firstName= firstName;
this.lastName = lastName;
this.employeeNumber= employeeNumber;
this.hireDate= hireDate;}


public String getEmployeeFirstName(){
return firstName;}

public void setEmployeeFirstName(String firstName){
this.firstName= firstName;}

public String getEmployeeLastName(){
return firstName;}

public void setEmployeeLastName(String lastName){
this.lastName= lastName;}

public int getEmployeeNumber(){
return employeeNumber;}

public void setEmployeeNumber(int employeeNumber){
this.employeeNumber= employeeNumber;}

public Date getHireDate(){
return hireDate;}

public void setHireDate(Date hireDate){
this.hireDate= hireDate;}


  public String toString() {
String str = "Employee Name: " + firstName + " " +lastName +
             "\nEmployee Number: " + employeeNumber +
             "\nEmployee Hire Date: " + hireDate;
return str;
        }

}

🌐
Systech
systechgroup.in › home › date java program
Java Program to Handle Date Create Format and Display
October 11, 2025 - The provided Java program is a simple date validation tool. It prompts the user to input a date in the “dd/mm/yyyy” format, validates the entered date, and then provides feedback on whether the date is valid or not.
Top answer
1 of 6
1

I suggest u to use a simple Bean ZodiacSign like that:

class ZodiacSign {
private String  name;
private int     startMonth;
private int     startDay;
private int     endMonth;
private int     ednDay;

public ZodiacSign(String name, int startMonth, int startDay, int endMonth, int ednDay) {
    super();
    this.name = name;
    this.startMonth = startMonth;
    this.startDay = startDay;
    this.endMonth = endMonth;
    this.ednDay = ednDay;
}

// getter & setter

}

and iterate over a collection until you find a match, like this:

List<ZodiacSign> zodiac = Collections.emptyList();
zodiac.add(new ZodiacSign("AQUARIUS", Calendar.JANUARY, 20, Calendar.FEBRUARY, 18));
zodiac.add(new ZodiacSign("PISCES", Calendar.FEBRUARY, 19, Calendar.MARCH, 20));
// ..
zodiac.add(new ZodiacSign("CAPRICORN", Calendar.DECEMBER, 22, Calendar.JANUARY, 19));
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
for (ZodiacSign sign : zodiac) {
    if (month >= sign.getStartMonth() && month <= sign.getEndMonth()) {
        if (dayOfMonth >= sign.getStartDay() && dayOfMonth <= sign.getEdnDay()) {
            System.out.println("Zodiac Sign: " + sign.getName());
        }
    }
}
2 of 6
1

You can do it like this

public static void main(String[] args) throws ParseException {

    Scanner userInput = new Scanner(System.in);

    System.out.println("Hello you, Lets get to know each other");

    String userName;

    System.out.println("What is your name ?");
    userName = userInput.nextLine();

    System.out.println(userName + " please enter you DoB (DD/MM/YYY)");

    String dateOB = userInput.next();
    Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dateOB);
    System.out.println(dateOB);

    System.out.println("So your date of birth is " + dateOB);

    Calendar c = Calendar.getInstance();
    c.setTime(date);
    int day = c.get(Calendar.DAY_OF_YEAR);


    // IF day is from January 20 - to February 18 this means he or she is aquarius
    if (day >= 20 && day <= 49) {
        System.out.println("Aquarius");
    } else if (day >= 50 && day <= 79) {
        //If day if from February 19 to March 20 then pisces
        System.out.println("Pisces");
    }
    //write all zodiac signs ...

    userInput.close();
}
🌐
CopyProgramming
copyprogramming.com › howto › java-how-to-take-date-input-in-java
Java: How to input a date in Java programming language
July 16, 2023 - Sufficient information should be obtained for later error comprehension. How to read input type="date" to java object Date?, value = date A string representing a date. A valid full-date as defined in [RFC 3339], with the additional qualification that the year component is four or more digits ...
Top answer
1 of 3
1

You have defined the simpleDateFormat but dont seem to have used it. It will specifically serve what you need.

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(sdf.format(yourDate));

You need to add this logic inside

public String getItem() {
    
    return theTitle + ", " + sdf.format(theDate) + ", " + theTime + ", " + theLocation + ", " + theDuration + ", " + theCategory;
}

Since you hadnt specified anything there, it was calling the Date.toString() method while string concatenation.

2 of 3
1

I recommend you switch from the outdated and error-prone java.util date-time API to the rich set of modern date-time API.

Define a formatter object:

DateTimeFormatter.ofPattern("dd/MM/yyyy")

…and use that same DateTimeFormatter object for both parsing and generating text.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class Main {
    public static void main(final String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter date in the format dd/MM/yyyy: ");
        String dateStr = scanner.nextLine();

        // Define a formatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

        // Parse the date string to LocalDate using the formatter
        LocalDate theDate = LocalDate.parse(dateStr, formatter);

        // Whenever you need to display it, display it using the same formatter
        String backToStr = formatter.format(theDate);
        System.out.println(backToStr);
    }
}

A sample run:

Enter date in the format dd/MM/yyyy: 21/07/2020
21/07/2020

A side note: I can see that you have created a new instance of Scanner for every input. You should create just one instance of Scanner and use the same instance for every input.

🌐
UiPath Community
forum.uipath.com › help
Add date in Java date input field - Help - UiPath Community Forum
December 17, 2018 - Hello, I have problem to type (add) values in to data-field in Java form. I try everything with all options from “type into text” Activities without success. Best Steve