Browsing 7239 questions and answers with Jon Skeet

Calendar object in Java, returns wrong time?

So I've been working on a simple little method which would return a String as Calendar object. Here's what I have: Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf =...
Jon Skeet
people
quotationmark

My problem, as you may have noticed, is that firstly, the time is wrong. It's added an hour. No it hasn't. Not really. It's displaying the exact same time, but in your local time zone. Unfortunately you can't easily determine the... more 2/10/2014 7:07:54 PM

people

BinarySearch on List<T> seems to be returning strange result

I am very new to C#. I have created a List object and then I am performing BinarySearch on a particular item. But the search results seem to strange. Here is the code : class...
Jon Skeet
people
quotationmark

Your list isn't sorted to start with. Binary search only works when the original input is sorted. The whole point is that you know that if list[x] = y, then list[a] <= y for all a < x and list[a] >= y for all a > x. So either... more 2/10/2014 5:46:50 PM

people

Java: Why does this string of 4 characters create a byte[] with 6 data?

I need to be able to convert an int into a string which represents a series of bytes, and back. To do this, I came up with this code: Int - Byte[] - String new...
Jon Skeet
people
quotationmark

Without knowing the platform default encoding of your machine, it's slightly hard to say - and you should avoid calling String.getBytes without specifying an encoding, IMO. However, basically a String represents a sequence of characters,... more 2/10/2014 4:13:21 PM

people

I cant start a timer 2 times

I'm making a timer in Java and I need help. There is a swing timer added to the main class. I have a jFrame which has 2 panels, 1 having the jLabel, the other one having 3...
Jon Skeet
people
quotationmark

Yes, it's behaving exactly as documented. As per the cancel() documentation: Once a timer has been terminated, its execution thread terminates gracefully, and no more tasks may be scheduled on it. And as per the documentation of the... more 2/10/2014 4:08:12 PM

people

What does Convert.ToSingle do?

I tried to search on Google and Bing this but both return zero results.... What does it mean when you put a hyphen in front of convert? -Convert.ToSingle
Jon Skeet
people
quotationmark

It's just the - operator, applied to the result of calling Convert.ToSingle(...). So for example when used as a unary operator: double x = 10.52123; float y = -Convert.ToSingle(x); is equivalent to: double x = 10.52123; float tmp =... more 2/10/2014 2:06:23 PM

people

Binary search index position

how to get highest and lowest index position in Collections.binarysearch() through key value int curIndex = Collections.binarysearch(myList,"String"); int highIndex = ?
Jon Skeet
people
quotationmark

You're already making an incorrect assumption: that the value returned will be the lowest index. From the documentation: If the list contains multiple elements equal to the specified object, there is no guarantee which one will be... more 2/10/2014 12:17:25 PM

people

csc.exe with arguments does not compile/work from console application

I got main.cs file in C:\kaannos\main.cs. my string filepath: this.filePath = @"c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe"; startInfo.FileName = filePath; ...
Jon Skeet
people
quotationmark

csc will always write the output into the current working directory unless you specify a different output location with the /out flag. So it won't work if you don't have write access to the current working directory, unless you specify... more 2/10/2014 11:21:32 AM

people

Calculating Current Year in 'yyyy MM dd' format sets the incorrect value in Java

Im using the following code to calculate the current year value in yyyy-MM-dd format however the below code assigns paramStartDate value as 0001-01-01 which is incorrect It...
Jon Skeet
people
quotationmark

This is the problem: calendarStart.set(Calendar.YEAR,calendarStart.YEAR); You're setting the value to Calendar.YEAR which is a constant with value 1. If you don't want to change the year number, just remove this line completely. (As... more 2/10/2014 10:51:26 AM

people

questions regarding multi threading

Thread class is said to have a "IS-a" relation as well as "Has-a" relation with the runnable interface. What is the benefit of having both of these? Why do we give priority to...
Jon Skeet
people
quotationmark

What is the benefit of having both of these? It was a poor design choice, IMO. It would have been cleaner to avoid Thread implementing Runnable in the first place. This has led to various bugs (as witnessed by questions on Stack... more 2/10/2014 9:57:56 AM

people

Linq check condition in where clause if field can be null

I have question - how to check condition in where clause even when item has no reference? Most basic way - I am checking field from my Class, which can be null. When I just check...
Jon Skeet
people
quotationmark

Just check for null first, just as you would if you were writing normal C# code in a loop. where p.destinataire != null && p.destinataire.StartsWith("D") If p itself can be null (i.e. your list can contain null elements) then... more 2/10/2014 9:52:46 AM

people