Browsing 7239 questions and answers with Jon Skeet

Calling an async routine from another async routine

I think this is the correct way to do this sort of thing, but I can't find clear documentation that says so in so many words. I have related routines where RoutineA does some...
Jon Skeet
people
quotationmark

Well, there is a slight difference. Suppose the code that finds the key throws an exception. In your first code, the result would be a faulted task. In your second piece of code, the exception would immediately be thrown to the caller.... more 12/7/2014 8:33:30 PM

people

Get deterministic values from SecureRandom?

For benchmarking purposes, I'd like to have a SecureRandom produce deterministic output. Is this possible through the use of a seed (or maybe specification of an...
Jon Skeet
people
quotationmark

The simplest way of doing this is probably to make most of your code only depend on Random, with an instance being injected (e.g. by passing it into a constructor). That way, for testing purposes you can pass in a simple Random with a... more 12/7/2014 9:40:00 AM

people

Get short days of week in android

How i get days of week (SUNDAY, MONDAY, TUESDAY etc.) as short format(SUN,MON,TUE etc.) for system locale in Android? String mondayshort --> output:"MON"(for English) or...
Jon Skeet
people
quotationmark

To format a specific date (not necessarily the current date), use EEE in a SimpleDateFormat. If you want all the short days of the week, you could use DateFormatSymbols symbols = new DateFormatSymbols(locale); String[] shortDays =... more 12/6/2014 11:09:15 AM

people

convert hexadecimal number to binary

int hex=Integer.parseInt(str.trim(),16); String binary=Integer.toBinaryString(hex); i have a array of hexadecimal numbers as strings and i want to convert those numbers to...
Jon Skeet
people
quotationmark

The problem is that e24dd004 is larger than int can handle in Java. If you use long, it will be fine: String str = "e24dd004"; long hex = Long.parseLong(str.trim(),16); String... more 12/6/2014 10:06:54 AM

people

What guarantees are provided if one thread mutates an object already in one of the concurrent collections?

If thread one puts a mutable object A into a concurrent collection, like a ConcurrentSkipListMap, I understand that this is thread-safe in the sense that if thread 2 'get's the...
Jon Skeet
people
quotationmark

However, if thread one has mutated object A while it is in the collection, does getting it by thread two still provide the same full visibility guarantee? No, definitely not. The concurrency guarantees only apply to the collection... more 12/6/2014 9:51:19 AM

people

Is there a more terse way to accomplish this?

Is there a more terse way to accomplish this? private boolean fieldLockExists(String tableName, String fieldName, Integer id) { List<MpFieldLocks> fieldLocks =...
Jon Skeet
people
quotationmark

If you're using Java 8, you can use streams: private boolean fieldLockExists(String tableName, String fieldName, Integer id) { return getFieldLocks(tableName, id) .stream() .anyMatch(fl ->... more 12/5/2014 4:20:10 PM

people

Retrieving specific data from XML file

Using LINQ to XML. I have an XML file which looks like this: <?xml version="1.0" encoding="utf-8"?> <TileMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...
Jon Skeet
people
quotationmark

You're currently just looking at the child elements of the root element. Instead, if you want to find all descendants, use Descendants. Additionally, there's no point in using a query expression of from x in y select x (or rather,... more 12/5/2014 1:48:14 PM

people

How Convert UTC Date & time to local time using different timezone Nodatime

i am using a function which is taking date time over the internet from external server. here is the function which i am using to get date and time without depend on user pc date...
Jon Skeet
people
quotationmark

You shouldn't be converting the DateTime to a string and back - but your current problem is that you're converting the UTC value you get back from the server into a local DateTime for no obvious reason. Ideally, I'd suggest changing... more 12/5/2014 1:10:07 PM

people

Why SimpleDateFormat HH works differently from hh

final String sourceDate = "05.12.2014 12:17"; final String testDate = "05.12.2014 13:17"; final SimpleDateFormat originalSDF = new SimpleDateFormat("dd.MM.yyyy...
Jon Skeet
people
quotationmark

If you use originalSDF.setLenient(false) then parsing "05.12.2014 13:17" will throw an exception... basically in lenient mode, hh is treated as HH (i.e. as a 24-hour value) when the value is greater than 12 (and possibly when it's... more 12/5/2014 9:57:33 AM

people

Parse Javascript date to C# DateTime, FF data is other then Chrome date

I convert javascript date to C# DateTime. When I use firefox, JavaScript return date to my C# function: string jsDate = "Fri Dec 05 2014 00:00:00 GMT+0100"; so, I parse it to...
Jon Skeet
people
quotationmark

Basically, you shouldn't use the default string representation from the browser. Otherwise you need to know which language it's going to use for month names etc, and you're basically fighting a losing battle. I would strongly recommend... more 12/5/2014 9:34:46 AM

people