Calculate yesterday and tomorrow date in any year in java

Posted By: Java Examples - 10:13 PM

Share

& Comment

Java version of the  this problem is given below :
/**
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package age;

import java.util.Scanner;

/**
 *
 * @author ACHCHUTHAN
 */
public class date {

    public static boolean isleepyear(int y) {
        if (y % 100 == 0) {
            return (y % 400 == 0);
        } else {
            return (y % 4 == 0);
        }
    }

    public static int days_in_month(int m, int y) {
        int d = 0;
        switch (m) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                d = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                d = 30;
                break;
            case 2:
                if (isleepyear(y)) {
                    d = 29;
                } else {
                    d = 28;
                }
                break;
            default:
                d = -1;
        }
        return d;
    }

    public void tomorrow(int d, int m, int y) {
        if ((d == 31) && (m == 12)) {
            System.out.print("Tomorrow is : " + " " + "1" + " /" + "1" + " /" + (y + 1));
        } else if (d == days_in_month(m, y)) {
            System.out.print("tomorrow is : " + "1" + " /" + (m + 1) + " /" + (y));
        } else {
            System.out.print("Tomorrow is : " + (d + 1) + " /" + m + " /" + y);
        }
    }

    public void yesterday(int d, int m, int y) {
        if ((d == 1) && (m == 1)) {
            System.out.print("Yesterday is : " + " " + "31" + " /" + "12" + " /" + (y - 1));
        } else if (d == 1) {
            System.out.print("Yesterday is : " + " " + (days_in_month((m - 1), y)) + " /" + (m - 1) + " /" + y);
        } else {
            System.out.print("Yesterday is : " + " " + (d - 1) + " /" + m + " /" + y);
        }
    }

    public static void main(String[] a) {
        Scanner input = new Scanner(System.in);
        int m, y, n, d;
        System.out.println("Enter Year :");
        y = input.nextInt();
        System.out.println("Enter month :");
        m = input.nextInt();
        System.out.println("Enter day :");
        d = input.nextInt();
        date b = new date();
        b.yesterday(d, m, y);
        System.out.println();
        System.out.println("Today is : " + d + " /" + m + " /" + y);
        b.tomorrow(d, m, y);

    }
}

Output of this program is given bellow :

run:
Enter Year :
2012
Enter month :
2
Enter day :
28
Yesterday is :  27 /2 /2012
Today is : 28 /2 /2012
Tomorrow is : 29 /2 /2012
BUILD SUCCESSFUL (total time: 5 seconds)

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .