In my flutter application I get appointment time in UTC format from a server.
I would like to display it in local time.
Is there a way to convert UTC to local time in flutter?
Dart has inbuilt DateTime type that provides quite a few handy methods to convert between time formats.
void main() {
var utc = DateTime.parse("2020-06-11 17:47:35 Z");
print(utc.toString()); // 2020-06-11 17:47:35.000Z
print(utc.isUtc.toString()); // true
print(utc.toLocal().toString()); //2020-06-11 23:17:35.000
}
Related
I would like to know how a date such as "2022-07-17T01:46:12.632892+05:30" be converted to a Human Readable date in DD/MM/YYYY and hh:mm:ss format? I probably have not surfed through a lot of other questions and suggestions on the Internet but the ones I came across were not of any help. Also, what are such date formats(like the one in question) called?
It is rather straightforward using DateFormat from the package intl which comes with Flutter:
import 'package:intl/intl.dart';
void main() {
final dateTime = DateTime.parse('2022-07-17T01:46:12.632892+05:30').toUtc();
final dateFormat = DateFormat('dd/MM/yyyy HH:mm:ss');
print(dateFormat.format(dateTime)); // 16/07/2022 20:16:12
}
The time has here been converted to UTC to make the example the same for all readers. If not, the created DateTime would be localtime which uses the timezone on the device which the program are running.
If you want to print the time using the timezone offset of 5 hours and 30 minutes, you can do something like this:
import 'package:intl/intl.dart';
void main() {
final dateTime = DateTime.parse('2022-07-17T01:46:12.632892+05:30').toUtc();
final dateFormat = DateFormat('dd/MM/yyyy HH:mm:ss');
print(dateFormat.format(dateTime.add(const Duration(hours: 5, minutes: 30))));
// 17/07/2022 01:46:12
}
I want to merge these two date and time variables and have another variable called var toUTC.
I have two variables for date and time from the Date Picker and Time Picker.
var selectedDate;
var selectedTime;
print(selectedDate); // 2021-02-06 00:00:00.000
print(selectedTime); // 1:15:00.000000
The var selectedDate has the time but I don't want to use this. I want to replace it with the value from var selectedTime.
What I am trying to do is
print(toUTC) // 2021-02-06 1:15:00.00000 <---- merge selectedDate and selectedTime
to convert to UTC time.
I've tried to take only 2021-02-06 and merge with selectedTime, but I failed to convert to UTC.
final toUTC = DateTime(selectedDate.year, selectedDate.month, selectedDate.year,
selectedTime.hour, selectedTime.minute);
Or if its something you are going to need to do regularly you could make an extension for it. The below does it by passing the hour and minute in as separate variables, but you could change that to be another DateTime value if you would prefer.
extension DateTimeExtensions on DateTime {
/// Creates a new date time with the given date but with the time
/// specified from [time]
DateTime withTime([int hour = 0, int minute = 0]) =>
DateTime(this.year, this.month, this.day, hour, minute);
}
To convert to UTC there is the method toUtc() on the DateTime class. I'm not sure if you've just not spotted that or if there is an issue I'm not seeing?
I am using timezone package to get date and time in flutter and the result is here
2020-07-13 12:02:22.952999+0700
is there a way to take only 2020-07-13 12:02:22 without point and +0700
Use intl package
Use DateFormat class like this:
DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now());
if the result is String, parse it to DateTime Like this:
DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.parse('2020-07-13
12:02:22.952999+0700'));
I have a set of dates stored as UTC in my database, when I import them in Salesforce using the tSalesforceOutput:
If I run the import from my machine, they get the wrong timezone
If I run the import from a server which is in UTC, they get the correct timezone.
Is Talend/Salesforce API using the local timezone? How can I prevent this?
Salesforce will allways convert datetime from user timezone to UTC before to store them.
To avoid any problem, the simpler is to fix the user timezone used for the Salesforce connection to GMT and to explicitly convert each datetime to this timezone before to call any tSalesforceOutputXxxx component.
Here is a routine you can use for this purpose:
package routines;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class dateConversion {
public static String convertToGmt(String strDate, String timezone) throws Exception
{
if (strDate == null || timezone == null)
return null;
// Convert strDate from any valid TimeZone such as Europe/Paris to GMT
// strDate is expected to be formatted as "yyyy-MM-ddTHH:mm:ssZ"
SimpleDateFormat indfm = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
indfm.setTimeZone(TimeZone.getTimeZone(timezone));
Date inDate = indfm.parse(strDate);
SimpleDateFormat outdfm = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
outdfm.setTimeZone(TimeZone.getTimeZone("GMT"));
String s = outdfm.format(inDate);
return s;
}
}
Hope this helps.
TRF
I created a simple GWT example using eclipse, I only added a method to GreetingService which is auto-generated.
Date greetServer2() ;
It's implemented like below:
public Date greetServer2(){
// TODO Auto-generated method stub
//
String s = "2014/04/08";
DateFormat inputFormatter = new SimpleDateFormat("yyyy/MM/dd");
Date date=null;
try {
date = inputFormatter.parse(s);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
On the client side I just show the date in a popup:
greetingService.greetServer2(new AsyncCallback<Date>() {
public void onFailure(Throwable caught) {
// Show the RPC error message to the user
...
}
public void onSuccess(Date result) {
Window.alert(result.toString());
}
});
I run it via eclipse, the url generated by eclipse is:
http://127.0.0.1:8888/HelloGWT.html?gwt.codesvr=127.0.0.1:9997
The popup window says "Tue Apr 08 00:00:00 CLST 2014"
But if I access without gwt.codesvr parameter:
http://127.0.0.1:8888/HelloGWT.html
The popup window says "Mon Apr 07 23:00:00 GMT-400 2014"
My GWT is 2.5.1, my JDK is 1.7.0_25.
Any clues?
Thanks in advance.
One result comes from a Java code, and the other one is produced by your browser. The difference is in the time zones. If you want consistent results, you should not use date.toString(), but display date using a DateFormat, and pass a time zone to it.
Remember that your users may be in different time zones, and they will all see a different "time" (and even a different date, like in your example) based on their browser settings, unless you specify a time zone in your code.
UPDATE:
There are different strategies for dealing with time zones. For example, you can save all dates as Long values (date.getTime()) for consistency. Then, you display it using a DateFormat and a time zone.
If you want to make sure that your date starts exactly at midnight in your selected time zone, make an adjustment before saving or using it. This is how I do it:
public static Long toMidnight(Long date, TimeZone timeZone) {
return date - date % (24 * 60 * 60 * 1000) +
timeZone.getOffset(new Date(date)) * 60 * 1000;
}