Browsing 7239 questions and answers with Jon Skeet

Day Validation returning an error

Day Validation is always returning the illegal argument exception and never setting the day as inputed especially when trying to input 29 for february in a leap year. i've been...
Jon Skeet
people
quotationmark

You're calling setDay first, before you call setMonth or setYear... which means setDay doesn't know which month and year you're talking about. It can't possibly validate that the day is correct without that information. You could just... more 12/17/2014 3:21:26 PM

people

Converting compressed data in array of bytes to string

Suppose I have an Array[Byte] called cmp. val cmp = Array[Byte](120, -100). Now, new String(cmp) gives x�, and (new String(cmp)).getBytes gives Array(120, -17, -65, -67) which...
Jon Skeet
people
quotationmark

When you've got arbitrary binary data, never ever try to convert it to a string as if it's actually text data which has been encoded into binary data using a normal encoding such as UTF-8. (Even when you do have text data, always specify... more 12/17/2014 3:10:21 PM

people

Java SHA1 output not the same as Linux's sha1sum command

I've tried the following code to produce a SHA1 digest of a String: import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import...
Jon Skeet
people
quotationmark

The problem is how you're using sha1sum under Linux with echo. It's including the line feed. To split it into steps: echo test > somefile sha1sum somefile will show you the same result... but if you look at somefile you'll see it's 5... more 12/17/2014 1:52:26 PM

people

For loop to create Tasks going over end condition

I have a for loop to create a number of Tasks that are perameterised: int count = 16; List<Tuple<ulong, ulong>> brackets = GetBrackets(0L, (ulong)int.MaxValue,...
Jon Skeet
people
quotationmark

(This is a duplicate of similar posts, but they're often quite hard to find and the symptoms often differ slightly.) The problem is that you're capturing the variable i in your loop: for(int i = 0; i < count; i++) { tasks[i] =... more 12/17/2014 1:38:44 PM

people

Is there any other way to set Task.Status to Cancelled

Ok, so I understand how to do Task cancellations using CancellationTokenSource. it appears to me that the Task type "kind of" handles this exception automatically - it sets the...
Jon Skeet
people
quotationmark

You only need to wrap the calling code in a try/catch block where you're asking for the result, or waiting for the task to complete - those are the situations in which the exception is thrown. The code creating the task won't throw that... more 12/17/2014 1:23:28 PM

people

Error in conversion of dates in java

String date = jsonobject.getString("needbydate"); DateFormat df = new SimpleDateFormat("MMM/dd/yyyy"); DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ"); Date...
Jon Skeet
people
quotationmark

Your date formats are using the system default time zone. That's okay for your input, because it specifies the UTC offset explicitly - but for your output, you've just got a date. So it's showing you the date that that point in time... more 12/17/2014 11:22:47 AM

people

What would produce these strange characters in a DateTime to String conversion?

I have some code which records a DateTime in some output files: DateTime dateTime = DateTime::Now; String^ value = String::Format("{0} {1}", dateTime.ToShortDateString(),...
Jon Skeet
people
quotationmark

That's fairly reasonable if the user is in a different locale - you should ask your customer what their system locale is. Use the invariant culture if you want a machine-readable format. For example: String^ value =... more 12/17/2014 10:51:19 AM

people

Parse date with time zone, skip timezone

I try to parse date from string which contains time zone information. Input string is 2014-12-17T08:05:39+00:00. I use DateTime.Parse() method which return me 2014-12-17 09:05:39...
Jon Skeet
people
quotationmark

I would recommend parsing it as a DateTimeOffset instead of as a DateTime. You can then get the DateTime out of that, but it separates the "parsing the data you've been given" step from the "only using the bits I want from that"... more 12/17/2014 9:12:02 AM

people

Read the content of an xml file within a zip package

I am required to read the contents of an .xml file using the Stream (Here the xml file is existing with in the zip package). Here in the below code, I need to get the file path at...
Jon Skeet
people
quotationmark

Your section code snippet is failing because when you reach the end of the first using statement: using (Stream stream = entry.Open()) { metaDataStream = stream; } ... the stream will be disposed.... more 12/16/2014 4:16:16 PM

people

Why does adding double.epsilon to a value result in the same value, perfectly equal?

I have a unit test, testing boundaries: [TestMethod] [ExpectedException(typeof(ArgumentOutOfRangeException))] public void...
Jon Skeet
people
quotationmark

Double.Epsilon is the smallest positive representable value. Just because it's representable on its own does not mean it's the smallest value between any other representable value and the next highest one. Imagine you had a system to... more 12/16/2014 2:13:10 PM

people