Browsing 7239 questions and answers with Jon Skeet

Converting java.util.Date to java.time.LocalDateTime using ZoneId.systemDefault()

I am having trouble converting a java.util.Date into an java.time.LocalDateTime and I am having a really weird effect with regard to the timezone: Date date = new...
Jon Skeet
people
quotationmark

The conversion is correct according to the IANA rules. The Europe/Berlin rule starts with this line: Zone Europe/Berlin 0:53:28 - LMT 1893 Apr You started off with 1869-12-31T23:00:00Z, so the local time is... more 5/27/2016 9:34:15 AM

people

convert portion of byte array values to String

I have loaded the entire file into a byte[] array using following code: byte[] data = Files.readAllBytes(path); Now i would like to get the first 120 characters as String and...
Jon Skeet
people
quotationmark

Sounds like you're just looking for this constructor: String text = new String(data, 0, 120, StandardCharsets.UTF_8); (Always specify the encoding explicitly when converting between binary and text forms.) more 5/27/2016 6:31:49 AM

people

When implementing an interface in Generic, why it is not must implement the methods

When implementing an interface in Generic, why it is not must implement the methods public interface IMyTest<T> { T Add(T i, T j); } public class MyContainer<T>...
Jon Skeet
people
quotationmark

You're not implementing the interface. You're saying that the type argument supplied for the type parameter T must itself implement the interface. That's what the where T part means - it's specifying constraints on T. This means that in... more 5/26/2016 11:22:39 AM

people

C# Constructor overloads: new object.FromOtherObject()?

I have two different types of nodes, I need to be able to convert between them in a relatively easy fashion. I was thinking of doing this in the constructor as it would make code...
Jon Skeet
people
quotationmark

You could just have a constructor with a NodeA parameter: NodeB nodeB = new NodeB(nodeA); ... but having a factory method is also a pretty idiomatic solution. It has various benefits, including being able to have multiple methods with... more 5/26/2016 8:02:54 AM

people

Is it possible to acces a method int inside another method?

First of all I am new to JAVA, so I still dont understand everything and how it works. But I was working on something and was wondering if it could be done like this. import...
Jon Skeet
people
quotationmark

You can't access a local variable from a different method, no. (Once the method has completed, the local variable doesn't exist any more.) However, you can use the return value from your calc method, and pass that to the printText... more 5/25/2016 10:14:15 PM

people

How can I get a MediumDateString?

I can convert a string value in YYYYMMDD format to a human-friendlier string like so: string beginDate = GetElement(3, fileBaseName); string endDate = GetElement(4,...
Jon Skeet
people
quotationmark

No, there's nothing between those two within .NET - if you look at DateTimeFormatInfo you'll see ShortDatePattern and LongDatePattern, but no "medium date pattern". If you know all the cultures you'll need to handle, you could customize... more 5/25/2016 10:12:39 PM

people

How defining private static final field in multiple ways matter

What is the difference between private static final String JDBC_URL = getURL(); and then defining the getURL() like: private static String getURL() { return...
Jon Skeet
people
quotationmark

The second way is a compile-time constant, so elsewhere in your code when you refer to the value, it won't actually refer to the field - it will just have the value baked in. Unless you start messing with the field via reflection or... more 5/25/2016 5:04:55 PM

people

How can I convert a string to char[] without copying the object?

I have a string and I need to traverse it as a char array. Of course the normal method is to use toCharArray() String str = "Hello"; char[] charArr = str.toCharArray(); Now,...
Jon Skeet
people
quotationmark

No, it isn't. Not without reflection, which you should avoid. Messing with the underlying char[] in a string via reflection is a recipe for subtle bugs. You can access individual characters in a string using charAt, but if you really need... more 5/25/2016 4:55:16 PM

people

Getting junk data in messages being sent with TcpClient and TcpListener

I have several other pieces of network code running using identical code and I have never seen this before. The remaining bytes in the receiving buffer are getting filled with...
Jon Skeet
people
quotationmark

You're ignoring the return value of Socket.Receive which tells you how many bytes have been read - which is just like ignoring the return value of Stream.Read. So your buffer contains more bytes than you've actually received, and your... more 5/25/2016 4:33:20 PM

people

c# .net override ToString for multiD array

I have a class that primarily provides a 2d array to its caller (after parsing a file). It's returning this array fine, but I need to provide a way for that caller to convert...
Jon Skeet
people
quotationmark

That's not overriding anything - you're trying to overload the existing ToString method. It's a good job you're not trying to override anything, as you basically can't do that - you can only override members within a class declaration, and... more 5/25/2016 3:07:28 PM

people