The wrong time zone is used in the Java Web service

I have a Jax - B Java Web service that I use to update the database Each row in the table I am updating is represented by an object similar to the following: –

public class Item {
    private String id;
    private Date startDate;
    private Date endDate;

    public Item() { }

    ...

}

This class is instantiated in a separate program and then passed through soap through a message similar to the following: –

...

<item>
    <id>D001IAAC030</id>
    <startDate>2009-09-17T00:00:00.000+01:00</startDate>
    <endDate>2009-10-01T00:00:00.000+01:00</endDate>
</item>

...

As you can see, due to BST, the offset of UTC time is 01:00 However, when the object is grouped on the server (also on my local computer), it will revert to GMT and deduct 1 hour from the date

Can you tell me what to do: –

>Set my GlassFish server to the correct locale to identify the date as BST. > tell me how to intercept the grouping on the web server so that I can set the time zone myself before setting the date

TIA,

URF

Solution

You just need to remember that the date object (always) stores the date / time as the number of milliseconds since the era in the UTC / GMT time zone What excites people is date The toString () method returns a text representation in the default time zone of the JVM (through the internal calendar object) (look at the JDK source code.)

For example, on my machine

Date Now = new Date();
System.out.println(Now.toString());
System.out.println(Now.getTime())

Will give

Fri Oct 02 06:56:24 EST 2009
1254430584531

Milliseconds is the actual number of milliseconds since the epoch in the GMT / UTC time zone

When you manipulate / use date objects, you should always use the date formatter or calendar instance For example:

Date Now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:MM:ss zzz yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(Now.toString());
System.out.println(sdf.format(Now));

to

Fri Oct 02 06:56:24 EST 2009
Thu Oct 01 20:56:24 UTC 2009

Summary: Date objects are always treated as data only, the number of milliseconds since the era (don't use any deprecated method. Don't use toString (). Unless you understand what it shows.) To display, format, convert (add, subtract time, etc.), date / time is always implemented using calendar instance or dateformat, which is difficult to make mistakes

As Javadoc says, date:

"Before JDK 1.1, the date class had two additional functions It allows you to interpret dates as year, month, day, hour, minute, and second values It also allows you to format and parse date strings Unfortunately, the APIs for these functions are not suitable for internationalization Starting from JDK 1.1, the calendar class should be used to convert between date and time fields, and the dateformat class should be used to format and parse date strings The corresponding method in date is not recommended

Experiment with yourself using date, calendars and formatters and read Javadoc, it will become clearer

For the first part of the problem, you do not need to set the time zone of the GlassFish server to accommodate your data If you want to use data / time values to store time zone data, use calendars instead of dates in the object Or as I usually do, everything is stored as UTC time (in dB and date instances in the object), and the time zone is only used when displaying / outputting or parsing data Therefore, when your data is received, it can be parsed using dateformat or an equivalent time region set to 01:00 (if it has a time zone attached, it can automatically select it from the time string, as shown in the example)

I don't know the second part of your problem, but if your web server implementation handles dates correctly and parses it correctly, it should handle it without your intervention

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
分享
二维码
< <上一篇
下一篇>>