Best way to receive date from the "keyboard" [closed]

-1

Good morning, little gallery.

I need to know how best to store dates in java.

Example: In a clients class, I need to receive the CustomerDateDate, but for this I need to receive that date from the keyboard. What is the best way to do this?

    
asked by anonymous 14.06.2018 / 05:53

2 answers

1

One way to do this is to receive day, month, year and use the data to set a Gregorian Calendar.

Example:

Scanner leitura = new Scanner(System.in); //Inicializa o leitor do teclado  

System.out.println("Digite o dia");  
int dia = leitura.nextInt(); // Ler do teclado e armazena na variavel dia   

System.out.println("Digite o mês"); 
int mes = leitura.nexInt; // Ler do teclado e armazena na variavel mês


System.out.println("Digite o ano"); 
int ano = leitura.nexInt;  // Ler do teclado e armazena na variavel ano 

GregorianCalendar dataDeNascimento = new GregorianCalendar();
dataDeNascimento.set(ano, mes-1, dia);  //Em GregorianCalendar os meses começam a partir do 0 e sim, ano é o primeiro a ser recebido.

There are other ways to do this, for example, using Date, but most of the methods are discontinued.

I usually do the way I've shown above for you and depending on the operations you're going to do, GregorianCalendar has many functionalities such as getting the date in Milliseconds and more ...

If you'd like to read more: DevMedia - JavaCalendar

    
14.06.2018 / 06:10
0

Does it necessarily have to be the keyboard? If not, you can use the JDatePicker . If you need to, you can use "dd / mm / yyyy".

Scanner s = new Scanner();
String[] raw = s.next().split("/");
int dia = raw[0];
int mes = raw[1];
int ano = raw[2];
    
14.06.2018 / 07:49