How to handle missing PST hour in Java - java

I have a webapp that is to be deployed on server having Pacific TimeZone setting. My app is capable of handling PST<->PDT date conversions in calculations but i am facing issue handling missing hour/Invalid Time as described below.
Invalid Time – Between 02:00 AM to 03:00 AM on second Sunday in March due to forward auto-adjustment of clock, this time does not exist in Pacific Time Zone.
So whenever date like 10-Mar-2013 02:00:00 is entered system automatically converts it into 10-Mar-2013 03:00:00. I understood this is happening because this time is actually does not exist in Pacific timezone.
But as per requirement server timezone can not be changed (for eg. GMT) and still need to capture above time (10-Mar-2013 02:00:00). Additionally server auto-adjutment clock settings also can not be changed so i have to do application code change to support above.
Thanks in advance for your help.
Adding code for more clarification:
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyy HH:mm:ss");
//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = sdf.parse("100313 02:00:00");
Date date1 = sdf.parse("100313 03:00:00");
System.out.println(sdf.format(date));
System.out.println(sdf.format(date1));
Output:
100313 03:00:00
100313 03:00:00
If i uncomment commented line then output:
100313 02:00:00
100313 03:00:00

Regardless of the time zone setting of the computer, you should always be able to get the current UTC time. Java abstracts this from the OS details, so any time the documentation says you are getting a UTC value (such as milliseconds from 1/1/1970 UTC) then it is accurate. There is no such thing as missing or ambiguous time in UTC.
The reason that it is recommended your servers are set to UTC time zone has to do with the way the system bios clock is synchronized with the operating system's clock. Microsoft Windows operating systems keep the bios on local time and adjust it as necessary, while Linux and Macintosh keep the bios permanently on UTC. Because of this, on Windows systems, there is a remote possibility that the clock is read incorrectly if the adjustment didn't work for some reason. More on this issue here.
If your application is converting dates entered to values that only make sense in the computer's local time zone, then you have projected that time zone on to your users. For desktop applications, this is probably correct because the user is running the code on their own computer. For web applications (and other server-side code), this is certainly the wrong thing to do, because it is possible for the client to be in a different time zone than the server.
Please update your question to show the specific code from your application that is causing the problem you described. We can then point you at the specific changes you need to make so your application behaves correctly.

Related

Can't get current time forecast using forecast.io API

I'm building a mega simple weather Android app using the forecast.io service.
Using the default call of
https://api.forecast.io/forecast/<my_key>/37.8136,144.9631?units=auto
Always seems to give me the forecast at a time that I don't care for - ie, its currently 2.40PM here, and it keeps giving me 3.40AM forecast.
So I then try to use the time param and yet it still gives me 3.40AM, not 2.40PM
https://api.forecast.io/forecast/<my_key>/37.8136,144.9631,1451619638?units=auto
I've validated 1451619638 as my current unix time via the helpful site http://www.epochconverter.com/
Any pointers as to why I can't seem to get MY local current time?
The API returns the timezone and then the offset from GMT. Check those.
The idea of the first one is if you do not provide the time, you will get a current forecast, and all you will have to do is convert it into your timezone. So if they are doing GMT and you are GMT - 8, you simply subtract 8 from the time value.
Both fields are there:
latitude: The requested latitude.
longitude: The requested longitude.
timezone: The IANA timezone name for the requested location (e.g. America/New_York). This is the timezone used for text forecast summaries and for determining the exact start time of daily data points. (Developers are advised to rely on local system settings rather than this value if at all possible: users may deliberately set an unusual timezone, and furthermore are likely to know what they actually want better than our timezone database does.)
offset: The current timezone offset in hours from GMT.
I have been using this API for a while.

Time Zone issue with server and html client

Basic requirement is Server has data with GMT +00.00 and Browser needs to request data from server between certain dates(not time).
Let's say, one record saved on server on 28th March,2015 Night from USA local time. Server is following GMT +00.00 so record saved with 29th March, 2015 morning with GMT.
Now HTML is retrieving same record from the server. Server have GMT 29th March,2015. But now I want to display local date as per time zone of any browser. Client and server is communication with date only not time.
Right now server will give 29th March, 2015 to client. But It should display as a local date i.e. 28th March, 2015. So how can I manage display date among the different time zone?
Any idea or suggestions will be appreciated.
Well, one solution would be to create a column in your Database with the real timestamp of the date, time included. So, you could show this date (reformated if you want) to client and keep the GMT +00.00 for the process part.
The problem here, if think is that since you don't save the time part, it's impossible to say if it was Day/Night.
The other solution would be to change your actual date to datetime and save it in GMT +00.00 with another column specifying the offset between GMT +00.00 region and the other (here, USA).
Some maths should do.
I can't propose a written solution since you don't specify your server language (unless it's Javascript ?).
By all means, good luck with that ;).

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.

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