Problem with Java / Linux dates

1

I have a problem with dates, a Spring application deployed on Tomcat. When I retrieve the data from the forms, they are stored correctly in the database in Windows. When I pass the application to Linux, the same date is stored but one day less. The problem is in Spring, since what arrives in the database is the modified date. This date is not modified in any step explicitly, in fact in Windows the same application works correctly.

Has someone happened to you or do you have an idea where that day is lost?

    
asked by user1748166 14.10.2016 в 18:53
source

4 answers

2

If it's good for you I'll give you this code that what it does is Convert date and time between time zones or timezones, very much what you need.

public class TimeZoneCambio {
  public static void main(String[] args) {
    DateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    inFormat.setTimeZone(TimeZone.getTimeZone("America/Guatemala"));
    Date purchaseDate = new Date();

    System.out.println("Fecha/Hora Original");
    System.out.println(inFormat.format(purchaseDate));

    DateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    outFormat.setTimeZone(TimeZone.getTimeZone("Europe/Spain"));

    System.out.println("Misma Fecha/Hora en España");
    System.out.println(outFormat.format(purchaseDate));

  }
}

the output of the program will be like this,

Original Date / Time

2013-04-19 22:54:51

Same Date / Time in Spain

I hope you can use it. 2013-04-20 04:54:51

    
answered by 06.06.2017 в 13:51
1

Because of the information you give, there may be several factors, but the cause is the same. If you use java.lang.Date to represent a date with no time stamp, it takes 00:00 hours on the indicated day. As soon as said date is represented in a smaller timezone, the phenomenon that you comment occurs.

The solution may be to configure the servers well to match the timezone configuration, or to show the date in the client to configure the output component to format the date with timezone, for example in JSF you can apply a formatter.

You can also consider using bbdd date or time types with timezone information.

    
answered by 14.10.2016 в 19:14
1

For the description of the problem and what I see in comments, the reason is for a different configuration in the time zone of the servers. Configure or request that both servers be in the same time zone.

In case you can not change the time zone settings on your Linux server (which would be strange), you can make the change at the application level in Java, in the way the application starts. Based on this response , you would only have to add one parameter when running the JVM:

-Duser.timezone=Europe/Madrid
    
answered by 14.10.2016 в 19:24
0

I changed all the fields in the beans to String to not use Date when retrieving the data of the forms, since date in MySQL can be inserted from String, converting the String of the format of the view to the format of the database with:

private String formatDate (String date, String initDateFormat, String endDateFormat) {
    String parsedDate = null;
    try {
        java.util.Date initDate = new SimpleDateFormat(initDateFormat).parse(date);
        SimpleDateFormat formatter = new SimpleDateFormat(endDateFormat);
        parsedDate = formatter.format(initDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return parsedDate;
}
    
answered by 14.10.2016 в 19:55