Time zone in Java – webapp

I want to know the best practices for dealing with time zones in web applications

>When user1 adds a new date, it is in a different time zone and the server is UTC. Should I convert the date to UTC and store it in the database? > Display the date in UTC format, get the date, and then convert and display it according to the time zone of the client Is this the right way? > What is DST problem? Will it affect this process? > Is it just a good idea for me to read store dates in milliseconds somewhere? Now I store the date / time. > There are no appropriate methods or libraries, please suggest

My question is

Customers at 5:30 GMT create records and set delivery dates and times. Let's say 5:30 Beijing time on June 30, 2014

Therefore, transporters at GMT - 3:00 can see the exact local time selected by the customer in GMT - 3:00 How do you do this?

Solution

one

Yes Generally, the best practice is to store all date and time values in UTC Your business logic should run in UTC

You may also want to store user entered values or external data sources as audit trails or debugging aids But UTC is used as the official record

Yes, the time zone of the server should be set to UTC (or, if not possible, Reykjavik Iceland) But don't rely on this in programming Instead of relying on default values, specify the desired time zone in your code

two

Yes Convert to localized presentation time Of course, unless users prefer UTC

Think of it as part of localization When internationalizing, you can use the key values in the code Then, during the demonstration, you use the key value to find the localized translation string to be displayed to the user

three

no problem. If "DST" represents daylight saving time, the adjustment of daylight saving time will be processed automatically by using a decent date time library Warning: you need to keep the list of time zone definitions used by the library up to date because the government often changes the rules

If adjusting DST (or time zone) will cause confusion or error messages to your users, you should display UTC in this case

four

may not. In most cases, do not store or work milliseconds Databases and date - time libraries can be executed internally, but you should not

Some nerd types suggest tracking milliseconds But using datetime as milliseconds is like using text as a byte array We use a code base with a higher level of abstraction to deal with all the complexity of text (UTF-8, Unicode normalization of Metaphone symbols, etc.) and add useful methods (search, replacement, etc.) So it is related to date and time

In addition, using milliseconds can cause confusion and make debugging difficult because you can't easily understand their value Date and time work itself is tricky and error prone Using milliseconds does not help

Not all databases and other libraries use milliseconds internally Some use whole seconds, or microseconds, or nanoseconds They didn't use the same epoch

five

In Java, we have two good date and time libraries: joda time and Java time(Java 8).

java. The time package was inspired by joda time, but it was redesigned They have similar concepts, but they are not exactly the same As long as you use import statements carefully, you can use them in your code Both have their own advantages and disadvantages

Avoid using j.u.date / Calendar

Do not use Java bundled with Java util. Date and Calendar class As we all know, they are troublesome and flawed in design and implementation They have been replaced by sun / Oracle with the new Java Time package

Joda time and Java Time contains some convenient methods, which can be found in Java util. Date object for use when other classes require a j.u.date object

Bonus tips

About text formatting:

>Avoid using the string format you used in the question It is difficult to understand and parse. > Learn how to use various string formats defined by ISO 8601 standard for text representation of date and time values. > Do not discard leading zeros in the offset as in your problem This will break the code in the library and violate the standard requirements Always write 05:30, never 5:30 Develop this habit even when writing prose, not just programming code

Sample code

Use the sample code of joda time 2.3

Instantiation date and time, local to 05:30 offset I choose the Calcutta time zone at will Of course you'll replace it with the appropriate one

DateTimeZone timeZoneKolkata = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeKolkata = new DateTime( 2014,DateTimeConstants.JUNE,30,23,timeZoneKolkata );

Use the - 03:00 offset to adjust the same time to another time zone I chose America / Buenos at will_ Aires.

DateTimeZone timeZoneBuenos_Aires = DateTimeZone.forID( "America/Buenos_Aires" );
DateTime dateTimeBuenos_Aires = dateTimeKolkata.withZone( timeZoneBuenos_Aires );

Convert to UTC

DateTime dateTimeUtc = dateTimeKolkata.withZone( DateTimeZone.UTC );
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>