thymeleaf shows incorrect date(example, entered: 03-05-2018, showed 02-05-2018)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

thymeleaf shows incorrect date(example, entered: 03-05-2018, showed 02-05-2018)


Thymeleaf ( It is a part of the form)
<input class="form-control" type="date" th:field="*{fechanac}">.

What I have done
--------------------
I have tried using pattern=....., in entity persona and
 in thymeleaf
Also I tried using <input class="form-control" type="text" th:field="*{fechanac}">

but the result is the same, 1 day late.
Any Ideas to show the right date?????

alexchi
I have a problem with dates.

When I enter the date using <input type="date"> or <input type="text"> the date is correctly recorded into the database MySql, but when I list the date using Thymeleaf, the date is late.


Example: I entered 03-07-2000
database: 2000-07-03   ok
browser (view): 2000-07-02 incorrect (1 day late). I have used Chrome, Opera, Firefox. It is the same

Example: I entered 08-04-2010
database: 2010-04-08   ok
browser (view): 2010-04-07 incorrect (1 day late)


I have the following information:

Entity
        @Temporal(TemporalType.DATE)
        @DateTimeFormat(iso=ISO.DATE)
        private Date fechanac;

Thymeleaf (it is a crud using table. To show)
Reply | Threaded
Open this post in threaded view
|

Re: thymeleaf shows incorrect date(example, entered: 03-05-2018, showed 02-05-2018)

Reiju
It is not thymeleaf issue.
Problem with ur conversion from Date.class to String.

Try something like this in your controller
@InitBinder("yourFormDto")
public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd.MM.yyyy"), true, 10));
}

Or you can implement Spring's Converter<Date, String>...
Reply | Threaded
Open this post in threaded view
|

Re: thymeleaf shows incorrect date(example, entered: 03-05-2018, showed 02-05-2018)

alexchi
Hi.

I tried your code and something similar to your code, but It does not work. I show you my video here....

https://youtu.be/K_Bdob74Y-g

I am not sure how to implement your second solution. I´m new.
Reply | Threaded
Open this post in threaded view
|

Re: thymeleaf shows incorrect date(example, entered: 03-05-2018, showed 02-05-2018)

Reiju
binder.registerCustomEditor(Date.class, new CustomDateEditor(new MyDateFormat(), true));


class MyDateFormat extends DateFormat {

    //This method will parse your date string  received from from
    @Override
    public Date parse(String s, ParsePosition pos) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); //format of your date string
            Calendar c = Calendar.getInstance();
            c.setTime(sdf.parse(s));
            return c.getTime();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //This one converts stored date from db to string
    @Override
    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DATE, 1);  // number of days to add
            return toAppendTo.append(sdf.format(c.getTime()));

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

This is not ideal solution but it can help.
Anyway there is some problem in date-parsing...
try to use same dateformats on frontend and backend instead of "dd/MM/yyyy" and "yyyy-MM-dd".

Look at your entity in debug-mode or just use System.out.println(date field) before sending it to thymeleaf-view. Maybe you obtain wrong date from database and there is problem with entity mapping.
Reply | Threaded
Open this post in threaded view
|

Re: thymeleaf shows incorrect date(example, entered: 03-05-2018, showed 02-05-2018)

alexchi
Hi.

Unfortunately, the code did not work.

I found the same error on the internet using Java but not spring.

The solution is here:
https://es.stackoverflow.com/questions/168663/porque-java-resta-un-d%C3%ADa-a-la-fecha-obtenida-de-un-executequery-ejecutado-a-la-b 


This guy added serverTimezone=america/Mexico_City
I tried this solution, But It didn't work.  
When I write serverTimeZone=UTC, the date in the view is wrong, but the date in MySQL is ok

When I write serverTimeZone=America/Santiago, the date is wrong in the view and database.

I tried serverTimeZone=GMT-4, but nothing.

If I omit  serverTimeZone I get "The server time zone value 'Hora est. Sudamérica Pacífico' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specific time zone value if you want to utilize time zone support."

I verified the timezone in MySQL, first select now(), it was ok (Chile time and date) 2019-05-25 13:46:55
then I wrote select @@global.time_zone, the answer was system, therefore it was ok. I tried everything and nothing works

Well, if anyone can check the CRUD with the error here it is:
https://www.dropbox.com/s/xse6mkd3z5tc9bt/crudpaginador2.zip?dl=0

Lastly, my MySQL version is 8.0.15 Community Server -GPL