The server where I am working in some routes it returns a Date with one extra number and it was causing a weird behavior in our application. Then I isolated the weird behavior to the following code:
import 'package:intl/intl.dart';
void main() {
final format = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
print(format.parse('2020-08-05T14:23:55.974582+00:00'));
print(format.parse('2020-08-05T14:23:55.9745829+00:00'));
}
produces the output:
2020-08-05 14:40:09.582
2020-08-05 17:06:20.829
Why the extra number 9 introduces 3 hours difference? I can not understand why.
I created a repl so you can try: https://repl.it/join/jfhehoqt-leonardosilva25
when I try to parse the same string in swift, it gives a different result than dart, both strings gives me the same date
https://repl.it/join/ncjffdth-leonardosilva25
original swift code from repl for reference:
import Foundation
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ"
print(dateFormatter.date(from: "2020-08-05T14:23:55.974582+00:00")!)
print(dateFormatter.date(from: "2020-08-05T14:23:55.9745829+00:00")!)
Related
aye aye good people,
I'm really confused about the behavior of DateTime.parse();
on dartpad this works
void main() {
const String _iso8601 = '2019-04-01T08:30:00';
final DateTime _date = DateTime.parse(_iso8601);
print(_date.toIso8601String());
}
but in flutter doesn't, but this does
const String _iso8601 = '2019-04-01T08:30:00.000';
final DateTime _date = DateTime.parse(_iso8601);
I'm now in aqueduct and neither of those works including this
String _iso8601 = '2019-04-01T08:30:00Z';
please note that with "didn't work" I don't mean that it returns an error,
but just a null.
[edit: correction
when I mock the string instead of mapping it from the body of a request it returns
Exception has occurred. FormatException (null)
but then again I'm using Iso8601]
If you have some experience with this situation I could use some help.
[edit: note that aqueduct runs on dart 2.0]
Thank you in advance, Francesco
Examples of accepted strings:
"2012-02-27 13:27:00"
"2012-02-27 13:27:00.123456z"
"2012-02-27 13:27:00,123456z"
"20120227 13:27:00"
"20120227T132700"
"20120227"
"+20120227"
"2012-02-27T14Z"
"2012-02-27T14+00:00"
"-123450101 00:00:00 Z": in the year -12345.
"2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"
I wonder is it possible to parse clock time hour:minute:second in Java 8?
e.g.
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
final String str = "12:22:10";
final LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
I tried but get this exception:
Exception in thread "main" java.time.format.DateTimeParseException: Text '12:22:10' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 12:22:10 of type java.time.format.Parsed
LocalTime.parse
Since you only have a time use the LocalTime class. No need to define a formatting pattern in your case.
String str = "12:22:10";
LocalTime time = LocalTime.parse(str);
See that code run live at IdeOne.com.
See Oracle Tutorial for more info.
LocalTime.parse("04:17");
Getting error while parsing string to datetime.
string datestring = "111815";
DateTime date = Convert.ToDateTime(datestring);
I also tried using, Parse, ExactParse with/without culture specificinfo.
I'm still getting the error:
String was not recognized as a valid DateTime.
Please suggest the correct solution.
You just need to specify the right format string when you call ParseExact. In your case, it looks like this is month-day-year, without any separators, and with a 2-digit year (blech). So you'd parse it like this:
using System;
using System.Globalization;
class Test
{
static void Main()
{
DateTime dt = DateTime.ParseExact("111815", "MMddyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt);
}
}
If you're in control of the format at all, I'd strongly recommend yyyy-MM-dd instead of this ambiguous (due to the 2-digit years) and US-centric (due to month/day/year) format.
I developed WCF service that contains so many methods and these methods return json format. My main problem is when i have datacontract member has datetime type i get in json like this /Date(1233846970110-0500)/ which is causing me issue in IOS application. How can i write a global method that converts to MM/dd/yyyy format for very call. I tried to different methods but none works when i test it, always returns the same above format.
I tried in global.ascx like this but like
private void RegisterRoutes()
{
// Create Json.Net formatter serializing DateTime using the ISO 8601 format
var serializerSettings = new JsonSerializerSettings();
serializerSettings.Converters.Add(new IsoDateTimeConverter());
var config = HttpHostConfiguration.Create().Configuration;
config.OperationHandlerFactory.Formatters.Clear();
config.OperationHandlerFactory.Formatters.Insert(0, new JsonNetMediaTypeFormatter(serializerSettings));
var httpServiceFactory = new HttpServiceHostFactory
{
OperationHandlerFactory = config.OperationHandlerFactory,
MessageHandlerFactory = config.MessageHandlerFactory
};
RouteTable.Routes.Add(new ServiceRoute("VWPM_Srv", httpServiceFactory, typeof(IVWPM_Srv)));
}
If you want to send an MM/dd/yyyy formatted string. You just make the property a string and assign the date in the MM/dd/yyyy format.
However you can also make ios accept the dates wfc send you:
json-serialized-date-passed-between-ios-and-wcf-and-vice-versa
In my grails domain I am having a field Date i.e. java.util.Date.
In my controller I am loading this date from params using SimpleDateFormate.
To be precise assume that params.date is something like '20/02/2013 02:30 am'. In the controller I load this as follows:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm a");
domainInstance.date = simpleDateFormat.parse(params.date)
While this statement executes no error is detected. However when the domain Instance is being saved error is generated that
[typeMismatch.Domain.date,typeMismatch.date,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes
[Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "20/02/2013 02:30 am"]
Can you anyone tell me where things are going wrong.
I am pretty sure that SimpleDateFormat parses String to Date. Why is it accepting as String.
Thanks for the response, but I have found the solution to the problem. The problem was something like this.
I was instantiating my domainInstance as domainInstance = new Domain(params)
This was the first statement in the controller action.
When this statement is executed params holds the date in the format "dd/MM/yyyy HH:mm a". Hence this statement adds an error in the domainInstance object.
Later after using SimpleDateFormat the variable is updated but the error still remains in the object because of which the error crops up.
The solution to this error is immediately after the statement 'domainInstance = new Domain(params)' call the statement domainInstance.clearErrors().
This clears up all the errors in the object. Later when the domainInstance is being saved validate is called. In case validate fails due to some other error then the respective error is added at that time.
Rammohan
Grails 2.3.1 is actual problem
def domain = new FooBar(params)
domain.clearErrors()
domain.save(flush:true) // <--- validation will be there
if (doamin.hasErrors()) {
... do something
}
You can try:
domainInstance.date = new Date().parse("dd/MM/yyyy HH:mm a", params.date)