How to Assign Static Value to Date in Java?
In this tutorial, we will learn certain ways to assign a static value to a date in java. We will see the limitation of each method that will lead to the development of another method.
Let us first understand the sense of the static keyword.
The static keyword in Java is primarily used for the management of memory. The static keyword in Java is taken into account to share the identical variable or method of a given class. The static keywords can be used along with variables, methods, blocks, and nested classes. The static keyword fits in to the class more than an instance of the class. The static keyword is taken into account for a constant variable or a method that is similar for every instance of a class.
Let us first know the meaning of static value.
A static value is a value that depends on which constructor is to be used. The most elementary one is Date first date= new Date();.
In addition to this basic constructor, other constructors can be used too to provide a long for the time or a string such as – ‘2022-11-26 23:05:00’, ‘2021-10-2103:10:00’.
To be able to use the date, one must take classes in java.time package into its account, otherwise the date is of no use.
There exists a public static util method that can analyse a string and a Date object is returned, but also a Parse Exception is thrown in case the parsed string cannot be transformed into a Date object.
Now, in the following class, a static final Date is being initialized to a value using the util method described already. Since the util method throws Parse Exception, so this is not allowed. The following example throws a parse exception so, it is not allowed in java.
Example 1:
public static final DATE_1 = Util.getDateFromString('30000201');
Example 2:
public static final DATE_2 = Util.getDateFromString('22000104');
Example 3:
public static final DATE_3 = Util.getDateFromString('22000107');
The solution to the above issue is to take the static initializer block into account.
Method 1: static initialize block
public static final Date DATE_1;
static {
try {
DATE_1 = Util.getDateFromString("21000201");
} catch (ParseException e) {
This is the space where the user can do anything such as – throwing an unchecked exception or using a fallback value, it is to be noted there should only be one assignment to the final value
}
}
Method 2: Another way to use an anonymous class, with an instance block
public static final Date DATE_1 = new Date() {{
try {
setTime(Util.getDateFromString("21000201").getTime());
} catch (ParseException e) {
throw new RuntimeException(e);
}
}};
Method 3: Overriding the setter methods
public static final Date DATE_1 = new Date() {{
try {
super.setTime(Util.getDateFromString("21000201").getTime());
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
@Override public void setYr(int yr) { throw new UnsupportedOperationException();}
@Override public void setMnth(int mnt) {throw new UnsupportedOperationException();}
@Override public void setDte(int dte) {throw new UnsupportedOperationException();}
@Override public void setHrs(int hrs) {throw new UnsupportedOperationException();}
@Override public void setMin(int min) {throw new UnsupportedOperationException();}
@Override public void setSec(int sec) {throw new UnsupportedOperationException();}
@Override public void setTme(long tme) {throw new UnsupportedOperationException();}
};
Method 4: The Creation of another method, either locally or in Util, that envelops the ParseException in an unconstrained exception, in the following way:
public static final Date DATE_1 = getDateFromString("21000201");
private static Date getDateFromString(String dateStrg) {
try {
return Util.getDateFromString(dateStrg);
catch(ParseException e) {
throw new IllegalArgumentException(e);
}
}
Summary:
In this tutorial, we understood the meaning of static keywords and static values. We saw certain examples to understand the topic better. Further, we saw four different methods to assign static values to date.