Using timezone 'EST' in freemarker template - java

<#setting time_zone="America/New_York">
Time: ${response.currentDate?string("MM/dd/yyyy hh:mm a zzz")}.
I need the timezone to be displayed as 'EST'.
But currently, when i run the application and the email gets generated from the template above, it is displaying as 'EDT'.
Can you please let me know what needs to be done to show as 'EST'?
The current timezone (on the system where I am testing) is Indian Standard Time
Thanks!

try <#setting time_zone="US/Eastern">

The behavior is expected at this point of year.
Refer: http://www.timeanddate.com/library/abbreviations/timezones/na/edt.html
(thanks to the above comment by Jon)

Related

Java(Spring) automatically correcting date by timezone bug

so I have a task to fix a bug. So basically I make a GET request for /report-information and get in response two fields: Date dateFrom, Date dateTill.
The correct date that it should return should be: 2019-07-01 00:00:00 and 2019-07-31 23:59:59 - that's the data from a database. Basically, I make a GET request for these fields, Java takes it from database and sends it back to me.
But the problem is that somehow the values that are returned after making a GET request are: 2019-06-30 21:00:00 and 2019-07-31 20:59:59. Basically -3 hours because of some automatic timezone correction.
What I need is to create a method or something similar that would make java ignore the timezone and wouldn't change the date. I should use Date variables not to mess up a lot of other code that uses these fields.
Also I should mention that when I debugged the whole process, somehow in the #RequestMapping method it returns these fields correctly, as they should be, but when I make a request through my browser or Postman, I get the corrected by timezone version of the date. But it is possible that I just missed something.
Do you have any suggestions or ideas why and at what moment does Java change the date automatically? And what should I do to prevent it from correcting the date.
Thank you!
Since you have mentioned that in your Controller code you can see the correct date, you may want to check the Jackson serializer ( assuming this is the one serializing objects to json ) settings used for serializing the date object into JSON. You can check the multiple options for configuration here.
i also face this problem some day ago....
in my case it's arise from Jackson lib (i project is in spring boot)
to fix this problem i set my time zone for Jackson lib in application.property file
spring.jackson.time-zone:Asia/Dhaka
in my case i am in Dhaka Bangladesh
also try by this
for more details

Google App Engine - GAE will not set Default TimeZone

I have tried the following so I can get Date based on my timezone which is "Africa/Johannesburg" or GMT+2:00 but Google servers always return time using its own timezone which is 2 hours behind mine.
I have done the FF:
in appengine-web.xml I have set
<property name="user.timezone" value="Africa/Johannesburg"/>
I have also tried TimeZone.setDefault(TimeZone.getTimeZone("GMT+2:00")); before creating Date object
in the init method of my servlet, I have also tried
#Override
public void init() throws ServletException {
TimeZone.setDefault(TimeZone.getTimeZone("GMT+2:00"));
}
But this thing won't just work. Because JDK date is not thread safe, I am using JodaTime, which works well, In fact when I do new DateTime(DateTimeZone.forID("Africa/Johannesburg")) I get correct time but for legacy issues, I have to store date in JDK date hence have to convert Joda to JDK Date by invoking .Date(), then the time is completely screwed up in wrong timezone.
Does anyone by chance know how to set this without having to subtract the hours difference.
You can't. The system timezone is not changeable. You should store all of your dates in unix time and convert them to a Date or Calendar object using your timezone. I also would not assume that GAE is always going to use the same timezone...
When you save any date in Datastore it will be saved in the timeZone you have set in your JVM, thats why before starting the app I always set it to UTC:
//To avoid difference of dates depending on where the server is located
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Nonetheless when you browse the datastore in the gcloud console it will be shown in your local timezone (probably it gets the browser timezone and adapts the response to you). But when you query it back the calendar date taken in count will be the one you used for saving it (In my case UTC).

how TimeZone.getDefault() works?

My server is in US and I am accessing the application in India through
web browser, in that case what TimeZone.getDefault() will return?
If it returns Time Zone based on India on basis of what it will return?
I have changed in control panel setting to different locale and
different time zone of the system even though it is not changing based
on my settings.
I have written the code as fallows...
def dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT,Locale.getDefault())
dateFormat.setTimeZone(TimeZone.getDefault())
It's going to return the timezone of the JVM TimeZone.getDefault() is executed on. So if the application is running on a server in India, it will be something like "Asia/Calcutta".
Default time zone is usually set for host, not user or application. In your case it will be default time zone for a server where your application is running, most probably US time zone.
Try to run command date +%Z in Unix console on server.
This is a matter of 'where this code executes'. If you are talking about a web application which is being accessed by a browser in India, the kind of date you get will be in US. Well, unless you set the proper locale and timezone for the user's session. In frameworks like Vaadin once you call setLocale() on the application it sets the timezone as well, but in other platforms you might have to explicitly use a date formatter with a specific timezone.

