Put date and time in a variable

1

I would like to know how you can put together a date and time delivered by a user in a single variable of type Date

System.out.println("Ingrese fecha desde la cual se desea programar los pedidos para su producción");

    SimpleDateFormat sdfg = new SimpleDateFormat("dd/MM/yyyy");
    String fe=lector.readLine();
    Date fechaProg=sdfg.parse(fe); 

    System.out.println("Ingrese hora desde la cual se desea programar los pedidos para su producción");
    SimpleDateFormat hora=new SimpleDateFormat("HH:mm:ss");
    String h=lector.readLine();
    Date horaProg=hora.parse(h);


    SimpleDateFormat fechaHora= new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
    Date fh;
    fh = fechaHora.parse(h+fe);// creo que el problema esta aqui pero no se como arreglarlo
    cxn.EnviarProgProd(fh);
    
asked by sebastian 13.11.2016 в 00:29
source

2 answers

3

Your format contains a space between the date and time:

"dd/MM/yyyy hh:mm:ss"
           ^ aquí

When you send the string to be processed, it does not have that space:

fechaHora.parse(h+fe);

Add the space manually:

fechaHora.parse(h+" "+fe);

Also, consider that you are using different formats for the time:

//cuando capturas la hora, lo haces con HH
SimpleDateFormat hora=new SimpleDateFormat("HH:mm:ss");
//...
//cuando quieres la fecha y la hora, la hora tiene formato con hh
SimpleDateFormat fechaHora= new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
    
answered by 13.11.2016 в 01:19
1

try with:

 fh = fechaHora.parse(h+" "+fe);
    
answered by 13.11.2016 в 01:10