24h time after midnight order [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm facing this problem. When I wrap times from websites I'm always getting a 0:00h or 0:30h for example. This is technically at the beginning of the current date. But for humans is at the end of current date.
I need 0:00 to 6:00 get sorted at the end of the day instead of beginning, directly from data-base.
I'm using java, hibernate criteria, mysql datetime.
Thanks all!

You can get your dates and times ordered correctly by using MySql DATETIME data types throughout your application. The Java equivalent class is util.Date.
According to computer and telecommunications industry standards, the first millisecond of each calendar day has the time 00:00.000. So, if you add ten seconds to 23:59:51.000 you get 00:00:01.000 on the next day.
It sounds like your business has a different, non-standard, rule for describing the beginning and ending of each day, to use for this particular display. That's fine. But you need to enumerate this rule very precisely indeed. Midnight matters in many fields of human endeavor!
Let's say your rule is that a day's information runs from [01:00:00 to 01:00:00) the next day.
Then you can select yesterday's records in a MySQL query like this.
WHERE `timestamp` >= CURDATE() - INTERVAL 1 DAY + INTERVAL 1 HOUR
AND `timestamp` < CURDATE() + INTERVAL 1 HOUR
...
ORDER BY `timestamp`
This will display your stuff in the right order. It won't display the time 24:01, but it will place 00:01 after 23:59.

Related

Good practices with java.time - how to represent datetime in my dto/entity/model classes? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
For simplicity let's assume we're talking about a classic web application that has backend written in Java, has a SQL database and communicates through REST.
My question is: What is the best java.time class to represent some point in time (e.g. time when a comment was posted) in a data structure-like class (DTO, JPA Entity, Model)? I've already seen it all: LocalDateTime, ZonedDateTime, Date, Instant. Especially LocalDateTime as a field in JPA Entity just doesn't feel right. What should be used? ZonedDateTime? OffsetDateTime? Instant? "Good" old java.util.Date and convert it into some java.time object only when some date calculations are needed?
An unambiguous, unmoving point in history is always correctly represented as an Instant, which is a point in physical time.
In your scenario, it might particularly make sense for this Instant to appear differently depending on the time zone of the person who asked: for example, if the comment was posted five minutes ago, any user looking at that comment should see a time that is five minutes before their local time.

Handling multiple timezone in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to store multiple user details from different country. How can I store the dates in DB (in UTC format) and how can I show the dates to the users accorning to their time zones.
I am using Java and mySQL.
In MySQL, use the TIMESTAMP data type in your tables.
For each user, store a timezone column, VARCHAR(64) is a good data type for that column. When a user registers to use your system, ask for the time zone value. Mine is America/New_York. Yours might or might not be Asia/Kolkata. For a user interface for this user-preference setting, the WordPress.org software has a good example.
Finally, whenever you establish a connection from your Java program to your DBMS in behalf of a user, issue the SQL command
SET SESSION time_zone='(whatever tz string the user gave you)'
before you handle any user data.
This will cause all times going in to your tables to be converted to UTC, and all times coming out to be translated to local. It works properly for NOW() and CURDATE(). Again, you must use TIMESTAMP and not DATETIME or DATE data types for this.
Make sure your server OS and default MySQL time zones are set to UTC. If you don't do this before you start loading information into your database, it will be almost impossible to fix. If you use a vendor to run MySQL, insist they get this right and fire the vendor if they don't have it right.

Automatically insert records to the database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Good day, guys. I have a web application that is used for monthly inventories. I'm using java and MySQL. I'm able to insert and update a new record to the database. These monthly inventories can be viewed using jsp tables. The records are filtered by classifications and month-year. Every month contains a lot of same records and only differs to the number of inventory items and its price. Because of this, I want to make an automatic insertion of records in the database based from the records of the previous month.
Basically, what I want to do is to automatically fetch the records/items of the previous month and make a new copy to the database with an updated date based from the system date. This will be done every time the system date reaches a new month. Is this possible? If yes, can you suggest solutions on how I can achieve this? If not, can you suggest other alternative solutions? Thank you in advance!
UPDATE
I've made an extra button in my JSP that calls a servlet when clicked. This automatically generates a copy of records of the previous month. I used the INSERT ... SELECT query to do this. Now, I want make it like a scheduled task so that it automatically generates a copy of records without clicking a button. The records will be generated every time the system date changes the month and date. I've read about cron jobs but I don't really know how to apply it in my web application. I'm only using jsps and servlets in windows. Can anyone suggest How I can achieve this? Thank you in advance.
Using a cron job with you php file, assuming all the data will be passed from periodicity you can set your cron job up like so, if your not familiar at all with cron jobs here's a good tutorial to get you going :
01 04 1 1 1 /var/www/somedirectory/somephpfile.php
just change the somedirectory/somephpfile.php to where your file is saved.
there's multiple different Cron codes you can use here
heres a breakdown of the code i wrote above
01 04 1 1 1
this is your timing, starting from the first number:
1.minute (0-59)
2.hour (0-23)
3.day of month (1-31)
4.month (1-12)
5.day of week(0-6) starting from Sunday
Then the last part of the code:
/var/www/somedirectory/somephpfile.php
This is just the location of your file that you want to be executed automatically
if you need any more just ask =)

Is a given date older than a week? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The program that I am developing needs to determine if a given date is older than a week. I have come up with the following code:
((new Date()).getTime() - date.getTime() > 7 * 24 * 60 * 60 * 1000)
Tell me if the code is correct. Help me write correct code if it's incorrect. Also, if you have better ways to code, let me know.
Coming from the same country, I think I understand why she posted this question. The reason is quite complicated and and you may not understand unless you have a working understanding of the culture. Anyway, saying the following code is a better way would suffice.
System.currentTimeMillis() - date.getTime() > 7 * 24 * 60 * 60 * 1000
As stated in the comments, you should have run the code and have a test for it.
Concerning correctness, it is not correct for all cases, since for example it does not respect summer/winter time. So if you have an hour shift in the middle of your time range you get a different result as without. Additionally, Date has no timezone information, so in many cases you experience trouble when comparing dates created by different machines.
That is one of the reasons that you should almost always use APIs for date/time calculations. Since Java 8 is not public yet, you probably have to live with the ugly existing classes or use one of the available open source APIs in this regard - but, don't do it by yourself.

Clock Class - Adjust hour in respect to timezone, with no pre defined classes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For my assignment i have to create a clock class. One feature is to be able to change the timezone,
which i can with a setter method. I am having trouble with the logic i guess. There are 24 timezones and then Greenwich Mean Time. So im storing timezone as an integer, -5, 12 , 0 etc. My question is how would you change the hour accordingly based on the new timezone given.
Would it involve if logic...
I'm programming in Java
So your timezone can be positive or negative.
There is current and new timezone.
Adjust hour correctly.
One more thing, this is not using the standard java classes. I've used them before, just wondering the logic.
You could have the users select their specific time zone; but ideally you'd want them to select their regional time zone. For example, people living on the eastern coast of the USA might say that their time zone right now is Eastern Standard Time, when the vast majority of them are actually using Eastern Daylight Time.
As for adjusting the hour based on their region, I would think it would be as simple as recalling their time zone from however you're storing them, checking if its daylight savings time there or not, and then producing the formatted date that is converted from Greenwich Mean Time into the time zone of the user.
So once you have the time zone offset for a particular person, then you can do the displayTime = UTCtime + timezoneOffset

Categories

Resources