Java gregorian calendar and removing time and timezone - java

I have a gregorian calendar that serializes to the string below during a soap request
2079-07-07T00:00:00.000-07:00
The .NET webservice reads it as the string below
07/07/2079 01:00:00
Is the 07:00 causing the issue? If so how can I get rid of this?

Someone might be able to give you an affirmative answer if you tell us which time zone is configured on your system, but I would assume a mismatch in the DST rules.
The JVM comes with its own timezone and DST rule database, while Windows (and .NET) uses a different database. In theory, the two databases should of course contain the same rules, but I have ran into differences in the DST rules for historical dates. I would assume there might be differences for dates far in the future as well.
If you actually want to transport a date value (no time component) over the SOAP service, the easiest solution would be to use the appropriate XML Schema datatype instead of a datetime type.

Use a DateTimeOffset in your .net code and it will work just fine.

Related

Spring Boot 2 get client timezone [duplicate]

How to get client/request timezone in jsp?
Unfortunately this information is not passed in HTTP headers.
Usually you need cooperating JavaScript to fetch it for you.
Web is full of examples, here is one http://www.coderanch.com/t/486127/JSP/java/Query-timezone
you cannot get timezone, but you can get current time from client side.i.e. through javascript and than post back. On server side, you can convert that time to GMT/UTC. The UTC shows the TimeZone.
If you just need the local timezone in order to display local times to the user, I recommend representing all times in your service in UTC and rendering them in browsers as local times using Moment.js.
My general rule is to handle and store times in UTC everywhere except at the interface with the user, where you convert to/from local time. The advantage of UTC is that you never have to worry about daylight-saving adjustments.
Note that if you want to show the age of something (e.g. "posted 3 hours ago") you just need to compare the UTC timestamp with the current UTC time; no need to convert to local times at all.
Best solution for me is sending date/time as a string, and then parse with server's timezone to get a timestamp. Timestamps are always UTC (or supposed to be) so you will not need client's TimeZone.
For example, sending "10/07/2018 12:45" can be parsed like:
SimpleDateFormat oD = new SimpleDateFormat();
oD.applyPattern("dd/MM/yyyy HH:mm");
oD.setTimeZone(TimeZone.getDefault()); // ;)
Date oDate = oD.parse(request.getParameter("time"));
Obviously you can set your specific date/time format.

Java(Spring) automatically correcting date by timezone bug

so I have a task to fix a bug. So basically I make a GET request for /report-information and get in response two fields: Date dateFrom, Date dateTill.
The correct date that it should return should be: 2019-07-01 00:00:00 and 2019-07-31 23:59:59 - that's the data from a database. Basically, I make a GET request for these fields, Java takes it from database and sends it back to me.
But the problem is that somehow the values that are returned after making a GET request are: 2019-06-30 21:00:00 and 2019-07-31 20:59:59. Basically -3 hours because of some automatic timezone correction.
What I need is to create a method or something similar that would make java ignore the timezone and wouldn't change the date. I should use Date variables not to mess up a lot of other code that uses these fields.
Also I should mention that when I debugged the whole process, somehow in the #RequestMapping method it returns these fields correctly, as they should be, but when I make a request through my browser or Postman, I get the corrected by timezone version of the date. But it is possible that I just missed something.
Do you have any suggestions or ideas why and at what moment does Java change the date automatically? And what should I do to prevent it from correcting the date.
Thank you!
Since you have mentioned that in your Controller code you can see the correct date, you may want to check the Jackson serializer ( assuming this is the one serializing objects to json ) settings used for serializing the date object into JSON. You can check the multiple options for configuration here.
i also face this problem some day ago....
in my case it's arise from Jackson lib (i project is in spring boot)
to fix this problem i set my time zone for Jackson lib in application.property file
spring.jackson.time-zone:Asia/Dhaka
in my case i am in Dhaka Bangladesh
also try by this
for more details

JSF Date Pattern issues [duplicate]

