2013-07-29 06: 35: 40: 622 GMT to 2013/07/29 with 2013/07/29 Date type and not String

0

I have that date format 2013-07-29 06:35:40:622 GMT and I want this to get this format 2013/07/29, I have seen that with format of SimpleDateFormat can but returns me% String and I want 2013/07/29 in Date format.

Is it possible or not?

    
asked by Yassine 26.10.2016 в 15:47
source

2 answers

1

Joacer's answer tells you how to convert, however I believe that your doubt comes more to a perception problem of Date from the database.

In Java, the Date object saves the epochtime in milliseconds in a long time, so it will be the same 2013-07-29 06: 35: 40: 622 GMT as 2013/07/29 on the date.

If you need a Date for database you need the Date class of SQL and you can not work both via import, so one of them must be called with its canonical name. java.util.Date or java.sql.Date

link

    
answered by 26.10.2016 / 16:34
source
2

I believe that following these steps you can get the date transformation you want:

  • Convert the text of the first date to a Date object
  • Convert the Date date to the second format you want
  • Create the Date object with the new formatted date
  • An approximate code would be the following:

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //He puesto este formato, pero puedes poner el que más se adecue a tus necesidades
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
    
        try{
             //Convertimos el texto de la primera fecha en un objeto Date
             Date fecha = sdf.parse("2013-07-29 06:35:40");
    
             //Convertimos la fecha Date al segundo formato que queremos              
             String fecha2 = sdf2.format(fecha );
    
             //Creamos el objeto Date con la nueva fecha formateada
             Date fecha = new Date(fecha2 );
        }catch (Exception e){
            e.printStackTrace();
        }
    

    I hope it helps you

        
    answered by 26.10.2016 в 16:18