In this tutorial, I will share a Java program to get yesterday’s date with an output. Copy the below and execute it with the help of Java compiler and see the output.
In this program, I have used pre-define classes:
1) java.util.Calendar
2) java.text.DateFormat and
3) java.text.SimpleDateFormat
At the end of this tutorial, I shared the output of this Java program. It will print the date in “dd MMM yyyy” format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.Calendar; import java.text.DateFormat; import java.text.SimpleDateFormat; class ExGetYesterdayDate { public static void main(String args[]) { String yesterdayDate=null; Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); //Set the format date DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy"); //formatted date yesterdayDate=dateFormat.format(cal.getTime()); System.out.println("Yesterday's date = "+ yesterdayDate); } } |
Yesterday’s date = 12 January 2016
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: java date, java date class, java date function, Java program, Java programming, Java programs with examples, Java programs with output