-1

I have a database elsewhere that I have to get my data from that is in another timezone.

To get the latest updated elements I need to convert the Date to the timezone of the database so I can only get the lines changed after a specific time.

For communication with the database I created a Java class with all kind of static final elements. I would like to add a static final DateFormat with a SimpleDateFormat and a TimeZone so I can always use this FINAL DateFormat to change the date to the datetime of the database everywhere in the application.

So I have a class:

public class Data_DB {

    static final DateFormat FORMATTER= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}

I can use the DateFormat everywhere in my application, quite nice. But I want to change the TimeZone of formatter too.

public class Data_DB {

    static final DateFormat FORMATTERSD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static final DateFormat FORMATTERDB = FORMATTERSD.setTimeZone(TimeZone.getTimeZone("XXXX/xxxx"));
}

But the return value of FORMATTERSD.setTimeZone(TimeZone.getTimeZone("XXXX/xxxx")) is void.

I do not want to instantiate Data_DB to add the TimeZone to it.

In what way can I solve this to get a static final element FORMATTERDB that I can use everywhere in the application?

irJvV
  • 892
  • 8
  • 26
  • seems a stupid question (-1) so i will delete it, thanks for the help though =^) – irJvV Oct 06 '17 at 13:26
  • @Hugo can you delete this question ? Seems i can not do this =^( – irJvV Oct 06 '17 at 13:33
  • I can't: https://meta.stackexchange.com/questions/5221/how-does-deleting-work-what-can-cause-a-post-to-be-deleted-and-what-does-that –  Oct 06 '17 at 13:36

2 Answers2

3

You can do like this :

static final DateFormat FORMATTERSD;
    static {
        FORMATTERSD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        FORMATTERSD.setTimeZone(TimeZone.getTimeZone("XXXX/xxxx"));
    }

The static will be called once.

Please note that a SimpleDateFormat is not thread safe, if you have multiple thread, it could not work

Tuco
  • 493
  • 2
  • 6
  • 18
  • Great thanks, that works well =^) Data_DB.FORMATTERSD.format(new Date()) indeed gives the right result =^) – irJvV Oct 06 '17 at 10:41
2

Note that SimpleDateFormat is not thread safe. So two threads may at the same time change the internal state of the static object, playing haywire.

Also a final object and then changing a field with setTimeZone would change the original object!

static DateFormat formatterSD() {
    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}

static DateFormat formatterDB() {
    DateFormat df = formatterSD(); // Must be a *new* SimpleDateFormat.
    df.setTimeZone(TimeZone.getTimeZone("XXXX/xxxx"));
    return df;
}

With java 8 come many new classes. A bit overwhelming at first, but worthwile, as such issues are resolved there. See LocalDateTime.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I was already doing some research how to make it thread safe. It is not needed for now but better do it as secure as possible. In your solution you add functions to solve this. I would rather use a static final element without instantiations or functions, just a simple call to Data_DB.FORMATTERSD with whatever comes next in a formater form (it has to be changed to mach the TIMESTAMP in the DB). If i find it i will post it as an answer here, thanks =^) – irJvV Oct 06 '17 at 11:19
  • 3
    @irJvV The scary thing with one mutable singleton constant, is that an idiot could use it, set his Polynesian time zone, and then other parts are messed up. Java 8 did solve that in the new time classes. Maybe make a child class without usable setters. – Joop Eggen Oct 06 '17 at 12:35
  • 2
    @irJvV The best alternative for a thread-safe formatter is to use Java 8's [new java.time API](https://docs.oracle.com/javase/tutorial/datetime) - it also has the advantage of solving [many problems](https://stackoverflow.com/q/1571265/7605325) and [design issues](https://stackoverflow.com/q/1969442/7605325) of `Date` and `SimpleDateFormat`. And for JDK 6 and 7, you can use the [ThreeTen Backport](http://www.threeten.org/threetenbp). Also see [here](https://stackoverflow.com/q/29477166/7605325) and [here](https://www.javacodegeeks.com/2010/07/java-best-practices-dateformat-in.html) –  Oct 06 '17 at 12:38
  • True, the java.time classes may seem overwhelming at first. But just search Stack Overflow for the main class names, and get the hang of it quit easily by reading example code: Instant, OffsetDateTime, ZonedDateTime, ZoneId, LocalDate, LocalDateTime, and DateTimeFormatter. – Basil Bourque Oct 06 '17 at 16:26
  • @Joop Eggen After testing all the options i decided to turn the Java application into an multithreading application so i will use your solution. (Tuco gave the answer to the question though so i accepted his answer) – irJvV Oct 13 '17 at 07:17
  • @irJvV thanks for the feed back (and points I already have enough). – Joop Eggen Oct 13 '17 at 09:31