A date (as the Date temporal type) is stored in my DB like this: 31.10.2012
When I display it in the UI, it is rendered like this per default: 2012-10-31
I convert it using <f:convertDateTime pattern="dd.MM.yyyy" /> and unexpectedly it turns to
30.10.2012
The SimpleDateconverter, fed with the same date and the same format string returns 31.10.2012, as expected.
What am I missing?
Thank you
EDIT : for dates stored as Timestamp the same conversion yields correct results, so I suppose it has something to do with the Date interpreted as the exact midnight which in turn might be interpreted to belong to 2 different days. But I still have no clue where to define the behaviour and what would be the best workaround.
This is undoubtedly a timezone-related issue.
JSF defaults to GMT (UTC) in date/time conversion. So if your server platform default timezone is GMT+X (not GMT-X), then the time will go back in the past X-amount of hours. If the time is already 00:00:00 (midnight), then the date will even go back one day in the past.
There are 2 standard ways to achieve your functional requirement anyway:
Tell JSF to use the server platform default timezone instead for all date/time conversion by adding the following context parameter to web.xml:
<context-param>
<param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
<param-value>true</param-value>
</context-param>
Alter every <f:convertDateTime> to explicitly specify the webapp-specific timezone. As you're based in Germany and the date format pattern also confirms this, I'll assume CET.
<f:convertDateTime ... timeZone="CET" />
In any case, using a non-universal or even mixed timezone throughout the application is not recommendable. It's recommend to set the timezone in all layers and environments to UTC. Not only in the server and front end tier and presentation layer, but also in the SQL database and back end tier and persistence layer. This way the code is not sensitive to timezone and DST(!) related matters and you can just focus on altering the timezone during presentation only, if necessary.
See also:
Daylight saving time and time zone best practices

Timezone codes conversion between SQL server and Java

I have a table in SQL server which stores the TimeZone codes as the format listed here: TimeZone Microsoft. I wonder if there is a way to get this TimeZone code correctly in Java using the method TimeZone.getTimeZone(""). As the TimeZone codes in Java is not the same as it is in SQL (Available TimeZones in Java).
Or is there a way to return the time offset in SQL server by the TimeZone code? Thanks
There's mapping information between Windows time zone IDs and IANA IDs in CLDR - it's reasonably easy to read the XML, and you may well find there's a Java library around to parse this for you.
However, it's worth being aware that the time zone mappings aren't perfect: the Windows time zone data is basically not the same source as IANA, and some zones will map to different offsets for some periods. They're updated on different schedules, too.

Weird mysql beahviours with timezones? How to control them?

I wrote a webapp using spring+hibernate. I developed everything on windows and then I moved it to a Linux virtual server (Aruba, an Italian provider). I noticed an annoying thing: when dates where saved on windows the time would be the same of my "wall clock", so if I read 13:45 I will have the same hour in the mysql row. This doesn't happen on Linux anyway. In fact the linux machine is on CEST as well (my timezone), I got it typing "date" in the shell. But I get the dates saved in the DB with an offset that is relative to GMT. Again, my app always displays everything in GMT (Including GMT as a time zone if I choose to format the dates to display the time zone) and mysql saves everything in that format. How do I control all this?
I post the solution by myself, because I think it's worth having it in this site.
First of all: mySql doesn't store any timezone information. So say that you are running on GMT+4 and you write a couple of records that contain date fields. Then you move your system in GMT-2 you read those records (perhaps importing the data from mysqldump). If your system and VM have GMT-2 as timezone the dates you read will be taken as if they were written in GMT-2 and NOT ADJUSTED.
Solution: Take control of your VM timezone by using -Duser.timezone="GMT" command line option (you can even put this in your Tomcat startup script) or your preferred timezone (but GMT is better, let me explain why). This way you'll know for sure which timezone your VM is running. This doesn't mean that Java VM will assume that your system time is the one you specified in your user.timezone, it will know the system timezone and adjust dates accordingly. In fact if you are not in GMT, you will see dates in adjusted to GMT and saved to DB accordingly. This way you'll be sure that you are using that as a reference.
The problem is that if you take a date object and you do myDateObject.toString(), you'll get the date adjusted to GMT, with the hour offset. Which is not what you'll probably want.
The solution is to use SimpleDateFormat and do somthing like this when you have to output a date:
SimpleDateFormat dateFormat = new SimpleDateFormat(
"HH:mm dd/MM/yyyy z", Locale.ITALY);
dateFormat.setTimeZone(TimeZone.getTimeZone("Europe/Rome"));
Everything will get converted the right way. You can even go further if you are developing a web app. You can extract the timezone from the HttpRequest and adjust date output accordingly, but I didn't go so far as I'm writing an application that is intended for Italian users only :D (yay).
Hope this will help.

Categories

Resources