Ive been going through many different post about this issue but seem unable to make any proposed solution work. I have added this:
spring.jackson.deserialization.ADJUST_DATES_TO_CONTEXT_TIME_ZONE =false
To my applications.properties file but this doesn't work. I tried implementing a custom deserializer but that doesn't seem to work. Does anyone know how to get spring to read the time fields straight fcrom the db without adjusting the timezone for each value?
EDIT: I have tried a new method and I actually changed the offset being applied to the output records. In my STarterApp class I added this:
#PostConstruct
void started() {
TimeZone.setDefault(null);
//TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
The null only applies an hour to each record where as using UTC applies about 5 hours. Is their a way I can set timezone to have spring not adjust the times returned to include timezone differences? I just want the time that was saved originally without the offset
Related
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
I am having a problem where I am trying to find records with a particular username and where the start date is between a start and end date. I have searched around and below is the seemingly agreed upon answer for how to do this. Unfortunately when I use it I still get the same error:
Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'start' expression specified
The query I am attempting to use is:
Query query = new Query(
Criteria.where("username").is(username)
.andOperator(
Criteria.where("start").lt(DateUtils.ceiling(date)),
Criteria.where("start").gte(DateUtils.floor(date))
)
);
I am using spring-data-mongoldb 1.8.4.RELEASE
So this does in fact work properly. I just spent the last few hours ripping my code apart and rewriting it... Comparing with version in git shows no differences except now it works.
Just wanted to post this answer for anyone who comes looking. I really have no explanation as I was cleaning the project between runs, perhaps some copy of the old class was being held by the app server.
My primary need is to get DAU, MAU, Crash percent, Availability, Rating etc., for any custom time period. (Eg: last 2days, 1week, Date1 - Date2 etc.,) So far I have been using the data from Crash Trends page in dashboard, by setting custom date values and getting the data/values manually.
So, I wanted to automate this, and started implementing the Rest API. The documentation seemed pretty vague, and I only found the endpoint "apps" in the API to be returning something related to what I am looking for (but it only provides very limited details, and no way to set custom dates)
API I request used : https://developers.crittercism.com:443/v1.0/apps?attributes=appName,crashPercent,mau,rating
Am I missing something in the documentations??
Can someone tell me how I can get the details I want from via the Rest API??
Mainly the crash trends details like AVAILABILITY/CRASH PERCENT/DAU/MAU etc., for custom date intervals (not exceeding more than a month). Thanks!
I am a product manager at Crittercism.
The developers.crittercism.com/v1.0/apps endpoint gives you a snapshot of the app data along with some other properties ( link to the apps tore, icon url etc)
For your requirement you should use this endpoint
developers.crittercism.com/v1.0/errorMonitoring/graph
You use this to get retrieve the following metrics
dau
mau
rating
crashes
crashPercen
appLoads
affectedUsers
affectedUserPercent
You can query for two time ranges 1 day (1440 mins) and 1 month (43200 mins)
Here is the documentation for this http://docs.crittercism.com/api/api.html#!/errorMonitoring/graph
Hope this helps.
I got hopelessly stuck on this task. I get other-than-UTC future date input from user > I need to persist it as UTC time. I tried various ways, but it always ends up like this: (method names are irrelevant)
Could please anybody give me the right direction ?
It looks like you're already doing the right thing in the first line. With slight modification:
DateTime instant = getDeadLine(orderBean, localTz);
DateTime.getMillis() will give you the number of milliseconds since the UTC epoch... so that's what you need to persist. If you need to be able to convert back to local time, you'll need to know which time zone to convert back to of course - either using the same one every time, or storing it along with the UTC millis.
One thing to note is that local dates/times aren't always unambiguous - the same local date/time can occur twice due to daylight saving transitions. You'll need to think about whether that will ever be relevant to you.
So, I have an XML file I am using with dbUnit as a data source, and it has some dates in it. The date in the file is like so: "2010-02-04", but once it gets loaded and I access it to print or compare in tests, it thinks the date is "2010-02-03 23:00".
I'm guessing this has to do with EDT/EST, but I'm not sure how I can force dbUnit (or possibly hibernate?) to use the correct timezone.
Does anyone have any experience with dbUnit and dates?
Thanks,
Peter
EDIT
Okay, I'm pretty sure, for some reason, it's reading the date in as EDT, which is correct, then storing it as EST, which is not technically correct (we are EDT here), but is correct per my computer which says it's EST. This conversion is causing the date to "lose" an hour. Not sure why Java thinks we are EST (Windows XP knows we're not), and am doubly unsure why dbUnit thinks the date should be EDT, since it, like Java, should probably be reading the incorrect timezone as EST. This is pretty confusing.
EDIT
I took the Hibernate XML out because that's not the problem. After more poking around, here is what is happening:
1. dbUnit reads the date as a String from the source XML and converts it to a java.sql.Date. By nature, these do not store hours/minutes/seconds, but when you look at the millis, it's clearly adjusted by 4 hours worth in order to show that, though it's midnight here in EDT, it's 4am in UTC. Also, this is backed by a Calendar, which has a timezone that is labeled "America/New York" or something equivalent. I can't exactly remember.
2. Whenever I print this date, my system, which thinks it's EST reconverts 4am UTC to 11pm the previous day, which would be correct, except we're not in EST.
3. Whenever I create a date myself for testing as the example, since my JVM believe we're in EST, it adds five hours worth of millis to make the difference between it and UTC. Clearly, these are different 'dates', and it fails.
So, the real question is, twofold, I guess:
1. Why does my console think it's EST? My Windows XP settings are correct as far as I know. I understand there used to be a problem in Java 1.4 that got the wrong TZ, but I think this has been since fixed and I am running (supposedly) 1.6.
2. Why does dbUnit, which is in the same JVM using the proper fancy TZ.
I suppose I could stop using dates all together, but I don't necessarily have that luxury.
5 years later, I hope I understand your problem correctly because you wrote quite a lot. Nevertheless I had a similar problem.
DbUnit or Hibernate switched my UTC times to my local time even though I had set TimeZone.setDefault(TimeZone.getTimeZone("UTC")); in my Spring application.
When I used -Duser.timezone=EDT, as Andy pointed out, my tests passed. Due to this I assumed that DbUnit fills its database before the actual application is started and therefore the default timezone is set too late.
To avoid this I came up with the two following workarounds for UTC, for ETC (or any other timezone) just switch it accordingly.
Regular application
All your test classes must set the default time zone. To not repeat my self I let all my test extend UtcTimeZoneTest.
public class UtcTimeZoneTest {
static {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
}
public class TestClass extends UtcTimeZoneTest {
// your tests
}
Spring application
In Spring applications you can also do the same thing, but I think the following is a bit nicer. Again all your test classes, must do this. (Although I am not a Spring expert and I hope this has no drawbacks.)
public class UtcSpringJUnit4ClassRunner extends SpringJUnit4ClassRunner {
static {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
public UtcSpringJUnit4ClassRunner(Class<?> clazz) throws InitializationError {
super(clazz);
}
}
#RunWith(UtcSpringJUnit4ClassRunner.class)
public class TestClass {
// your tests
}
How are you executing your tests? Try adding the following VM argument to the command line (or however you're executing your tests):
-Duser.timezone=EDT