Store customized date in database - java

I have an application which is used by all over world. But the application server is present in India.
One of my application features is
**create template using date
So, for that I have to store the client systems date & time.
But I need to store date time based on Client side or server side in single column in database?
What is the best approach to do this?

I think you have a few options here, depending on how you wish to delegate responsibility and how the templates will be used.
The simplest solution is simply that the client submits the date & time and you store it as is with no time zone information. This however assumes that the template would only be used in the same time zone, or that it is acceptable to have the same date & time visible everywhere according to where it was created (probably not the best approach for a worldwide system)!
Another option is that you store the value as UTC. Then you can either:
Make the client responsible for converting to their local time
zone
Store the offset / time zone separately and include it in the result
This is probably the best approach
Or another option is to store the date & time together with the time zone it was created within. Then you might have to convert between different time zones either on the server or client side if you need a different one.

Related

Equal today date to date after a year

I am trying to license my product(java app) with a simple technique by comparing two dates.I want to know how should I increment the date when I give the app to my client.I need to increment the date everyday,so that when it expires it doesnt work.
I dont want to use the system date,cause there are chances the client might change that.So could anyone provide me any suggestions.
Since the java app is going to be running on your client, you have basically two options (simplistically speaking).
Option1. Use a (possibly) encrypted file on your client's filesystem to store the date you want to store and compare against your expire date.
Option2. Upon registering, register to a server. Now the server knows when you registered, so it should be able to determine when you expire. Each time the application starts, make a call to the server asking if it has expired. This way, you have transferred part of the expiry logic to the server.
Keep in mind that both approaches in their simplistic form can be easily circumvented but that is most of the time the case when you have an app running on clients' computers.
One way i can think of is
Use webservice to get current date in your client
earthtools.org provides a free web service to get the time zone from a city here:
http://www.earthtools.org/webservices.htm#timezone
You just pass in the long/lat values like this: (This is for New York)
http://www.earthtools.org/timezone-1.1/40.71417/-74.00639
Use java Rest client to get date in your code
http://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/

How to get originals date and time in java

Actually
Date date = new Date();
will give us date of that server where application is running.
if particular server has set date and time wrong in that case how we can get real date and time?
I would not worry to much about the server's correct date. I mean a server is usually administrated by you, so just set the time correctly or use NTP to automatically set the correct time.
It is more the client's date/time you should worry about since this is set by your users and can be wrong all the time. If you need to trust the time on client and need it to be reliable and comparable for several users, then retrieve the time from your server and account for latency, if need be.

How do i get date from database other than GMT format?

Trying to get a time stored in a datadase.
select dbtimezone from dual gives me -07:00
I am using Java program to get the Date from Oracle
Column i type of Date.
while i am fetching the time in my java program am getting it as GMT.
actually i want the time as it is there in database not converted time.
Though i can convert back to -07:00 , i am seeking another way to do because conversion always depends on the dbtimezone of the database using.
Can any one help me ?
Thanks in advance
That oracle just has one timezone can make life difficult if you deal with different timezones. I've always thought life was easier if you consider timezone a view artifact and
represent all times as UTC, then convert in the view. You put the timezone information someplace in the database and convert accordingly.
...actually getting that right can get interesting because you don't want to make the same mistake of being too general again. For example, a client may be based in a particular timezone, but have offices in many. Though an office is in a particular timezone, the activity relating to the time may involve a different timezone etc.
See java.util.TimeZone, more specifically the getOffset methods, which return the number of milliseconds to add to the UTC time to get local time. Note that it also considers the daylight saving time.

Client side time zone support in GWT

