GWT DateTimeFormat reverses timezone value - java

Consider the following code is run in GWT:
import com.google.gwt.i18n.client.DateTimeFormat;
...
DateTimeFormat fullDateTimeFormat = DateTimeFormat.getFullDateTimeFormat();
Log.info(fullDateTimeFormat.format(date, TimeZone.createTimeZone(-120)));
Log.info(fullDateTimeFormat.format(date, TimeZone.createTimeZone(0)));
Log.info(fullDateTimeFormat.format(date, TimeZone.createTimeZone(180)));
And supposing it is Greenwich time 16:00.
Why do I get the following output?
Monday, February 21, 2011 6:00:00 PM Etc/GMT-2
Monday, February 21, 2011 4:00:00 PM Etc/GMT
Monday, February 21, 2011 1:00:00 PM Etc/GMT+3
The expected one is
Monday, February 21, 2011 2:00:00 PM Etc/GMT-2
Monday, February 21, 2011 4:00:00 PM Etc/GMT
Monday, February 21, 2011 7:00:00 PM Etc/GMT+3
What is the right way to fix it?

"Etc/GMT-2" is actually (and very surprisingly) "+02:00", see http://en.wikipedia.org/wiki/Tz_database#Area:
In order to conform with the POSIX style, those zones beginning with "Etc/GMT" have their sign reversed from what most people expect. In this style, zones west of GMT have a positive sign and those east have a negative sign.
Your code leads to a different output on my machine (probably because of my different Locale):
Monday, 2011 February 21 18:00:00 UTC+2
Monday, 2011 February 21 16:00:00 UTC
Monday, 2011 February 21 13:00:00 UTC-3
So, it's not DateTimeFormat which is responsible for the reversing, but TimeZone.createTimeZone(int timeZoneOffsetInMinutes)!
Let's look a bit more into the GWT javadocs of com.google.gwt.i18n.client.TimeZone:
getOffset(Date date):
* Returns the RFC representation of the time zone name for the given date.
* To be consistent with JDK/Javascript API, west of Greenwich will be
* positive.
and composeGMTString(int offset):
* In GMT representation, +/- has reverse sign of time zone offset.
* when offset == 480, it should output GMT-08:00.

I had a look at the source code. It has comments saying
20:00 GMT0000, or 16:00 GMT-0400, or 12:00 GMT-0800
are all the same. As per that I infer the relation between time and timezone as the amount of time subtracted from GMT or added to the GMT. So 16.00 GMT becomes 1400 GMT -0200 or 1900 GMT +0300. Keeping that in mind we have to work the other way around to get the desired result.

Related

How do I get Cron Expression over a specific range of dates?

I want to get valid dates between certain dates via cron expression. Is this possible?
Returns only 1 with getNextValidTimeAfter.
For example, my statement is as follows;
0 0 0? MAY, JUN, JUL MON, TUE 2020-2024
This means:
At 00:00:00 am, on every Monday and Tuesday, in May, June and July, between 2020 and 2024
What I want to do is;
To bring the days in this statement between June 2020 and September 2020.
Months are entered as numbers so it should be something like this: 0 0 0 ? 5-9 * 2020-2024. Look at this resource for more information.

Java - Trouble parsing SimpleDateFormat hours, 00 and 02 works but 01 doesn't

I'm parsing a date, "00:45:00 Mar:2017", "01:45:00 Mar:2017" and "02:45:00 Mar:2017".
All of the others give the correct output, besides "01:45:00 Mar:2017", which always rounds to "02:45:00 Mar:2017".
Code
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:dd MMM:yyyy");
Date date = simpleDateFormat.parse("01:45:26 Mar:2017");
System.out.print(date);
} catch (ParseException e) {
System.out.print(e.getMessage());
}
Output from "00:45:26 Mar:2017"
Sun Mar 26 00:45:00 GMT 2017
Output from "01:45:26 Mar:2017"
Sun Mar 26 02:45:00 BST 2017
Output from "02:45:26 Mar:2017"
Sun Mar 26 02:45:00 BST 2017
Day zero?
Your pattern says the third pair of digits is day-of-month (dd) but there cannot be a day number zero as seen in your examples with 00.
I suspect that third pair of digits is actually seconds rather than day-of-month, and that your day-of-month is missing.
UK DST Cutover
Yes indeed, in the UK the Daylight Saving Time (DST) cutover “Spring forward” is 2017-03-29 at 01:00. Described here:
Mar 26, 2017 - Daylight Saving Time Started
When local standard time was about to reach
Sunday, March 26, 2017, 1:00:00 am clocks were turned forward 1 hour to
Sunday, March 26, 2017, 2:00:00 am local daylight time instead
Formatting patterns are case-sensitive
Another problem: You used lowercase hh where you probably should be using uppercase HH for 24-hour clock.
Avoid legacy date-time classes
Also, you are using troublesome old date-time classes that are now legacy. Supplanted by the java.time classes.
Apparently the British clocks change on the 26th of March, at 1:00.

