Skip to content

Tag: java

Singleton using AtomicReference

Is it correct implementation of lazy-initializing singleton using AtomicReference? If no – what are the possible issues? Answer No, this is bad: Using an AtomicReference is a nice idea, but it won’t work because Java doesn’t have lazy evaluation. The classic post 1.5 singleton methods are: E…

Regular expression to match unescaped special characters only

I’m trying to come up with a regular expression that can match only characters not preceded by a special escape sequence in a string. For instance, in the string Is ? stranded//? , I want to be able to replace the ? which hasn’t been escaped with another string, so I can have this result : **Is Da…

How to set an expiration date in java

I am trying to write some code to correctly set an expiration date given a certain date. For instance this is what i have. However, say if i the sign up date is on 5/7/2011 the expiration date output i get is on 11/6/2011 which is not exactly half of a year from the given date. Is there an easier

Base64 decoding using JDK6 only

This question with regard to JDK 5 says, there is no implementation provided with JDK 5, but JDK 6 is supposed to have a sun.misc.Base64Decoder. As far as I can tell though, this class is not provided with the JDK and I was not able to find any other similar classes in itSo, what is the situation like with JD…

How do I do calendar arithmetic with java.util.Date?

Currently I have a Date object representing a time. How would I add 5 minutes to this object? Answer You could use Calendar, which will make it easy to add any length of time: For your case you could just add the time in milliseconds like this: