I am working on a project which requires me to create a Date value by setting it to a particular timezone .
I have used the following code
Date formatter1 = new Date(HttpDateParser.parse("2013-08-02 11:00:00"));
System.out.println("Date dd formatter1"+formatter1);
Result as follows:
Fri Aug 02 16:30:00 Asia/Calcutta 2013
After parsing, it is giving me time according to device time zone ...
adding 5:30 automatically if device time zone is set to India Kolkata.
I want result to be as :
Fri Aug 02 11:00:00 Asia/Calcutta 2013
I mean it should not add the offset as reference to GMT .
How could I work upon this code to set the data required to me as per the Timezone and not change it internally ?
One problem is that your original date string:
2013-08-02 11:00:00
does not include time zone information. So, it is being interpreted as a GMT time. Which then means that, displayed in Calcutta time, it will be
Fri Aug 02 16:30:00 Asia/Calcutta 2013
You want to specify that 11:00 is already in Calcutta time. To do that, use one of the formats defined in the HttpDateParser documentation:
// we make sure to specify time zone information "+05:30"!
long timeSinceEpoch = HttpDateParser.parse("2013-08-02T11:00:00+05:30");
Date date = new Date(timeSinceEpoch);
System.out.println("Date: " + date);
// use this to slightly change the date formatting ... same time zone
String pattern = "yyyy-MM-dd hh:mma";
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
String formattedDate = formatter.formatLocal(timeSinceEpoch);
System.out.println("SimpleDateFormat: " + formattedDate);
Note: that in addition to adding "+5:30" to the time string, you have to replace a space after the date with a 'T'.
This code will output:
[0.0] Date: Fri Aug 02 11:00:00 Asia/Calcutta 2013
[0.0] SimpleDateFormat: 2013-08-02 11:00am
if the device's time zone is actually set to Calcutta (Kolkata +5.5).
References
Have a look at this answer on Stack Overflow.
and maybe this one, too.
Related
I have some timestamp strings (e.g. "23/06/2021 13:46") that I know to be in the UK local time (either GMT or BST depending on time of year), but they have no timezone indication as part of the string.
Within rails what is a sensible way to turn them into Time objects in the correct timezone?
I can do it in a really roundabout way:
time_string = "23/06/2021 13:46"
base_time = Time.parse(time_string)
Time.use_zone("Europe/London") do
Time.zone.now.change(year: base_time.year, month: base_time.month, day: base_time.day, hour: base_time.hour, min: base_time.min, sec: base_time.sec)
end
=> Wed, 23 Jun 2021 13:46:00 BST +01:00
But there must be a better way!
I have read loads of different sources and all seem to be about how to convert an existing time object into a different timezone, or cast a Time object to a string.
Thanks!
You can use the in_time_zone method to convert a timestamp to your server's time zone:
time_string = "23/06/2021 13:46"
base_time = Time.parse(time_string)
new_time = base_time.in_time_zone
I ended up making use of a gem called "Time of Day", it will take a string and give you a time of day object, you can then convert that to local time in your current Time.zone on any date you chose:
Time.zone = "Europe/London"
time_string = "23/06/2021 13:46"
parts = time_string.split(" ")
date = Date.parse(parts[0])
tod = Tod::TimeOfDay.parse(parts[1])
tod.on date # => Wed, 23 Jun 2021 13:46:00 BST +01:00
Date difference is zero today. When comparing Nov 04 and Nov 05, rare issue happening only for today.
Code I'm using
let dayDiff = Calendar.current.dateComponents([.day], from:businessDate , to: today).day!
Ipad date and time set to automatically.
Tried restarting iPad.
Tried setting date to tomorrow and I get date diff 1.
Is there any issues because of changes in day light savings yesterday?
If so, what settings I have to update in order to work it as expected?
Note: We can fix it with code changes, but like to know any iPad settings will fix this problem.
In any timezone that had a change in daylight saving time during that period (such as the in USA), the difference between 2018-11-04 00:00:00 UTC and 2018-11-05 00:00:00 UTC is only 23 hours which is less than 1 day so you get a difference of 0 days.
If you create those dates with midnight local time instead of UTC time, you will get the expected result of 1 day difference.
Or if you use a calendar set to the UTC timezone to calculate the difference, you will get 1 day difference.
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'm working with a Grails application and I can't find a clear way to translate a date from one TimeZone to the next/previous TimeZone available (I will define what I consider the next/previous Timezone below).
The facts:
I'm working with date filters in a Grails Application so we can retrieve information based on those filters. The application supports TimeZone so the logged user can set his profile to a particular Timezone.
The dates of the filter are on UTC format and the information on the database is stored also on UTC.
The question:
The user logged has his profile on Timezone UTC+00.
There is a predefined date filter called "This Week", when a user clicks on it generate a period of dates corresponding to the current week in UTC so we can bring all the information on the database from the current week.
If we are on the current day (Thu 19 Nov 2015) the dates the filter will generate for "This Week" to look on the database would be:
Sun Nov 15 00:00:00 UTC 2015 to Sun Nov 22 23:59:59 UTC 2015
This is correct and will bring all the information on the database on that period.
Now, suppose the user doing this has set a Timezone UTC +01.
If we are on the current day (Thu 19 Nov 2015) the dates the filter will generate to look on the database would be the same period:
Sun Nov 15 00:00:00 UTC 2015 to Sun Nov 22 23:59:59 UTC 2015
But in this case I need to translate them to a correct TimeZone because the "This Week" for the current user won't be the same for a user on UTC +01 this is because for this user the date Sun Nov 15 00:00:00 would correspond to the date
Sun Nov 14 23:00:00 on the UTC time (since he/she is one hour ahead) and therefore when filter by "This Week" I should search on the database by the dates:
Sun Nov 14 23:00:00 UTC 2015 to Sun Nov 22 22:59:59 UTC 2015
The thing is I have created a method utcDateToUserTZ(date) to translate from an UTCDate to LocalTimeZone date but this would convert the UTC dates:
Sun Nov 15 00:00:00 UTC 2015 to Sun Nov 22 23:59:59 UTC 2015
into
Sun Nov 15 01:00:00 UTC 2015 to Sun Nov 23 00:59:59 UTC 2015
And that is not what I'm looking for. I need those dates to be translated to the previous TimeZone.
I hope no to be walking around with an issue that has a simpler solution, in which case I hope you can tell me if there exist something easier to solve this.
Thanks,
The method commented before is:
public DateTime utcDateToUserTZ(Date date) {
TimeZone profileTimeZone = getCurrentUserProfile().timeZone
DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(profileTimeZone)
return new DateTime(date, dateTimeZone).withZone(dateTimeZone)
}
My app is working in "Moscow" (+04:00) timezone. But sometimes I need to create time object by only local time (for example "01 may 2012 13:45") and name of ActiveSupport::TimeZone object (for example "Berlin": +02:00 in Summer Time and +01:00 otherwise).
For example if I get "01 may 2012 13:45" and "Berlin" as input I want to yield "2012-05-01 13:45:00 +0200" or "2012-05-01 11:45:00 +0000". I create following function:
def from_local_datetime(local_datetime, time_zone)
offset = Time.now.in_time_zone(time_zone).formatted_offset
datetime = case local_datetime
when String
DateTime.parse(local_datetime)
else
DateTime.new(local_datetime)
end.change(:offset => offset)
return datetime
end
And at the first look it works as I expected. But is it a best practice for this kind of task? May be in some situation It works with errors. I'm not definitely sure.
I would be greatful to any comments.
UPD: I think bug may occur about time when DST changing the time. For example 26 march 2011 was GMT+1 in Berlin time zone and Time.now.in_time_zone("Berlin").formatted_offset returns "GMT+1", but it would be GMT+2 in 27 march 2011. So if I call from_local_datetime("28 march 2011", "Berlin") before 27 march it returns 28 march 2011 00:00:00 +0100, but If I call it after changing the time my function returns 28 march 2011 00:00:00 +0200 :(
Your conversion method is the right approach.
With web sites, you should make sure times are stored as UTC in the database. If you can get the UTC value out of the database, instead of the local time (or maybe you can set your web server's time zone to UTC) it won't have to convert the time from UTC to local time, when you are going to then convert it to the user's timezone anyway.
And, of course, you will have to store the user's time zone preference.
TZInfo::Timezone.get('Europe/London')
Find the time zone
http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html