cron expression for the last weekend of the month

i have a job to be run on the last weekend of the month (i.e) if the month has a sunday at the end it should run on sunday and if the month ends or has a saturday at the end the job should run on saturday.
I didnt find any documentation to build a cron expression for this case.
Thanks in advance.
You can use L in the day of week field
0 0 0 ? * SUNL
This will trigger at every last SUN of every month, e.g.
Sunday, August 31, 2014 12:00 AM
Sunday, September 28, 2014 12:00 AM
Sunday, October 26, 2014 12:00 AM
Sunday, November 30, 2014 12:00 AM
Sunday, December 28, 2014 12:00 AM
I don't see a way to specify the last weekend of a month since quartz does not allow to combine the L character with multiple days of week. So you can't do something like SATL,SUNL. If you want to trigger something on every last SAT and SUN I would define 2 cron expressions.
From the quartz documentation (Special characters)
L ("last") - .....
for example "6L" means "the last friday of the month".
I tested it with my cron expression view - a plugin that I wrote for eclipse
https://github.com/link-intersystems/eclipse-plugins-repository. Maybe it is also useful for you,

Using DateTime to describe a birthday

I'm using the library DateTime to store date values for birthdays.
DateTime dateTime01Abegin = new DateTime(2013, 5, 23, 00, 00);
DateTime dateTime01Bbegin = new DateTime(2012, 5, 22, 00, 00);
Running the method .getDayOfYear() on them, I am getting a value of 143 for both. But one is May 23rd and one is May 22nd - I can't figure why they're returning the same value!
2012 has 366 (february 29) days and 2013 has 365, that's why both dates return 143.
The count of the days in a year has an offset of one day in leap years, since after february 28 leap years have an additional day compared to normal ones.
Not every year is 365 days long, some years are 366 days long.
2012 is a leap year, which means that it has an additional day, February 29th. For dates prior to February 28th, the .getDayOfYear() will return the same values for similar dates for any year. For dates after February 28th, .getDayOfYear() will return the same values for similar dates if both of those dates are in a leap year, or if both of those dates are not in a leap year. Otherwise, they should be off by one.
Leapyears. 2012 is a leapyear, so there was a Feb 29th, pushing all the "later" dates up one slot, so your May 22nd is actually day 143 in both years.
2012 was a leap year. So may 23 2013 came 1 day before may 23 2012.

wrong result in date difference in android

I want to perform a date operation in my android app. What I want is to subtract two dates and get the result. But subtraction leads to the wrong result whenever I change the time zone to central daylight time.
I use the following code to find the difference between the two dates.
Long lDateDiff = dtCycleDay.getTime() - m_dtHistory[0].getTime();
lDateDiff = lDateDiff / (1000 * 60 * 60 * 24);
Here in m_dtHistory[0], the date stored is Thu Mar 01 00:00:00 CST 2012.
And in my dtCycleDay variable the date changes from Thu Mar 01 00:00:00 CST 2012, Thu Mar 02 00:00:00 CST 2012, Thu Mar 03 00:00:00 CST 2012... and so on.
Now up to Thu Mar 11 00:00:00 CST 2012, the subtraction result is fine, but when the date changes to Thu Mar 12 00:00:00 CDT 2012, the CST changes to CDT and it show wrong subtraction result.
Why this happens and these happen only when I change the time zone to Central Daylight Time or pacific Daylight Time.
What do you mean by the "wrong" subtraction result?
My guess is that the result is 23 hours or 25 hours- which is exactly what I'd expect when a daylight transition occurs, as the intervening day is longer or shorter in terms of elapsed time. The "longer" day won't be relevant when dividing by 24, but the shorter one will... you're assuming that every day has 24 hours, and that you can therefore count the number of days by dividing the elapsed milliseconds by "the number of milliseconds in 24 hours". That doesn't work due to varying day lengths.
Don't forget that a Date value is purely an instant in time. It doesn't know about calendars or time zones... if you want to know the difference in "local" dates and times (where midnight to midnight is always 24 hours), I'd suggest using Joda Time instead... Date and Calendar don't really do that for you.
If the real problem you're describing is the time zone changing at the wrong date, that's a different matter entirely, and could be due to various different causes. For one thing, you should show exactly which time zone you're talking about: the abbreviations are ambiguous, whereas the tzdb names (e.g. "Europe/Paris") aren't.

Categories

Resources