How to Add Dates in Java
Java date functions let you configure and calculate dates and time functions. You must add the Java date libraries before you can create and add a date to your program functions. You first create a string value that contains the month, day and year. Then, you convert the value to a date variable.
Instructions
-
-
1
Right-click the Java source code file you want to edit. Click "Open With," then click your preferred Java editor.
-
2
Add the Java date utility libraries. Add the following line of code to the top of the file:
import java.text.SimpleDateFormat;
-
-
3
Create the string variable. The following code creates a string that contains the first of the year as the date:
String mydate = "1/1/2011";
-
4
Convert the string variable to a date variable, so you can perform date calculations. The following code completes the addition of the date value:
java.util.Date converteddate = null;
converteddate = formatter.parse(mydate);
-
1