change mySQL localTime to current time - java

I'm from Egypt and my time is +2 GMT , but my on-line server is on -5 GMT
so when calling some queries fro database to get time for 2 AM ! I got the data from Yesterday not for today
so I'm asking how to fix such problem using mySQL statement or Java code ?
for example when I called :
SELECT DAYOFWEEK(CURDATE())
It getting right value 3 , but if I called it on 3 AM local Time here in Egypt (1 GMT) it get it 2 ! .

The best way to handle time within a database is to always store the "UTC" time (even if you have to convert from local time when storing a timestamp). Then, when you retrieve the time you convert it to the "local time" of the client (browser usually in today's modern world) by applying the locale information.
If you use this process, you eliminate having to know where your server is located (and with hosting providers sometimes in locations you do not know ...).

Related

How to get the "Real" TimeZone of browser with GWT

I need the real timezone ID but NOT of fake / generic timezone like Etc/GMT-xxx.
I understand that there are some solution to get the offset, or even the "fake" timezone, with the commands:
int offset = new Date().getTimezoneOffset();
String timezone = TimeZone.createTimeZone(offset).getID();
And currently our project is using this. However, this will only give you the summarized offset, but you will never know if this offset actually contains the DST (Daylight Saving Time / Summer Time) offset. Or in other words : you know the total offset is 2 hours, but it wont tell if its actually 2 + 0, or 1 + 1, and it really does matter in my use case.
Here is our use case : We have a function that allow user to upload excel file, where the function will parse the excel file and insert the records into system. Each of the record will have different Date Time. Our function have to check the below:
If the browser TimeZone (from where the user upload the file) subject to DST_offset.
If it set subject to DST, we then further decide if the Date is fall under "summer".
Our system then offset the date time and store into DB accordingly.
So, inside a batch excel file, the multiple records there some may have DST Offset while some may not.
I understand that we can use the java Calendar to achieve the above logic 1 & 2, from this link https://www.baeldung.com/java-daylight-savings. But however, the pre-requisite is : we need the real timezone ID e.g. "Europe/Rome", but not the fake or general one "Etc/GMT-xxx".
Please advise how could we get the "real" timezone ID in Smart GWT? or if any alternative to handle the mentioned use case?
I added ZoneId.systemDefault() a few days ago to https://github.com/adrianmsmith/gwt-time which hopefully should do exactly what you want. It returns the zone like “Europe/Rome”, from the browser, if the browser makes this info available (IE does not, for example). Feel free to reach out to me via my email address on my github account if you have any issues.

dates change between client and server side (GWT)

I have a problem that I can't understand. I tried to find the answer a lot of time, but without success.
I work with GWT client side, and Java server side.
Client side, I read dates (java.util.date). And when I send these dates to server side, there sometimes is an hour offset when I receive it.
I know there are many problems with TimeZone. But I think TimeZone aren't responsible of my problem, because not all dates are wrong. To test which dates were wrong, I create a method which create a List of all dates between 1st January of 1900 and today, and which send this list to the server.
When I read the list received in server, here are the results :
All dates are correct from the year 1995 (dates didn't change during sending)
From 1979 to 1995 (approximately): All dates are correct, except 28 days in september / october (from the swith to winter daylight saving time). It's incorrect because of an offset of one hour.
Before : some dates are correct, and some incorrect.
So I tried to add 100 years to my dates client side, send it and remove 100 years server side. And all received dates were correct !
Anybody already had this problem ? And anybody understood this problem ? Any help is welcome.
Thanks !
Edit :
Ok I solved the problem. Read answer of Andrei Volgin to understand the problem. And here is the code which solved it :
// Create date you want
Date date = new Date()
// Get TimeZone of your date
TimeZone timeZone = TimeZone.createTimeZone(date.getTimezoneOffset());
// Adapt your date with the TimeZone
date.setTime(date.getTime() - (timeZone.getOffset(date) * 60000));
// You can send your date to server
// TimeZone server side is "UTC", and all dates received are correct
This is a TimeZone problem.
TimeZone definitions and especially daylight savings rules have changed over the years. If you simply pass the time zone ID or create a time zone using an offset, the browser is unaware of these changes. So the browser simply uses the time-zone offset and current DST setting for this time zone when displaying time. Most of the time this results in a correct time, but not always. This also explains why all dates in the future are correct.
In order to get the accurate conversion, you need to create a TimeZone object using a JSON data string that GWT provides, i.e. use createTimeZone(java.lang.String tzJSON) or createTimeZone(TimeZoneInfo timezoneData).

How to manage time properly mysql

I have already a table with three time fields. One to register the hour when the user start working, when stop working and the difference. Now I want to add the day of the week(Mon-Sun) and the date. How can I properly do that, saying that then I will want to grab the hours that the user worked in the past 7 days, lets say.
I've read that timestamp give all the information but I don't know whether I can separate days from date?
**table name = date_time
date_time_id = auto increment
user_id = var
time_in = time
time_out = time
time_dif = time**
ps: I am using java(servlets) and mysql.
Thanks guys
when I start getting really silly with yime in MySql, I use unix time. MySql has built in functions for unix time conversion, too. FROM_UNIXTIME() is one of them. Here's url of a reference page I use.
I've been burned by MySql time (probably more by my own confusion) a few times, so I prefer unix time and then I can manually figure things out by factoring seconds into minutes *60 and hours *60*60 and days *24*60*60.
Change your column types to TIMESTAMP instead of TIME. A timestamp includes both a date and a time.
You should hav column datetime type So you can easily get days month week whatever required.
Here all functions listed you can use as desired with datetime field:
Date and Time Functions
Use org.joda Datetime java library. Its very easy and you can do all the calculations using timestamp.

How to get country name from time of android mobile?

Hi I am very new to Android Development. I want to pick the country name based on the current time of the particular mobile, while clicking the button. How can i do it? Any body tell me? Thanks in advance.
It seems difficult considering most timezones contain multiple countries (and countries multiple timezones as well).
See: Android: Is there a way to get timezone for Country name?
Per above, the closest you'll get comes from here:
For getting the time zone, check out the "O" and "T" format specifiers
of the date() function. "O" will give you the Difference to Greenwich
time (GMT) in hours (your time zone offset) and "T" will give you the
time zone abbreviation like "EST" for Eastern Standard Time.
e.g. <?php echo date("T"); ?> will give you the executing PHP script timezone, for the user timezome you can pass their date/time as the second argument.
I don't think thats possible because you can have MANY MANY countries on the same time. For example, just take the GMT+/-0 time zone, you'll have England, France, Spain, Portugal, and a few others you can get from the african countries. You can't do it like that.
What i'd suggest is to implement a GEOIP location using for example: MaxMind GEOIP. You have some very powerful tools available in PEAR for that and it took me about 4 hours to setup the library, understand the code and do the code to query. I was querying for IPs and countries and even states/provinces in less than 4 hours. You can't get faster than that unless your a genious :)

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.

Categories