Issue with adding hours to Gregorian Calendar - grails

I have a Grails application I created a Gregorian Calendar GMT date. The time of day was 11:00:00 PM GMT. I added 3 hours to the Gregorian Calendar object and it changed the time of day to 2:00:00 AM but it did not increment the day of the year. I had to check for the case when I add hours to the calender and if that new time should pass to a new day I had to increment the day of the year. This seems like a bug in the GregorianCalendar class but want to make sure I wasn't doing something wrong. Here are the different ways I added hours.
myCalender.add(Calender.HOUR,3)
and
myCalender.add(Calender.HOUR_OF_DAY,3)`
and
myCalender.setTimeInMillis( myCalender.getTimeInMillis() + (( (1000 * 60) * 60) * 3))`
If the begin date and time is for example 6/1/2011 11:00:00 PM GMT and I execute the above code, I would expect the new date and time to be 6/2/2011 02:00:00 AM GMT but what I got was 6/1/2011 02:00:00 AM GMT. Can someone please educate me?

Works for me:
final DateFormat format = SimpleDateFormat.getDateTimeInstance();
format.setTimeZone(DateUtils.UTC_TIME_ZONE);
final GregorianCalendar cal = new GregorianCalendar(DateUtils.UTC_TIME_ZONE);
cal.set(2011, Calendar.JUNE, 1, 23, 30, 0);
System.out.println(format.format(cal.getTime()));
cal.add(Calendar.HOUR_OF_DAY, 1);
System.out.println(format.format(cal.getTime()));
Produces:
2011-06-01 23:30:00
2011-06-02 00:30:00

Related

Wrong date on setting Calendar timeZone to UTC

Allover the app I use Date objects that when I NSLog the value it shows me:
2020-05-24 22:00:00 +0000
Which I think locally means the 25th (- 1 for summer, -1 for timezone). I want to do some Calendar date comparisons:
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = TimeZone(identifier: "UTC")!
// In order to have start on Monday
calendar.firstWeekday = 2
Using this calendar, lets say I want to get the starting date of current week:
extension Date
{
var startOfWeek: Date {
return Calendar.gregorian.date(from: Calendar.gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))!
}
}
If I NSLog:
Date().startOfWeek
It will show me:
2020-05-25 00:00:00 +0000
If I disable the timeZone line on Calendar, it shows me:
2020-05-24 22:00:00 +0000
I always thought the second one is the correct UTC version. Am I wrong? Because I thought all core data dates, all dates are in the 2nd version. In short: If I set Calendar to UTC, my date comparissons are wrong. If I don't they are good. And all this time dates are in UTC.
You are wrong because CoreData dates are not affected by TimeZone. Dates are dates. Think of them as numeric values. When you translate that value to a date and hour then, and only then, the TimeZone is applied.
In your example everything is correct. For a calendar whose TimeZone is UTC, 2020-05-25 00:00:00 +0000 is the beginning of the week. If you use other TimeZone values (for example the default value from Locale) then the your week start at 2020-05-24 22:00:00 +0000. That means that in your TimeZone the hour is 2020-05-25 00:00:00.

Convert US Central Time to different time zones using moment JS

I have a Date Time(Friday, 27 October 2017 4:00:00 AM) in US Central Time zone (CDT). I want to convert this Date Time into different time zones. These are time zones i wanted to convert.
Eastern Time (EDT)
Pacific Time (PDT)
New Delhi, India (IST)
Central Europian Time (CET)
Riyadh, Saudi Arabia (AST)
Pakistan Standard Time (PKT)
Lagos, Nigeria (WAT)
Australian Standard Time (AET)
Greenwich Mean Time (GMT)
Moscow, Russia (MSK)
China Standard Time (CST)
This is how i am doing
var dateTime = moment.tz("2017-10-27 4:00:00 AM", "America/Chicago");
var IST = dateTime.tz('Asia/Calcutta').format('MMMM Do YYYY, h:mm:ss a');
console.log(IST) // October 27th 2017, 9:30:00 am
The returned Date Time is wrong. Because Indian Standard Time is 10 hours and 30 minutes ahead of Central Time.
It should be Friday, 27 October 2017 2:30 PM (IST)
Thanks!
The problem isn't with the conversion to the Indian time zone - it's the original parsing of the Chicago time.
This:
var dateTime = moment.tz("2017-10-27 4:00:00 AM", "America/Chicago");
... is treated as 4am UTC, and then converted to America/Chicago, so it ends up representing 11pm local time (on October 26th) in Chicago. You can see that by just logging the value of dateTime.
If you change the code to:
var dateTime = moment.tz("2017-10-27 04:00:00", "America/Chicago");
... then it's treated as 4am local time on the 27th, which is what I believe you expected. The result of the conversion to Asia/Calcutta is then 2:30pm as you expected.
So either change the format of your input, or specify that format. For example, this works fine too:
var dateTime = moment.tz("2017-10-27 4:00:00 AM", "YYYY-MM-DD h:mm:ss a", "America/Chicago");