Suspicious JVM Time Behavior

I have totally strange problem on one of my machines. I've written a program which is monitoring events from our servers and displays them on a monitoring pc. However, each event status message also contains a TIMESTAMP which is being retrieved by the following calls:
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());
with
public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
I'm expecting this call to return the local time, which is and was working great until today. But now my program shows me the local time +3 hours on every incoming event. A restart of the jar file does not help at all and the System Time is also set up correctly. This program is running on a machine which is synchronizing its time with an central corporate clock.
Can anyone explain me the mentioned behavior and/or state a possible solution which is not "restart the machine and try again" :-) ?
Cliffs:
- Retrival of Time is working on my- or any other machine when I start the Program
- The target Machine is running 24/7 and is only a monitoring machine, so no one is changing any options there
- It worked fine until today.
- All Time/Timezone settings on the Hostsystem (Windows XP) are correct. (checked via CMD -> "date" and also over the System Preferences)
I appreciate any kind of answer to this issue. Thank you for your time!
the Question is answered since the 25 already. I've implemented a explicit call of Timezone.setDefault() to make sure that the proper timezone is being shown the whole time. I'll close this Question now.
Thank you all for your answers!
This code:
cal.getTime()
Returns a Date object. A Date object is simply a wrapper for the UTC time as a long. In the code you showed us, it is just as correct to do sdf.format(new Date());
What is the format DATE_FORMAT_NOW? I assume you specify this yourself. Do you specify the timezone in this format?
EDIT As you mentioned in your comment, DATE_FORMAT_NOW does not specify a timezone. You should do both Timezone.getDefault() and sdf.getTimezone() to see if the value has changed to your locale + 3 hours.
EDIT2 I found this forum post about the time sporadically changing. In this case it was caused by an Oracle driver calling Timezone.setDefault(...) in the middle of a method, then setting it back afterwards.

Weird mysql beahviours with timezones? How to control them?

I wrote a webapp using spring+hibernate. I developed everything on windows and then I moved it to a Linux virtual server (Aruba, an Italian provider). I noticed an annoying thing: when dates where saved on windows the time would be the same of my "wall clock", so if I read 13:45 I will have the same hour in the mysql row. This doesn't happen on Linux anyway. In fact the linux machine is on CEST as well (my timezone), I got it typing "date" in the shell. But I get the dates saved in the DB with an offset that is relative to GMT. Again, my app always displays everything in GMT (Including GMT as a time zone if I choose to format the dates to display the time zone) and mysql saves everything in that format. How do I control all this?
I post the solution by myself, because I think it's worth having it in this site.
First of all: mySql doesn't store any timezone information. So say that you are running on GMT+4 and you write a couple of records that contain date fields. Then you move your system in GMT-2 you read those records (perhaps importing the data from mysqldump). If your system and VM have GMT-2 as timezone the dates you read will be taken as if they were written in GMT-2 and NOT ADJUSTED.
Solution: Take control of your VM timezone by using -Duser.timezone="GMT" command line option (you can even put this in your Tomcat startup script) or your preferred timezone (but GMT is better, let me explain why). This way you'll know for sure which timezone your VM is running. This doesn't mean that Java VM will assume that your system time is the one you specified in your user.timezone, it will know the system timezone and adjust dates accordingly. In fact if you are not in GMT, you will see dates in adjusted to GMT and saved to DB accordingly. This way you'll be sure that you are using that as a reference.
The problem is that if you take a date object and you do myDateObject.toString(), you'll get the date adjusted to GMT, with the hour offset. Which is not what you'll probably want.
The solution is to use SimpleDateFormat and do somthing like this when you have to output a date:
SimpleDateFormat dateFormat = new SimpleDateFormat(
"HH:mm dd/MM/yyyy z", Locale.ITALY);
dateFormat.setTimeZone(TimeZone.getTimeZone("Europe/Rome"));
Everything will get converted the right way. You can even go further if you are developing a web app. You can extract the timezone from the HttpRequest and adjust date output accordingly, but I didn't go so far as I'm writing an application that is intended for Italian users only :D (yay).
Hope this will help.

Categories

Resources