Browsing 7239 questions and answers with Jon Skeet

Get first and last day of month using threeten, LocalDate

I have a LocalDate which needs to get the first and last day of the month. How do I do that? eg. 13/2/2014 I need to get 1/2/2014 and 28/2/2014 in LocalDate formats. Using...
Jon Skeet
people
quotationmark

Just use withDayOfMonth, and lengthOfMonth(): LocalDate initial = LocalDate.of(2014, 2, 13); LocalDate start = initial.withDayOfMonth(1); LocalDate end = initial.withDayOfMonth(initial.lengthOfMonth()); more 3/6/2014 12:00:36 PM

people

Integer/ int autoboxing query with generic functions in Java

Okay, so I am trying to write a generic sorting package in Java (I'm taking an algorithms course and I see it as good practice, both in the algorithmic sense and in Java as I'm...
Jon Skeet
people
quotationmark

How do I fix this without using Integer[] arr = new Integer[]{..}? You don't. This simply won't work. I was under the impression that the compiler would box my int to Integer automatically? It will, for individual int to Integer... more 3/6/2014 10:57:08 AM

people

UserManager.CreateAsync(user, password) stuck in infinite loop

I'm trying to make a very simple implementation of an IUserStore that would essentially: use NHibernate save users in a single table (no claims/logins) store role names in the...
Jon Skeet
people
quotationmark

Your approach to async is fundamentally broken at the moment, because you're returning the same task for all operations... and never starting it. I don't see any "infinite loop" here - I just see you blocking on a task which can never... more 3/6/2014 10:25:16 AM

people

Textual order of static variables and initialization order in Java

I only spend five minutes to find a duplicate in SO. My question is simple. Does the following code always work? public class LexicalOrderStatic { private static Integer a1...
Jon Skeet
people
quotationmark

In java can I be sure that a1 is always initialized before a2 ? Yes, because the specification (section 12.4.2) guarantees it (emphasis mine): Next, execute either the class variable initializers and static initializers of the... more 3/6/2014 9:02:34 AM

people

Passing method with list of base type in return type as a parameter causes wrong method signature error

I'm trying to create generic method to filter results from several method returning lists of types that are deriving from same base type. I prepared a simplified version of my...
Jon Skeet
people
quotationmark

Assuming you're using C# 4+ and .NET 4+, you can get this to work using generic covariance: private static List<Animal> GetFilteredAnimals(string f, Func<IEnumerable<Animal>> method) A List<Cat> can't be treated... more 3/6/2014 8:55:30 AM

people

Accessing string representations of constant field values in java

I am using an imported class with has constant field values set using: public static final int BLUE = 1; public static final int GREEN = 2; etc. Is there any way of getting a...
Jon Skeet
people
quotationmark

If you can change the class which contains these constants, it would be better to make it an enum with a value() method. Otherwise, I would suggest building a Map<Integer, String> once using reflection, and then just doing map... more 3/6/2014 8:46:29 AM

people

How to send date and time to SQL using java

I am calling a stored procedure in a database. Two of its parameters requires date and time in sql date format. String x = new SimpleDateFormat("MM-dd-yyyy").format(new Date()) +...
Jon Skeet
people
quotationmark

Why are you formatting a Date to a String and then parsing it again? You shouldn't need a string representation at all. Avoid string conversions as far as you can - you're not really interested in the text representation of the date;... more 3/6/2014 7:01:58 AM

people

Best Method of standard string to XML legal string C#

Currently my understanding of XML legal strings is that all is required is that you convert any instances of: &, ", ', <, with & " ' < > So I made the...
Jon Skeet
people
quotationmark

Firstly, you should absolutely not do this yourself. Use an XML API - that way you can trust that to do the right thing, rather than worrying about covering corner cases etc. You generally shouldn't be trying to come up with an "escaped... more 3/5/2014 8:28:13 PM

people

Java: get InputStream out of an InputStreamReader

Is it possible to get an InputStream out of an InputStreamReader instance? If yes, how? EDIT: i'm reading a binary file over network, so i need to read bytes, not chars. I...
Jon Skeet
people
quotationmark

Not without reflection, no... and I really wouldn't recommend using reflection here. (You would be very implementation-specific.) Usually you shouldn't care about which implementation of Reader is being used at all, so you wouldn't even... more 3/5/2014 6:45:43 PM

people

Byte array not fully consumed

I am writing an FLV parser in Java and have come up against an issue. The program successfully parses and groups together tags into packets and correctly identifies and assigns a...
Jon Skeet
people
quotationmark

Both your use of available() and your call to read are broken. Admittedly I would have somewhat expected this to be okay for a FileInputStream (until you reach the end of the stream, at which point ignoring the return value for read could... more 3/5/2014 5:50:03 PM

people