Browsing 7239 questions and answers with Jon Skeet

Calender class and changed TimeZone conflicts

I would like to get the date format as yyyy-mm-ddT00:00:00 and different TimeZone as an output. But here i am getting the default Time Zone in the output to calendar date. Code...
Jon Skeet
people
quotationmark

I would like to get the date format as yyyy-mm-ddT00:00:00 and different TimeZone as an output. Then you need to be using a DateFormat of some description, e.g. SimpleDateFormat. In particular, a DateFormat knows about: The... more 11/18/2014 7:08:31 PM

people

static class self reference

I'm trying to create a basic Foo class, and would like to duplicate the way a Color works, but I'm having trouble getting my head around it. E.g., I'd like to get this...
Jon Skeet
people
quotationmark

For the first part of your question, it sounds like you just want static fields or properties, e.g. public class Foo { // A field... public static readonly Foo MyFunkyFooField = new Foo(...); // A property - which could... more 11/18/2014 12:50:47 PM

people

Simpledateformat didn't check date validation?

I have a REST api which involving date conversions. For that it has inputs like a dateString and its format .But my problem is I can't validate the date for some negative...
Jon Skeet
people
quotationmark

You need to tell the DateFormat to parse strictly, instead of leniently (the default): SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setLenient(false); sdf.parse("29-09-2014 20:35:27"); // Throws ParseException more 11/18/2014 12:42:32 PM

people

Is there a way to tell which exact enum I'm working with at run time?

I have two enums and a generic method. The generic type T could be either one of the enums. public enum myEnumA { a, b } public enum myEnumB ...
Jon Skeet
people
quotationmark

You want: if (typeof(T) == typeof(MyEnumA)) to compare the types. The is operator is for testing whether a value is of a particular type. Note that having to test for particular types within a generic method suggests that it might not... more 11/18/2014 9:58:55 AM

people

How to properly prepare a class for garbage collection?

I'm working on a dueling system for a game that I've been working on, here's the skeleton of the class: public class Duel { private Champion challenger; private Champion...
Jon Skeet
people
quotationmark

how should I properly set the class up to be collected by the Java garbage collection. You may not need to do anything. If there are no references to the Duel from elsewhere, then the Duel object itself is eligible for garbage... more 11/16/2014 8:41:55 AM

people

Array value resets automatically

Ok this silly problem is connected to the post HERE. What I did is basically removed the return and was able to set the values of the xValues array depending on combobox selection...
Jon Skeet
people
quotationmark

waka's answer is right about what's wrong - you're declaring new local variables which are entirely independent of the instance variable. However, waka's fix isn't quite right - you can only initialize arrays in that particular way at the... more 11/15/2014 11:20:30 PM

people

Overloading constructor with a generic type in java

Here is the code: import java.util.ArrayList; import java.util.List; /** * Created by IDEA on 15/11/14. */ public class Matrix<T> { private final int nrow; ...
Jon Skeet
people
quotationmark

The IDE asks me to rename or delete the first constructor, why? It's because of the way generics work in Java. Due to type erasure, you're effectively declaring two constructors like this: Matrix(List data) That's what the JVM... more 11/15/2014 9:49:53 PM

people

Why is this code snippet giving an error?

Set set=new TreeSet(); set.add(2); set.add(1); set.add("3"); System.out.println(set); Set is a Collection and it is not homogeneous so it should take any...
Jon Skeet
people
quotationmark

TreeSet stores its values in order - which means they have to be comparable with each other. You can't compare an Integer with a String, so you get an exception at execution time. If you really want to be able to do this, you could... more 11/15/2014 7:44:29 PM

people

Which one is Safer? Putting An "for" in a "Try" , or putting a "Try" in An "for"?

I have this piece of code, I want to know do they work likewise and only their speed is different or they act totally different? try { for (int i = Start; i < End;...
Jon Skeet
people
quotationmark

Just don't do that to start with - it's an abuse of exceptions, IMO. Write the code so it's safe without try/catch. If you don't know whether HR is long enough, use: int cappedEnd = Math.Min(HR.Length, End); for (int i = Start; i <... more 11/15/2014 10:11:10 AM

people

Class reference and GC cycle in c#

I saw some one wrote code int the following style: Dictionary<string,SomeClass> dict = new Dictionary<string,SomeClass>(); ... dict.Add(key,someClass); ... ...
Jon Skeet
people
quotationmark

Yes, it's redundant. If you're going to remove the key anyway, there's no point in changing the entry to have a null value first. It should make no difference to garbage collection anywhere. more 11/15/2014 10:00:08 AM

people