I need to take a date and change the time for that date

I am building an iOS app in Swift 2.
I need to take a date and change the time for that date.
How can I do that in the most simple way?
2016-12-10 12:00:00 to 2016-12-10 16:00:00
The time will come from a string but the date from an NSDate.
Simplest way to get 16:00 local time:
let d1 = Date()
let d2 = Calendar.current.date(bySettingHour: 16, minute: 0, second: 0, of: d1)!
Note that if you call print(d2), it will always print in GMT so the hour may not show up as 16:00, but it's 16:00 in your local timezone.

How to subtract date and time out of date time object?

Here is what I am trying to achieve - I have set up a scheduler to execute midnight of every friday, which collects the data from a service for the start date of last friday at 00:00:00 hrs and end time of last thursday at 23:59:59 hrs. Since it has to work every friday, I cannot hard code the dates so I thought of trying out DateTime.
So as per my requirement, if I am running the job on this Friday midnight i.e at "2014-12-12T03:00:00Z", then my start date should be "2014-12-05T00:00:00Z" and my end date should be "2014-12-11T23:59:59Z".
So to get start and end dates, I am trying to subtract days out of my now object. This is what I tried:
now = DateTime.now
p now.new_offset(0).to_s
startDate = now - 7
p startDate.new_offset(0).to_s
endDate = now - 1
p endDate.new_offset(0).to_s
This gives me the right date, but the time is wrong i.e. instead of start date with 00:00:00 and end date with 23:59:59 this would be start date with 03:00:00 and end date with 03:00:00.
How do I modify the DateTime object to get the start date with time at beginning of the day and end date with time at end of the day?
Sorry I am very bad in dealing with dates. Thanks in advance!!
You can use he beginning_of_day and end_of_day methods
1.9.3-p448 :001 > DateTime.now.beginning_of_day
=> Tue, 09 Dec 2014 00:00:00 +0300
1.9.3-p448 :002 > DateTime.now.end_of_day
=> Tue, 09 Dec 2014 23:59:59 +0300
I think what you are trying to do is easier done with the Date class :
require 'date'
start_date = (Date.today - 7).to_time
end_date = Date.today.to_time - 1
Instead of doing this manually, I will suggest a gem called Whenever: https://github.com/javan/whenever
It's a simple DSL for Ruby cron jobs.
Also remember that DateTime has beginning_of_day and end_of_day methods.

Timezone issues with timestamps in the FQL table 'event'

I'm having trouble with event timezones in the start_time and end_time fields in the event FQL table. I want to show the timestamp in a formatted date that accounts for the user timezone. Empirically, it appears the timestamps are in the PST timezone, so I convert them to GMT and add the user timezone, as contained in the timezone field in the user table. Conversion to GMT is performed by Java, with the following code (this should account for daylight saving time automatically):
long millis = 1000 * timestamp;
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
int offset = tz.getOffset(millis);
long timestampGMT = millis + offset;
However, this approach produces different results than the Facebook site.
For example I have an event with start_time=1317416400 and a user with timezone=2. Converting to GMT and adding the user timezone I get September 30th 2011, 16:00:00, but the Facebook site shows September 30th 2011, 14:00:00.
How can I obtain the correct (as in FB site) result?
Examples:
Summer:
http://www.facebook.com/event.php?eid=234094353309432
Created by a user on GMT+2
Time shown on the site: Friday, September 30 · 2:00pm - 7:00pm
start_time (from FQL): 1317416400
If it was UTC, then: Friday, September 30th 2011, 21:00:00 (GMT)
If it was PDT (with DST, so GMT-7), then: Friday, September 30th 2011, 14:00:00 (GMT-7)
Winter:
http://www.facebook.com/event.php?eid=254174591293234
Created by a user on GMT+2
Time shown on the site: Sunday, December 25 · 1:00pm - 4:00pm
start_time (from FQL): 1324846800
If it was UTC, then: Sunday, December 25th 2011, 21:00:00 (GMT)
If it was PST (no DST, so GMT-8), then: Sunday, December 25th 2011, 13:00:00 (GMT-8)
Thanks

Resources