I am trying to do a sort of and expiry date so that when an account is created about 3 months later I'll be informed so that the account can be renewed. I don't know how I can do this and to tell you the truth I never used the calander. I tried to do something but certainly I need help. After entering the dates I am parsing them in a calander format but I don't know if it is useless.Then I tried to do an expiry method but to tell you the truth I don't have an idea how to do it correctly. The following is my code till now. Can someone pls help me? Thanks a lot.

import java.io.*;         // required for handling the IOExceptions
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.*;

class Transactions
{
     // create an empty list to hold Cars
     ArrayList<GymUser> UserListIn = new ArrayList<GymUser>();
     
     public Transactions (ArrayList<GymUser> Tran)
     {
         UserListIn = Tran;
     }
     static void addTransaction(ArrayList<User> UserListIn)
    {
String tempMemberShipID;
        String tempName;
        String tempSurname;
        String tempAge;
        String tempAddress;
        String tempPhone;
        String tempDuration;
        String tempFrom;
        String tempTo;
        String tempTypeOfMembership;
        
        String longDate;
        int date;
        int month;
        int year;
        int hour;
        int minute;

        
        
        
        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        
        System.out.println("Please enter the member's Personal Details to process Transaction");
        System.out.println("----------------------------------------------------------------- ");
        System.out.println("Enter the membership number");
        tempMemberShipID = keyboard.next();
        System.out.print("Enter Name: ");
        tempName = keyboard.next();
        System.out.print("Enter Surname:");
        tempSurname = keyboard.next();
        System.out.print("Enter Address:");
        tempAddress = keyboard.next();
        System.out.print("Enter Age:");
        tempAge = keyboard.next();
        System.out.print("Enter Phone:");
        tempPhone = keyboard.next();
        System.out.print("Type of membership:");
        tempTypeOfMembership = keyboard.next();
        System.out.print("Membership Duration:");
        tempDuration = keyboard.next();
        System.out.print("From(YYYY-MM-dd): ");
        tempFrom = keyboard.next();
        
        //parsing date
        Date   newDate;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        System.out.print("  " + tempFrom + " parses as ");
        try { 
            newDate = formatter.parse(tempFrom);
           System.out.println(newDate + ".");
        } catch (ParseException e) {
             System.out.println("Unparseable using " + formatter + ".");
        }    
        

        System.out.print("To(DD/MM/YYYY): ");
        tempTo = keyboard.next();
        
        //parsing date
        Date   EndDate;
        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
        System.out.print("  " + tempTo + " parses as ");
        try { 
            EndDate = formatter2.parse(tempTo);
           System.out.println(EndDate + ".");
        } catch (ParseException e) {
             System.out.println("Unparseable using " + formatter + ".");
        }    
        
        
        
        
        UserListIn.add(new User(tempMemberShipID, tempName, tempSurname, tempAddress, tempPhone, tempAge,tempTypeOfMembership,tempDuration,tempFrom,tempTo));
        
            
    }

  static void Expiry()
    {
        Calendar calExpired = Calendar.getInstance();
        calExpired.add(Calendar.MONTH, -3);
    
        Date EndDate = null; //get it from your db or log file...
        Calendar cal_lastlogin = Calendar.getInstance();
        if(cal_lastlogin.before(calExpired))
        {
        System.out.println("Account to be Renewed!");
    //    System.out.println(item.getM_ID()+" "+item.getName()+" "+item.getSurname()+"  "+item.getAddress()+" "+item.getPhone()+" "+item.getAge()+" "+item.getType()+"  "+item.getDuration()+" "+item.getFrom()+" "+item.getTo());
        }
    }
}

What do you mean by doesn't work? Post a sample test case for which your code fails. And come to think of it:

Date EndDate = null; //get it from your db or log file...
Calendar cal_lastlogin = Calendar.getInstance();
if(cal_lastlogin.before(calExpired)) {
   /* something */
}

Here, you are pulling out the date to be compared in a variable EndDate and end up not using it. cal_lastlogin will always give a current Calendar instance, hence the test will be always false.

Yes you're right I am not using EndDate at all. So that can be removed..yes? Accordng to my logic I am 'putting' today's date in calExpired and decreasing 3 months from today. Then in the if statment I am comparing todays date stored in the var cal_lastlogin to that of the -3 months, and that's where is the mistake I think or somewhere near the logic of the system.out.println cause when I am testing the method I am having no output. In the system.out..... Now I am looking at the code and I am noticing that I am not checking the arrayList to compare the dates of when the account is created. I have to make that...what do you think!!

static void Expiry(ArrayList<User> UserListIn)
    {

        Calendar calExpired = Calendar.getInstance();
        calExpired.add(Calendar.MONTH, -2);
    
               Calendar cal_lastlogin = Calendar.getInstance();
        if(calExpired.before(cal_lastlogin))
        {
        System.out.println("Account to be Renewed!");
    
        } 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.