Unable to parse "CET" from formatted DateString - flutter

Why the DateFormat Parser cant parse CET?
final String dateString = "Sat Jul 05 00:00:00 CET 1975";
final DateTime dateTime =
DateFormat("EEE MMM dd HH:mm:ss vvv yyyy").parse(dateString);
Results in
FormatException: Trying to read from Sat Jul 05 00:00:00 CET 1975 at position 21
Position 21 would be "C" if I count right.
Anybody had this problem before?

intl package didn't implement parsing time zones yet, as evident in date_format_field class :
case 'v':
return formatTimeZoneId(date);
case 'z':
return formatTimeZone(date);
case 'Z':
return formatTimeZoneRFC(date);
And when you look at these functions implementation :
String formatTimeZoneId(DateTime date) {
// TODO(alanknight): implement time zone support
throw new UnimplementedError();
}
String formatTimeZone(DateTime date) {
throw new UnimplementedError();
}
String formatTimeZoneRFC(DateTime date) {
throw new UnimplementedError();
}
This issue has been open since 2015, and this is the response of one of the people responsible for it :
We don't expect to implement those until Dart DateTime's have time zone information, and that's not planned. I wouldn't expect it unless/until JavaScript DateTime's have them. Including localized TimeZone names is a lot of data. We should probably just remove those APIs until such time as we actually provide them.

Related

I get a expiry date and time from the api how compare it with the current time?

I'm getting a expiry time when i call the log in api in for format of "Thu, 30 Dec 2021 10:24:34 GMT" i want to get the current time and compare it.
By using DateFormat class in intl package,
you can parse from string date to DateTime.
And by using 'isBefore' or 'isAfter' method, you can compare two DateTime.
import 'package:intl/intl.dart';
void main() {
String strDate = "Thu, 30 Dec 2021 10:24:34 GMT";
DateFormat format = DateFormat("EEE, dd MMM yyyy hh:mm:ss");
DateTime parsedDate = format.parse(strDate);
print('parsedDate: $parsedDate');
DateTime now = DateTime.now();
print('now: $now');
print('parsedDate is before now: ${parsedDate.isBefore(now)}');
print('parsedDate is after now: ${parsedDate.isAfter(now)}');
}
If I understand your question correctly, this can help.
DateTime now = DateTime.now();
DateTime then = DateTime.parse(expirlyDateFromApi.toDate().toString());
var difference = now.difference(expirlyDate);

GMT time String to DateTime in dart [duplicate]

This question already has answers here:
How do I convert a date/time string to a DateTime object in Dart?
(8 answers)
Closed 1 year ago.
I want to convert string to DateTime in dart. I tried,
String timeVal = "Thu, 25 Mar 2021 19:29:28 GMT";
DateTime.parse(timeVal);
but getting an exception,
FormatException (FormatException: Invalid date format Thu, 25 Mar 2021
19:29:28 GMT)
How could I convert such a string value to DateTime in dart
You can use the intl package:
import 'package:intl/intl.dart';
void main() {
final string = 'Thu, 25 Mar 2021 19:29:28 GMT';
final formatter = DateFormat('EEE, d MMM yyyy HH:mm:ss');
final dateTime = formatter.parse(string);
print(dateTime); // prints : 2021-03-25 19:29:28.000
}
For more info, see this answer and the DartFormat class docs.
Please note, the parsed DateTime in the example does not have the correct time zone (ie dateTime.timeZoneName will return your local time zone).

Adding time to SimpleDateFormat in Groovy

I am trying to add time to groovy parameter which have DateTime stored in SimpleDateFormat.
import groovy.time.TimeCategory
import java.text.SimpleDateFormat
def testCase = messageExchange.modelItem.testCase;
def startdatetime = testCase.testSuite.project.getPropertyValue("StartDateTime").toString();
log.info startdatetime
aaa = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(startdatetime)
use(TimeCategory)
{
def enddatetime = aaa + 5.minutes
log.info enddatetime
}
startdatetime : Wed Nov 08 19:57:50 IST 2017:INFO:2017-11-08T15:00:00.000Z
Error popup displayed with message
'Unparseable date: "2017-11-08T15:00:00.000Z"'
If the date string is Wed Nov 08 19:57:50 IST 2017 and you want to convert it to date object, then you could do:
def dateString = "Wed Nov 08 19:57:50 IST 2017"
def dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
def date = Date.parse(dateFormat, dateString)
Looks you wanted to add 5 minutes to it which can be done as did already
def endDate
use(TimeCategory) { endDate = date + 5.minutes }
log.info "End date : $endDate"
If you want the date object to formatted, then do:
def outputDateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
log.info "Formatted date: ${date.format(outputDateFormat)}"
Another suggestion after looking at your code to get the project property value, use below one-liner.
Change From:
def testCase = messageExchange.modelItem.testCase;
def startdatetime = testCase.testSuite.project.getPropertyValue("StartDateTime").toString();
To:
def startDateTime = context.expand('${#Project#StartDateTime}')
Instead of "yyyy-MM-dd'T'HH:mm:ss'Z'" you probably want "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" since your input string includes milliseconds.
I have got no experience with Groovy, but I assume that since you can use Java classes, you can also use the modern Java date and time API. I much recommend that over the long outdated SimpleDateFormat class. Your format, 2017-11-08T15:00:00.000Z, is in no way tied to SimpleDateFormat, on the contrary, it’s ISO 8601, the format that the modern date and time classes (as opposed to the old ones) “understand” natively without the need for an explicit formatter for parsing.
So I suggest you try (by no means tested):
import java.time.Instant
import java.time.temporal,ChronoUnit
and
aaa = Instant.parse(startdatetime)
and maybe (if you still need or want to use the Java classes)
enddatetime = aaa.plus(5, ChronoUnit.MINUTES)

GWT behaves differently when url has gwt.codesvr

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;
}

Converting Date to other format in C#

Can any one suggest me the following:
i> how to convert 3/12/2010 10:15 to the format Mar 12, 2010 10:15 in c#.
ii> how to remove time part out of it(i.e after the conversion i want Mar 12, 2010
Thanks in advance.
nimesh,
The String.Format function should be able to accommodate your needs (updated to reflect my comment):
string inputString = "03/12/2012";
DateTime dt = DateTime.MinValue;
try {
dt = DateTime.Parse(inputString);
}
catch (Exception ex) {
// handle the exception however you like.
return;
}
string formattedDate = String.Format("{0:MMM d, yyyy}", dt);
To avoid the above, check out TryParse:
http://msdn.microsoft.com/en-us/library/system.datetime.tryparse.aspx
You can format it in lots of ways:
http://www.csharp-examples.net/string-format-datetime/
Use DateTime.Parse(...) to parse the input string into a DateTime object, and then use Format to make the string as posted by mikey.