Updating Date inside a while loop, Java

Snibb :

Im trying to compare the real world date with a user input date within a while loop. Although the initial execute is correct, the second time its executing the date stays the same. Ive tried asking for the date inside the while loop and now most recently from within a class method but still the date stays the same.

How can I retrieve an up to date date?

import java.util.Date;

import java.util.Scanner;

class Watch {
    Date CurrentTimeAndDate = new Date();

    int CurrentMinutes() {
        int currentMinutes = CurrentTimeAndDate.getMinutes();
        System.out.println(currentMinutes);
        return currentMinutes;
    }
}

public class App {

    public static void main(String[] args) throws InterruptedException {

        Scanner input = new Scanner(System.in);
        String timer = null;
        int i = 0;
        int num = 0;
        Date TimeAndDate = new Date();
        int getDay = TimeAndDate.getDay();
        int getMonth = TimeAndDate.getMonth() + 1;
        int getYear = TimeAndDate.getYear() + 1900;
        int getMinutes = TimeAndDate.getMinutes();
        Watch watch1 = new Watch();

        String[] Month = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
        String[] Day = { "", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
        System.out.println("Current time and date is    " + TimeAndDate);
        System.out.println("Printing my way! " + Day[getDay] + " " + Month[getMonth] + " " + getYear + " " + getMinutes);
        System.out.println(" Enter a short description of what you want reminding about ");
        String rem = input.nextLine();
        System.out.println(" Enter date of reminder 1-7");
        while (i < 7) {
            System.out.println(i + 1 + " = " + Day[i + 1]);
            i++;
        }
        int day = input.nextInt();

        System.out.println("Enter Month of reminder");
        i = 0;
        while (i < 12) {
            System.out.println(i + 1 + " " + "=" + " " + Month[i + 1]);
            i++;
        }
        int month = input.nextInt();
        System.out.println("Enter year");
        int year = input.nextInt();
        System.out.println("Enter Minutes, for testing purposes");
        int userInputMinutes = input.nextInt();
        System.out.println("Date set to remind you about " + rem + " " + Day[day] + " " + Month[month] + " " + year);
        if (year > getYear) {
            System.out.println("Its time to remind you about ");
        } else {
            System.out.println("Waiting");
        }
        int Mins = 0;
        while (userInputMinutes != Mins) {
            Mins = watch1.CurrentMinutes();
            System.out.println("Current Minutes = " + getMinutes);
            System.out.println("Entered minutes =" + userInputMinutes);
            Thread.sleep(10000);
        }
        System.out.println("Its time to remind you about " + rem);
    }
    public static void Date(String time) {
    }

}
Thanga :

You are setting the new Date() only once. So you will be getting that same in while loop iterations. To get a new date for every iteration, you have to set the below code inside the while loop

TimeAndDate = new Date();
int getDay = TimeAndDate.getDay();
int getMonth = TimeAndDate.getMonth() + 1;
int getYear = TimeAndDate.getYear() + 1900;
int getMinutes = TimeAndDate.getMinutes();
Watch watch1 = new Watch();

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=374377&siteId=1