Browsing 7239 questions and answers with Jon Skeet

Errornous calculation of year/month/day between two dates

What I try to do is calculate the number of years, months and days between two dates. Unfortunately, there is no method of the .NET Framework which can do this. What I did is...
Jon Skeet
people
quotationmark

Unfortunately, there is no method of the .NET Framework which can do this. True, but there is Noda Time instead :) (It's my port of Joda Time to .NET, although there are quite a few differences between the two projects.) LocalDate... more 12/19/2013 10:01:51 AM

people

C# Reflection: GetMethods(..) failing to retrieve anything

I am currently trying to get a very specific set of methods but am failing to do so. I need to get all methods that match a certain signature from all classes that implement a...
Jon Skeet
people
quotationmark

I suspect this is the problem: foreach(System.Type cls in classes) { methods = cls.GetType().GetMethods(...) cls is already a Type, so calling GetType() on it will return System.Type (or a subclass). I suspect you just... more 12/19/2013 8:13:17 AM

people

How to calculate the number of months between two dates in C#

I am doing this datediff = (date1 - DateOfDeposit).TotalDays; But this gives the no. of days and I want no.of months.
Jon Skeet
people
quotationmark

The simplest option is probably to use my Noda Time library, which was designed for exactly this sort of thing (as well as making it cleaner to work with dates and times in general): LocalDate start = new LocalDate(2013, 1, 5); LocalDate... more 12/19/2013 6:50:33 AM

people

Trying to convert Byte[] to bitmap but giving error saying "parameter is not valid" in c#

public class PrintPage { public void buildPdf(string url) { Bitmap bmp = PrintHelpPage(url); Document dc = new Document(); PdfWriter pdfWrt...
Jon Skeet
people
quotationmark

EDIT: Subsequent to your comment: actually I am trying to capture whole page as a picture of a random website to convert as PDF Then you're going about it the wrong way. You'll need to start a browser (e.g. a... more 12/18/2013 5:53:37 PM

people

OutputStreamWriter constructor: how does the encoding work?

I am not a Java developer. I just have to understand what this constructor is doing: x = new OutputStreamWriter(OutputStream output, "UTF-8") x.write(some string) I've...
Jon Skeet
people
quotationmark

Does this mean that the string in x is now encoded into UTF-8? No. It means that when the writer receives a string (which is always effectively in UTF-16) it is encoded into bytes using the specified encoding, and then written to the... more 12/18/2013 3:13:33 PM

people

What does the charset param do in IOUtils.copy()?

I'm not a Java developer but just have to understand what some Java is doing. I have some code IOUtils.copy(InputStream a, Writer b, "ISO-8859-1") or words to that effect The...
Jon Skeet
people
quotationmark

As I understand it this is just saying that a is expected to be in ISO-8859-1. Well, it's expecting the data in the stream that a refers to to be textual data encoded in ISO-8859-1. It is not doing any kind of conversion? Yes it... more 12/18/2013 2:45:50 PM

people

Getting a correct date by adding substracting a number of milliseconds

Today is 18/12/2013. I was testing this program where I wanted get another date by adding/substracting a number of milliseconds from System.currentMillis(). It works well when I...
Jon Skeet
people
quotationmark

This is the problem: ((long)( 48 * 24 * 3600 * 1000)) That's doing all the arithmetic in 32 bits, and then converting the (now truncated, as the result is too large for an int) result to a long. You want: 48L * 24 * 3600 * 1000 where... more 12/18/2013 1:35:52 PM

people

XML DOM Creating Mixed Content

I need to insert an element in the middle of another element's text. Given here is "before" and "after" that I want Before <element1> This is content of element1...
Jon Skeet
people
quotationmark

I suspect you'll need to remove the existing text node(s), and then create three new nodes: The text before <element2> <element2> The text after <element2> Add all of those to <element1> after removing the... more 12/18/2013 10:55:41 AM

people

Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type

Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type. And similarly, Boxed nullable enum can be cast to underlying type but...
Jon Skeet
people
quotationmark

I think this is a subtlety of the unbox and unbox.any IL instructions. From ECMA 335, section III.4.32 (unbox operation - unbox.any is similar) Exceptions: System.InvalidCastException is thrown if obj is not a boxed value type,... more 12/18/2013 10:49:47 AM

people

Why is Func<T> not called Meth<T>?

I just used Action<T>() and it's sibling Func<T>() today and this disturbs my mind now: Func<T> is commented like this in the official docs: Encapsulates a...
Jon Skeet
people
quotationmark

I regard "method" as a sort of implementation detail here - whereas the mathematical concept of a function is common. (How often have you heard of delegates being described as "function pointers"?) Note that the word "function" appears... more 12/18/2013 10:01:27 AM

people