-2

I'm writing a registration activity and I wanted to let users enter the registration date for storing the record but the problem is how do I get the date as input from user using scanner? Did this but it somehow says there's an error sorry I'm still new to java.

System.out.println(" Enter in  following format : dd-mm-yyyy");

String regDate=s.nextLine();

SimpleDateFormat format=new SimpleDateFormat("dd-mm-yyyy");

Date regDate=format.parse(date);
Andrea Ebano
  • 563
  • 1
  • 4
  • 16
Anonymous
  • 74
  • 2
  • 10
  • by reading it as a String, which you then parse to a Date – Stultuske Jul 10 '19 at 11:24
  • What you tried already about this? – akortex Jul 10 '19 at 11:28
  • read it as string and parse but it says date cannot convert into string? – Anonymous Jul 10 '19 at 11:44
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 10 '19 at 12:07
  • [foollove99](https://stackoverflow.com/users/5035754/foollove99) would like to ask: What is the input? From the date format, user need to enter: 10-07-2019 – Ole V.V. Jul 10 '19 at 12:10
  • okay I'll try thanks for the suggestion – Anonymous Jul 10 '19 at 12:10
  • Welcome to Stack Overflow. A thing to learn here is when you ask about code that isn’t working, be precise about how it fails. Quote any error message verbatim. In case of wrong results, give an example of input, expected result and how observed result differs. It shows an effort on your part, giving you a much better response, and it also gives us a much better starting point for helping you. See you around. – Ole V.V. Jul 10 '19 at 12:44

3 Answers3

0

Here is a really small example with proper exception handling:

public class DateInputTest implements Closeable {

    private static final String DATE_FORMAT = "yyyy-MM-dd";

    private final Scanner scanner;

    public DateInputTest() {
    this.scanner = new Scanner(System.in);
    }

    public LocalDate getDateInput() {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
    String line = scanner.nextLine();
    try {
        return LocalDate.parse(line, formatter);
    } catch (Exception e) {
        System.err.println("Invalid date value:: " + line);
    }
    return null;
    }

    @Override
    public void close() throws IOException {
    scanner.close();
    }

    public static void main(String... args) {
        DateInputTest test = new DateInputTest();
        System.out.print("Please input a date [pattern yyyy-MM-dd]: ");
    Optional.ofNullable(test.getDateInput()).ifPresent(System.out::println);
    }

}
akortex
  • 5,067
  • 2
  • 25
  • 57
0

You can't have two variables with the same name. Also, date isn't defined anywhere. Last line should be like this: Date date = format.parse(regDate);

TechMoose
  • 1
  • 2
0

You can do it like this:

import java.text.SimpleDateFormat;
import java.util.*;

class Main {
  public static void main(String[] args) throws Exception {
    System.out.println("dd-mm-yyyy");
    Scanner scanner = new Scanner(System.in);
    String date = scanner.nextLine();
    Date date1=new SimpleDateFormat("dd-MM-yyyy").parse(date);  
    System.out.println(date1);
  }
}
Sagun Raj Lage
  • 2,326
  • 2
  • 18
  • 28