3

I've the following problem, that showed up when I installed my webapp's war on a virtual linux server. Here it is: my server system time seems correct, in fact typing date on shell comes out with:

Mon Apr 11 11:47:30 CEST 2011

which is my "wall clock time". Even mysql works fine, if I do select now(), I get:

mysql> select now()
    -> ;
+---------------------+
| now()               |
+---------------------+
| 2011-04-11 11:52:57 |
+---------------------+
1 row in set (0.01 sec)

But my application (Spring+hibernate on Java6 + tomcat 6) will save every date on DB with a GMT timezone (and the time is correctly offset with the difference from GMT, which is good). Now the problem is that I get all dates displayed with GMT, even if I do:

static final DateFormat format = new SimpleDateFormat(
            "dd/MM/yyyy 'alle' HH:mm", Locale.ITALY);

I don't get the hours correctly offset, but they remain in GMT and they display GMT if I put Z (that displays timezone) in the pattern.

Under Windows instead I had date values stored in my native timezone on mySQL. So on my windows machine where I develop I will have all the dates in CEST. This is annoying as I don't know how to predict the way the system will behave and I have to do boring tests and figure out how to change this.

Do you have ever seen this problem? How to deal with it?

gotch4
  • 13,093
  • 29
  • 107
  • 170

4 Answers4

2

DateFormat does support a notion of time zone. Add something like:

format.setTimeZone(TimeZone.getTimeZone("Europe/Rome");
philwb
  • 3,805
  • 19
  • 20
1

DateFormat does not care about time zones, only formatting. In order to change to timezone you need something like Calendar.setTimezone().

nfechner
  • 17,295
  • 7
  • 45
  • 64
1
  SimpleDateFormat sf1 = new SimpleDateFormat("dd/MM/yyyy 'alle' HH:mm:ss");
        sf1.setTimeZone(TimeZone.getTimeZone("Europe/Rome"));
        System.out.println(sf1.format(new Date()));

// List all Timezone
        System.out.println(Arrays.asList(TimeZone.getDefault()));
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112
  • This is good. Anyway how do I control the way hibernate/mysql stores the date? I mean I noticed that on linux it uses GMT, on Windows CEST. I'd like to have it all set to GMT so that nobody gets confused. – gotch4 Apr 11 '11 at 13:15
  • @gotch4: kindly look at this post http://stackoverflow.com/questions/5617459/convert-utc-time-t0-local-time-in-java-or-groovy/5618268#5618268. – Dead Programmer Apr 11 '11 at 14:17
-1

PreparedStatement st = con .prepareStatement("insert into knjiga_osisi (rb_knjiz,Inv_br,Naziv_os,datum_k,rb_pk,opis,ulaz,izlaz,stanje,osnovica_zam,otpis,sadasnjav, vrednost_pp,rashodovanav) values (?,?,?,STR_TO_DATE(?, '%d-%m-%Y'),?,?,?,?,?,?,?,?,?,?)");

This is code for java. Mysql - java european date format

This line is for date

STR_TO_DATE(?, '%d-%m-%Y')

U can test in mysql only date row:

SELECT ID, DATE_FORMAT(Date, '%d-%M-%Y') FROM table1;