Menu
Learn Coding Online – CodingPanel.com
  • Technologies
    • .NET Core
    • Angular
    • ASP.NET
    • C
    • C#
    • C++
    • Java
    • JavaScript
    • JSON
    • Kotlin
    • Node.js
    • PHP
    • Python
    • React
    • SQL
  • Topics
    • Algorithms
    • Computer Networking
    • Computer Science
    • Design Patterns
    • Software Design
    • Software Engineering
    • Web Development
  • Interview Questions
    • C# Interview Questions
  • Learn
    • My Learning
    • Data Structure
    • Programming
  • Test your Skills
Learn Coding Online – CodingPanel.com

Program to get yesterday’s date in Java

Posted on November 25, 2020April 9, 2021 by CodingPanel Editor

In this Java program, you will learn how to retrieve yesterday’s date, then print it out to the console.

The concept is easy; get today’s date and substrate one day from it.

The output will be formatted as MM/DD/YYYY

Get yesterday’s Date using Java

package cp;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {
  public static void main(String args[]) {

    String yesterdayDate = null;

    //Initiate a calendar instance
    Calendar calendar = Calendar.getInstance();

    //subtract 1 date from current date
    calendar.add(Calendar.DATE, -1);

    //format date
    DateFormat dateFormat = new SimpleDateFormat("MM/dd/YYYY");

    //get formatted date
    yesterdayDate = dateFormat.format(calendar.getTime());

    System.out.println("Yesterday's date is " + yesterdayDate);
  }
}

Output

Yesterday’s date is 11/23/2020

 

Related posts:

  1. Get IP address, computer name and MAC address using Java
  2. An overview of Input/Output operation in Java (IO)
  3. Examples of ArrayList in Java
  4. Remove vowels from a string using Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Blog Authors

avatar for Asha S.Asha S. (4)

IT specialist Veteran

avatar for CodingPanel EditorCodingPanel Editor (40)

Blog Hero

avatar for Feras SFeras S (5)

Software Engineer

©2021 Learn Coding Online – CodingPanel.com | Powered by WordPress & Superb Themes