Browsing 7239 questions and answers with Jon Skeet
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...
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
given the following data source:
public struct Strc
{
public decimal A;
public decimal B;
// more stuff
}
public class CLASS
{
public List<Strc> listStrc =...
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
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...
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
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...
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
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...
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
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...
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
We try to parse the following ISO 8601 DateTime String with timezone offset:
final String input = "2022-03-17T23:00:00.000+0000";
OffsetDateTime.parse(input);
LocalDateTime.parse(input, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
Both approaches fail (which makes sense as OffsetDateTime also use the DateTimeFormatter.ISO_OFFSET_DATE_TIME) because of the colon in the timezone offset.
java.time.format.DateTimeParseException: Text '2022-03-17T23:00:00.000+0000' could not be parsed at index 23
But according to Wikipedia there are 4 valid formats for a timezone offset:
<time>Z
<time>±hh:mm
<time>±hhmm
<time>±hh
Other frameworks/languages can parse this string without any issues, e.g. the Javascript Date() or Jacksons ISO8601Utils (they discuss this issue here)
Now we could write our own DateTimeFormatter with a complex RegEx, but in my opinion the java.time library should be able to parse this valid ISO 8601 string by default as it is a valid one.
For now we use Jacksons ISO8601DateFormat, but we would prefer to use the official date.time library to work with. What would be your approach to tackle this issue?
You don't need to write a complex regex - you can build a DateTimeFormatter that will work with that format easily:
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX", Locale.ROOT);
OffsetDateTime...
more
9/29/2017 11:08:05 AM
If my time unit is of type seconds and cycle duration is 20 sec after 86400/20 = 4320 cycles, 24 hours are passed.
long numberOfCyclesToReach24Hours(long cycleDuration, TimeUnit...
It sounds like you're just looking for the convert method:
Converts the given time duration in the given unit to this unit.
return unit.convert(24, TimeUnit.HOURS) / cycleDuration;
more
9/28/2017 9:39:24 PM
I recently encountered an error in my code where I was attempting to "dismantle" an enum flag into an array of its constituent values, but unexpected results would sometimes be...
Basically, you shouldn't use HasFlag for a non flags-based enum... and a flags-based enum should use a separate bit for each flag.
The problem is indeed because of the values. Writing this in binary, you've got:
public enum TestEnum
{
...
more
9/27/2017 2:02:23 PM
We need to cache some information about some objects therefore we are using java.util.WeakHashMap. If we the key is java.util.HashMap we are seeing unexpected...
This has nothing to do with it being a weak map, and everything to do with you modifying a map key, which is basically something you should avoid doing. By adding an entry to the map, you're changing its hash code. This is easily...
more
9/27/2017 9:17:00 AM