leer la entrada del usuario de la clase 'Fecha'

PPC:

Hola soy nuevo en JAVA y he estado buscando en la red para esto y no pude encontrar una solución.

Así que estoy tratando de leer la entrada del usuario y almacenarlos en el vector pero uno de ellos incluye una entrada de fecha. Ahora, he creado una clase de nombre 'Fecha' así que tengo que usar la fecha como el tipo, sin embargo, no sé qué poner como nextLine o nextInt - con el fin de leer.

public static void edit(){
      viewAll();
      Scanner in = new Scanner (System.in);
      int i;


      System.out.println("Enter the Animal Code you wish to edit:");
      String cd = in.nextLine();

      if (cd == code){

       System.out.print("What is the animal's type? "); 
        String type2 = in.nextLine();
        n.add(type2);

       System.out.print("Enter the animal's unique code - Format: (XXXX111)");
       String code2 = in.nextLine();
       n.add(code2);
       //unique
       //XXXX102
       System.out.print("Enter the animal's weight:" ); 
       int weight2 = in.nextInt();
       n.add(weight2);

       System.out.print("Enter the date that the animal was obtained on: (dd/mm/yyyy)");
       Date date2 = in.next();

       list.addAll(n);

       //use date class
       System.out.print("Enter the animal's room and section (location) in the park:");
       String location2 = in.nextLine();
       n.add(location2);



       System.out.println();
       System.out.println("Changes have been saved");
       System.out.println();

     }
Nexevis:

En primer lugar importar las bibliotecas que necesita, incluyendo la fecha con:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

A continuación, puede tomar en la entrada como una cadena y convertirla en Fecha haciendo:

    DateFormat formatter = new SimpleDateFormat("dd/mm/yyyy");
    String dateString = in.nextLine();
    Date animalDate = formatter.parse(dateString);

Usted no tendrá que crear una clase Fecha y puede simplemente la biblioteca java.util.

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=119726&siteId=1
Recomendado
Clasificación