How to manage time properly mysql - java

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.

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.

sqlite datetime format: which one is better?

I'm creating a sqlite database from my c# project which will be used in my android application later on.
I have a CreateDate column in my sqlite database which I will need to get records ordered by this column later in my android application. Now my question is that is it better to save the date time as TEXT with YYYY-MM-DD HH:MM:SS format or I just convert all my dates into miliseconds and order them easily later since it's just a number?
Which one is easier to be ordered later in mobile application? or maybe my approaches are wrong so please feel free to shoot me with some best practices for this situation.
Thanks in advance.
Well unix timestamp looks simple and easier but i wont recommend you this.
Check out this gif.
What happens on January 19, 2038?
On this date the Unix Time Stamp will cease to work due to a 32-bit overflow. Before this moment millions of applications will need to either adopt a new convention for time stamps or be migrated to 64-bit systems which will buy the time stamp a "bit" more time.
Therefore i would suggest you to stick with YYYY-MM-DD HH:MM:SS format as it is both C# and Android compatible(or parseable i should say) and neither would string overflow in near future ;)
Good Luck.

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.

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