how to remove the time from the date in java?

0

I have a variable of type Date called dateElab and I use it in the value of a p: Calendar of primefaces

<p:calendar id="mask"  pattern="dd/MM/yyyy" mask="true" locale="es" value="#{Registro.datosVO.fechaElab}"

So when I select any date, I mark the following TESTING DATE AWED Feb 14 00:00:00 CST 2018 and always marks the same time, then what I want is to remove the time How can I do it?

    
asked by Root93 14.02.2018 в 23:08
source

3 answers

1

In your database the field should be of type date () you probably have it as a timestamp with time zone , in this case even if you format it the date will always put the time in zeros 00:00:00, and if you can not change the data type in the database then when you retrieve the date you can do a SimpleDateFormat for example:

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

public class FormateDate {

    public static void main(String[] args) throws ParseException {
        String date_s = "2011-02-19 00:00:00.0";

        SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = dt.parse(date_s);

        SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(dt1.format(date));
    }

}
    
answered by 19.02.2018 в 16:36
0

A java.util.Date represents the milliseconds that have passed since a date called epoch (01/01/1970 00:00:00 GMT).

Therefore, milliseconds is its precision. It will depend on your database engine the exact type of storage and with what precision date, datetime, datetime with timezone.

If you want to display that date without the time details, then you have to specify a display format, with SimpleDateFormat for example.

    
answered by 15.02.2018 в 01:16
0

To save you the date you have to do the following:

  • dateElab must be Date() in java
  • dateElab must be mapped as Date
  • in the database Date no datetime "this depends on which database you are using"
answered by 15.02.2018 в 00:38