How to add days to a date in Java

To add years, days, hours or minutes to a Date object in Java, you need to use the java.util.Calendar class. //Adds one day to the current date Calendar cal = Calendar.getInstance(); // The current date cal.add(Calendar.DAY_OF_MONTH, 1); Date date = cal.getTime(); // 1 day in the future If you want to substract time, simply use […]

How to create a Date object for a specific date in Java

Creating a new date in Java is quite simple. Use the Calendar class to quickly create your Date object //Generate a date for Jan. 9, 2013, 10:11:12 AM Calendar cal = Calendar.getInstance(); cal.set(2013, Calendar.JANUARY, 9); //Year, month and day of month Date date = cal.getTime(); If you also need to set the time, you can […]