BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to get the previous & next day date from a given date in Java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

This tutorial is divided into three sections as follows:

1) Calculate the number of days between two dates
2) Get the previous day date and the next day date from the given date
3) Compare two dates with each other

You’re reading part 2 pf above mentioned tutorials: Here we are providing an input date in yyyy-MM-dd format and generating the previous days and next days date from it.

class Demo{
   public static void main(String args[])
   {
      /*This is our input date. We will be generating
       * the previous and next day date of this one
       */
      String fromDate = "2014-01-01";
      //split year, month and days from the date using StringBuffer.
      StringBuffer sBuffer = new StringBuffer(fromDate);
      String year = sBuffer.substring(2,4);
      String mon = sBuffer.substring(5,7);
      String dd = sBuffer.substring(8,10);

      String modifiedFromDate = dd +'/'+mon+'/'+year;
      int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
      /* Use SimpleDateFormat to get date in the format
       * as passed in the constructor. This object can be
       * used to covert date in string format to java.util.Date
       * and vice versa.*/
      java.text.SimpleDateFormat dateFormat =
        	new java.text.SimpleDateFormat("dd/MM/yy");
      java.util.Date dateSelectedFrom = null;
      java.util.Date dateNextDate = null;
      java.util.Date datePreviousDate = null;

      // convert date present in the String to java.util.Date.
      try
      {
	  dateSelectedFrom = dateFormat.parse(modifiedFromDate);
      }
      catch(Exception e)
      {
	  e.printStackTrace();
      }

      //get the next date in String.
      String nextDate =
      dateFormat.format(dateSelectedFrom.getTime() + MILLIS_IN_DAY);

      //get the previous date in String.
      String previousDate =
      dateFormat.format(dateSelectedFrom.getTime() - MILLIS_IN_DAY);

      //get the next date in java.util.Date.
      try
      {
          dateNextDate = dateFormat.parse(nextDate);
          System.out.println("Next day's date: "+dateNextDate);
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }

      //get the previous date in java.util.Date.
      try
      {
	  datePreviousDate = dateFormat.parse(previousDate);
	  System.out.println("Previous day's date: "+datePreviousDate);
      }
      catch(Exception e)
      {
	  e.printStackTrace();
      }
   }
}

Output:

Next day's date: Thu Jan 02 00:00:00 IST 2014
Previous day's date: Tue Dec 31 00:00:00 IST 2013

Top Related Articles:

  1. Convert String to date in Java
  2. Java StringBuilder offsetByCodePoints()
  3. Java StringBuffer class With Examples
  4. Java 8 – Calculate days between two dates
  5. Compare two dates with each other in Java

Tags: Java-Date

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap