We've a public static util method which can parse a string and return a Date object, but it also throws ParseException in case the string parsed cannot be converted to a Date object.
Now, in another class I would like to have a static final Date initialized to a value using the util method described above. But given that the util method throws ParseException, this is not allowed.
This is what I want to do, which is not allowed
public static final MY_DATE = Util.getDateFromString('20000101');
What is the recommended way to keep this Date field 'final'?
Well you could use a static initializer block:
public static final Date MY_DATE;
static {
try {
MY_DATE = Util.getDateFromString("20000101");
} catch (ParseException e) {
// Whatever you want to do here. You might want to throw
// an unchecked exception, or you might want to use some fallback value.
// If you want to use a fallback value, you'd need to use a local
// variable for the result of parsing, to make sure you only have a
// single assignment to the final variable.
}
}
However, I would advise against this. Date is a mutable type - exposing it via a public static final variable is a bad idea.
As of Java 8, the java.time package is the most appropriate to use for almost all date/time work, where you'd write:
public static final LocalDate START_OF_JANUARY_2000 = LocalDate.of(2000, 1, 1);
Prior to Java 8, I'd recommend that you use Joda Time which has many immutable date/time types - and is a thoroughly better library for working with dates and times. It looks like you'd want:
public static final LocalDate START_OF_JANUARY_2000 = new LocalDate(2000, 1, 1);
Note that even if you do decide to go with java.util.Date, it doesn't make much sense to parse the string in my view - you know the values numerically, so why not just supply them that way? If you haven't got a suitable method to construct a Date from a year / month / day (presumably applying an appropriate time zone) then you could easily write one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With