Browsing 16016 questions and answers with Jon Skeet

DST changes caused an java.text.ParseException: Unparseable date

Following is the code snippet which is throwing an exception: SimpleDateformat dateFormatter = new SimpleDateFormat("yyyyMMddHHmm"); Date date =...
Jon Skeet
people
quotationmark

You're trying to parse a date/time that didn't occur. We now know that this was in the Sydney time zone. At 2am on October 1st 2017 in Sydney, the clocks went forward to 3am. If you were looking at a clock every minute you'd... more 10/5/2017 9:03:07 AM

people

Explicitly marking derived class as implementing interface of base class

interface IBase { string Name { get; } } class Base : IBase { public Base() => this.Name = "Base"; public string Name { get; } } class Derived : Base//, IBase { ...
Jon Skeet
people
quotationmark

It's explained in sections 13.4.4 to 13.4.6 of the C# 5 specification. The relevant sections are quoted below, but basically if you explicitly state that a class implements an interface, that triggers interface mapping again, so the... more 10/3/2017 9:41:51 AM

people

Why regex Matcher.find not showing a word which meets the criteria

The given regex code is import java.util.regex.*; public class Test { public static void main(String[] args) { String longString = " Derek Banas CA 12345 PA...
Jon Skeet
people
quotationmark

Your pattern includes whitespace after the non-whitespace. That accounts for both of your questions: the space between "Derek" and "Banas" counts as part of the first match, which stops it being part of a match for "Banas" (because you'd... more 10/1/2017 8:55:10 PM

people

Adding elements to different collections in a single lambda expression

I possibly use the wrong terms, feel free to correct. I have a test method which takes a Runnable: void expectRollback(Runnable r) { .. } I can call this method like...
Jon Skeet
people
quotationmark

It's subtle, but I think I've got it. In JLS 15.27.3 we have: A lambda expression is congruent with a function type if all of the following are true: ... If the lambda parameters are assumed to have the same types as the... more 10/1/2017 8:29:15 AM

people

Why does short circuit evaluation work when operator precedence says it shouldn't?

In JavaScript and Java, the equals operator (== or ===) has a higher precedence than the OR operator (||). Yet both languages (JS, Java) support short-circuiting in if...
Jon Skeet
people
quotationmark

Or am I confusing matters? You are. I think it's much simpler to think about precedence as grouping than ordering. It affects the order of evaluation, but only because it changes the grouping. I don't know about Javascript for sure,... more 9/30/2017 7:07:14 PM

people

Using LINQ and lambdas to search Dictionary>Class>List>Struct data

given the following data source: public struct Strc { public decimal A; public decimal B; // more stuff } public class CLASS { public List<Strc> listStrc =...
Jon Skeet
people
quotationmark

You're essentially flattening to a sequence of the struct values - and that flattening is represented with SelectMany. So you want: var res = dict.Values .SelectMany(x => x.listSrc) .Where(ls => ls.A > 3) .Select(ls... more 9/30/2017 9:35:56 AM

people

C# Delegate,Action,Func ?? How to write it to make shorter code

I want to know a good idea to Rewrite my class with Lambda + delegate + Func/Action. in my code, there is lot of Lock wrapper ( to save a Sqlite model ). using...
Jon Skeet
people
quotationmark

Well you won't be able to do it quite like that, but you could write: await LockWrapper(() => Work1(modelInstance)); and write LockWrapper as: private async Task LockWrapper(Func<Task> taskProvider) { await... more 9/30/2017 9:04:09 AM

people

Date Zero Validation In Java

I have a method taking Date field as a input parameter. public static String formatDate(Date inputDate) { // if user send the date with inputDate= new Date(00000000) or new...
Jon Skeet
people
quotationmark

It sounds like you just want: if (inputDate == null || inputDate.getTime() == 0L) That will detect if inputDate is null or represents the Unix epoch. As noted in the comments though: Rejecting a single value is kinda dangerous - why... more 9/29/2017 4:39:41 PM

people

How to take care of different timezones in RemoteApp

I have a windows applications which are deployed as remoteapp. I'm trying to generate a timestamp using C# DateTime.Now. I'm interested to know, how the timestamp will be...
Jon Skeet
people
quotationmark

I'm trying to generate a timestamp using C# DateTime.Now. It's strongly recommend against doing that. That will use the time zone of the system where the code is running. It's just not suitable for a timestamp. In... more 9/29/2017 1:57:06 PM

people

How do you check if 2 OffsetDateTime lie within another 2 OffsetDateTIme?

Given an POCO Event{OffsetDateTime Start, OffsetDateTime End} and a POCO Trial {OffsetDateTime Start, OffsetDateTime End} Where trials typical span hours, and events happen over...
Jon Skeet
people
quotationmark

Note: this answer aims at "trial completely contains event" - for "trial overlaps event", see Matt Johnson's answer. OffsetDateTime.ToInstant is unambiguous, so you could certainly just convert to Instant values. You might want to create... more 9/29/2017 1:43:26 PM

people