I'm working on a GWT app where I need to support the following scenario:
The server is located in time zone A
The client's browser is set to time zone B
The GWT app is configured to display date/time in time zone C
Since GWT does not support Calendar and the native support for time
zones in javascript is non-existent I can't think of a nice and clean
solution to this problem.
Have any of you done something similar or do you know of any good
utils I could use?
Thanks!
In my experience, the following best practice significantly reduces complexity and confusion when dealing with dates and timezones in gwt:
Whenever operating/storing dates within the application, treat all dates as milliseconds since epoch in GMT timezone. You can store them as string or int, it doesn't really make a difference.
Whenever displaying the date to the end user, format the date using appropriate timezone.
For your case, when you create a date on the Server (timezone A) convert it to milliseconds since epoch in GMT before sending it to the Client. On the client, use DateTimeFormat (or write your own date formatter util) to convert it into either timezone B or timezone C as appropriate.
You can't change the GWT timezone, hence all java.util.Date's has the browser timezone. You will need to handle the current timezone setting manually.
I see 3 options:
You manage the timezone conversion yourself.
You override the serializer/deserializer of java.util.Date like in this post. And maybe using a custom java.util.Date implemtation, that overrides the getTimezoneOffset(). This approach requires recompilation of the GWT API!.
You implement your own Date, either by extending java.util.Date (like in option 2) or wrapping it with some timezone object. In this option CustomFieldSerializer's may still be usefull, but there is no need for recompiling the GWT API.
I would prefer option 3. At least until GWT RPC maybe someday will support for overriding the CustomFieldSerializer's
Usefull date/time formatting hints.
Dave Paroulek's answer is the right approach. If you want to see an example of this, we created widgets that work independent of TimeZone and process the values on the server-side where we have all of the TimeZone information we need.
UTCDateBox - Wrapper around the GWT DateBox and always chooses the date at midnight in GMT and represents the value as a Long instead of a Date.
UTCTimeBox - New widget that always chooses a time as millis since midnight, independent of timezone, also represented as a Long.
UTCDateTimeUtils - Server-side code that splits a Date into 2 Long values appropriate for UTCDateBox and UTCTimeBox in a given TimeZone and combines them back into a Date in a given TimeZone.
Here is an example of the date the time controls being used together.
Blog article describing their implementation.
These widgets are available on GitHub.
I'm assuming you are using RPC calls for server-client communication here. Also assuming that you don't care about timezone B, and you know what timezone C is on the server.
You have a few options here:
Calculate the desired date in the server (no Java limits on what you can do there) and send it in a String to be displayed to the client, so you don't have to do anymore transformations on the client.
or:
Calculate the offset between timezone A and C on the server, apply it to all the Date objects you are passing to the client and just display them on the client.
if for some reason none of these were valid for you
Calculate the offset, send it to the client and apply it to any Date you receive from the server by transforming to ms, adding the offset and then creating a Date object again.
see this demo project
GWT timezone demo project
I created a GWT-compatible Java version of the jsTimezoneDetect Javascript library specifically for this purpose. This should provide a (very good guess of) the timezone name purely on the client side. Feel free to try it out and let me know if it works or doesn't work for you.

How to use Java timezone id in a Windows (non-Java) application?

I need to add timezone information to a db table with user maintained locations. The data will be accessed mostly from Java code but there is also some PL/SQL and Win32 (Delphi) code which needs to understand the timezone information.
It seems straight forward to use the id from java.util.TimeZone. Java can easily convert that (obviously), Hibernate has built-in support for it and apparently also Oracle understands those timezone ids:
select TZ_OFFSET('Pacific/Marquesas') from dual.
The problem is: the timezone ids do not seem to be compatible with the Windows Timezone DB. For example, the java.util.timezone id "Pacific/Marquesas" (-09:30) is not in the timezone picklist in Windows. The registry does not contain it at all; see
\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
Here I can only pick either -09:00 or -10:00. So, if I were to store the timezone like this, how can I get to the actual offset/DST infos in Windows (without Java)? Hopefully this does not require a mapping table which I have to keep up to date whenever it changes. Is there a globally accepted standard which works better than the java timezone id?
Update
The timezone info is used in combination with DATE columns on the database. Those columns contain local date/time values. If a location can be associated with those values, the location's timezone enables me to convert the date/time value to UTC or any other timezone whenever needed.
I realize that instead of DATE a TIMESTAMP_TZ data type or something similar would be more appropriate. However, this would require a data migration (for which the TZ is required again) and is not supported by the legacy applications which also work on the data (unless a lot of code is changed). The problem is almost the same if I had to convert the values to UTC.
Bottom line is I need to keep the DATE values in local time but I need to know for some of them which TZ that means.
I can give a little background, if not a real answer.
Many systems use the Olson implementation of timezone data. So those names work in many systems (most Unix, Java, Oracle I think). Microsoft does their own thing.
I see at the bottom of that Wikipedia link there's a reference to some mapping to the Windows world.
Good luck!
I realize this is not the best way to do it, but it might be sufficient in your case. Without knowing all the requirements I can't tell.
What do you need to use the time zone information for? Just to present the time with the correct offset and maybe also the name of the time zone?
You could continue to use Java to determine what the offset of the user is by looking up the user's selected time zone using Java. Each time the user logs in record in your database what the offset currently is. Then other apps can look at this information to determine how to format the time.
This assumes that users who regularly login are the ones that this needs to be done for. If that's not the case you could run a daily job to lookup the time zone for each user in Java and record the offset currently in effect.
Hackish, agreed, but the only other way I see is to maintain a mapping. And what happens when someone selects a time zone that you don't have a mapping for?

Categories