Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a date as input in java?

Is there any direct way to set a date to a variable but as an input? I mean that i don't know the date at design time, the user should give it. I tried the following code but it doesn't work: Calendar myDate=new GregorianCalendar(int year, int month , int day);

like image 940
user3159060 Avatar asked Dec 20 '14 13:12

user3159060


People also ask

How can I get scanner input date?

Scanner sc = new Scanner(System.in); ToolBox. readDate(sc, "YYYY-MM-DD"); ToolBox. readDate(sc, "YYYY-MM-DD HH:mm:ss"); ToolBox. readDate(sc, "YYYY-MM-DD"); sc.

How do you initialize a date?

Calendar to initialize date: Calendar calendar = Calendar. getInstance(); //To Initialize Date to current Date Date date = calendar. getTime(); //To Initialize Date to a specific Date of your choice (here I'm initializing the date to 2022-06-15) calendar.


Video Answer


3 Answers

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);
like image 133
sitakant Avatar answered Nov 02 '22 01:11

sitakant


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 );

Table of date-time types in Java, both modern and legacy.


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.

like image 22
Basil Bourque Avatar answered Nov 02 '22 01:11

Basil Bourque


Maybe you can try my simple code below :

SimpleDateFormat dateInput = new SimpleDateFormat("yyyy-MM-dd");
Scanner input = new Scanner(System.in);

String strDate = input.nextLine();

try
{
   Date date = dateInput.parse(strDate);
   System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
} 
catch (ParseException e) 
{
   System.out.println("Parce Exception");
}
like image 21
Ghayuh F P Avatar answered Nov 02 '22 02:11

Ghayuh F P