So my goal is to create a class where someone can log in and create an 'event', which really just means they can store a String that is the name of the 'event' (like Woodstock), a future date, and a time that the event starts. I have to store this in a mysql database (I am using LAMP). I've found Joda time, but I have no idea if LocalDateTime can be stored easily in my database (I don't necessarily need time zones as this is a local program but if it needs to have time zones thats fine).
What would also be cool, but I really don't have to implement this but if they could see a calendar for like the next three months or something so they can add the date easily. Anyway, does anyone recommend one format over any others?
Related
I have an instance of Oracle DB running in 2 locations - US and Europe. The java app servers connected to these are also in different locations. Eg, DB in US is connected to java server in India and DB in Europe is connected to java server in Japan. My task is, to pick a date from database from US and send it via Kafka to Europe and vice versa.
The problem is, the time I send from first region will not match with the time in other region. I want that if I send "22-Aug-2022 12:00:00" from US, it should get converted into equivalent time in Europe that is "22-Aug-2022 06:00:00" (depends on DB time only, not app server time).
What I thought of this problem was that I would take the UTC time from one region, send it over and in the destination region, insert the equivalent time. But whatever I have tried is not working due to lack of experience. I am not able to get the correct UTC timestamp in java.
Can someone suggest the best approach for this problem which takes care of converting and fetching the timestamp at DB level.
Edit:-
PS: Please ignore the time conversion logic in the given example. It's just an example and may be it is not factually correct. Please provide suggestions on time conversion in a generic way
The most accepted solution is to:
Store all dates in UTC in database and backend so it is consistent.
Then convert the dates to local date and time zone on frontend to display to user.
as how to convert to UTC maybe this answer can help you: Java: How do you convert a UTC timestamp to local time?
I am trying to figure out how I can pull records from a mongo database using Jongo that will check the date that is on the object in the db and will only return an object with the data from, in my case, the last three most current dates. I'm using LocalDate to set the date on the object.
So that's the problem, what I'm doing is I have an angularjs web ui that interfaces with a java backend and I'm trying to scale down my scope, because the current way I'm displaying data is not feasibly scalable. So I want to limit the response I receive to the last 3 objects created instead of all of the objects. And I'm to the point where I need to do a check on the date but I can't seem to find a reasonable solution. isAfter() and isBefore() was where I started my initial effort, but I can't guarantee the dates in any way so I don't know where to set my constraints.
The code I'm working on doesn't really matter, I'm just more interested in how I can do the check in general.
Thanks in advance for any help, if any at all, can be offered!
In java, I want to know that what is the best practice to keep date info for display, query, report etc. It seems that if we persist as long, all timezone dependency will be removed and we will keep 'persist globally, display locally' principle since Date object automatically converts long to current timezone.
But what is the advantage of persisting as Date object?
Do I loose any info other than info owner's timezone?
Can I get any wrong info when DLS takes into account?
Difference between persisting as UTC date and long is just readable db info?
Depending on your database you should use either TIMESTAMP WITH TIMEZONE or you convert it to UTC time and store it as long.
The first one relies on the DB to handle it correctly (the DB will, but will your DB driver? You have to test this for your setup). The second one makes it a manual process, you will get the correct result in the end but will have more hazzle with it because you have to take care about everything.
Inside Java you might want to use Calendar over Date because there you can specify the TIMEZONE etc. manually, thus you are able to display Dates in timezones different to your own easier.
I'm having some problems with a simple query I'm doing.
I have a postgresql database, with a time wihout zone column.
I cannot change the type of this column. Also, I have to use criterias, so don't tell me to change this. However, if there is a better solution for the future instead of using the time type I'm curious about it.
This column is mapped to a java.util.Date attribute. This could be changed to Time or whatever.
What I'm doing is adding this :
Time someTime = someDate.getTime();
criteria.add(Restrictions.eq(propertyName, someTime)));
This criteria doesn't return anything, when it should. What is done is the following: The user puts some string time, that is parsed to Date, being that 1970-1-1 and the time. This time is then added to the criteria. Also, the time in the database I'm using as example, is the same Date (1970-1-1, etc) that the one that is created when parsing the user data.
I have tried to search for some documentation on how criterias are used against time without timezone but haven't found anything.
Suggestions why this is failing?
UPDATE:The problem has been solved using the library JodaTime. But I still don't understand why was failing...
I've this url to set up the connection in my Italy website, however, when i try to perform some insert action from the site, the date is still not right. (it should be for example: 01:24, but it is 02:24)
jdbc.url=jdbc:mysql://sql.example.com/database?autoReconnect=true&characterEncoding=UTF-8&sessionVariables=time_zone='Europe/Rome'
Do I need to add any other params to make it work correctly?
Is there a complete list of all timezones?
Sorry I don't have the answer to your direct question. However I can suggest something worth considering that will avoid all time zone problems at the database entirely. If possible I recommend simply using BIGINT fields for storing dates with Java. You just store the long of the number of milliseconds since the epoch, e.g. from System.currentTimeMillis() or Date.getTime().
Then interpretation of the time zone for a date is always managed in Java, which is good at using the epoch based number. It does make it a little more involved to directly query the database for a date outside of Java, however it's not too hard and tends to be worth it IMO:
SELECT FROM_UNIXTIME(date_field / 1000) FROM table;
There is a list of "tz" timezone names in Wikipedia.