Results 1 to 7 of 7
  1. #1
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default Date format problem

    Hi All

    I'm new to java. I am currently creating an account class and I am having a problem with the date format. How can I get the date and time to appear as year , month , day , time. The code is below

    Java Code:
     import java.util.Calendar;
    
    
    public class Account2
    {
            public int accountId;
            protected double balance;
    		public int annualInterestRate;
    		Calendar c = Calendar.getInstance ();
    
            // Constructor to initialize balance
            public Account2( int accountNumber,double amount, int interestRate, Calendar dateCreated  )
    	{
    	    accountId = accountNumber;
    		balance = amount;
    		annualInterestRate = interestRate;
    		c.set(2010,2,7,12,01);
    
    
    	}
    
            // Overloaded constructor for empty balance
            public Account2()
    	{
    		balance = 0.0;
    
    	}
    
            public void deposit( double amount )
    	{
    		balance += amount;
    	}
    
            public double withdraw( double amount )
    	{
                    // See if amount can be withdrawn
    		if (balance >= amount)
    		{
    			balance -= amount;
                            return amount;
    		}
    		else
                    // Withdrawal not allowed
                            return 0.0;
    	}
    
            public double getbalance()
    	{
                    return balance;
    	}
    
    	public int getaccountId ()
    {
    return accountId;
    }
    
    public double getMonthlyInterestRate() {
    return annualInterestRate;
    }
    
    public Calendar getc (){
    	return c;}
    
    }
    
    
    
    
    
    /*
     *
     * 
     * Demonstration of Account2 class
     *
     */
    
    class AccountDemo
    {
            public static void main(String args[])
    	{
                    // Create an empty account
                    Account2 my_account = new Account2();
    
    				my_account.accountId = 1122;
    
    				my_account.balance = 20000;
    				my_account.c.set (2010,02,15,10,40) ;
    
                    // Deposit money
    		my_account.deposit(3000.00);
    
                    // Print current balance
    		System.out.println ("Current balance " +
    			my_account.getbalance());
    
                    // Withdraw money
    		my_account.withdraw(2500.00);
    
                    // Print remaining balance
    		System.out.println ("Remaining balance " +
    			my_account.getbalance());
    
    
    			// Print remaining balance
    		System.out.println ("Your account was created on" +
    			my_account.getc());
    
    
    	}
    }
    Last edited by Norm; 01-01-2012 at 01:01 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Date format problem

    get the date and time to appear as year , month , day , time.
    Where do you have the date in your program?

    Have you looked at the DateFormat class?

  3. #3
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default Re: Date format problem

    Calendar c = Calendar.getInstance ();

    then in the accountdemo i set the date and time as.

    my_account.c.set (2010,02,15,10,40)

    I will look at the dateformat class to see if this helps. Thanks

  4. #4
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default Re: Date format problem

    Java Code:
    import java.util.*;
    
    
    public class Account2
    {
            public int accountId;
            protected double balance;
    		public int annualInterestRate;
    		Date dateCreated = new Date () ;
    
            // Constructor
            public Account2( int accountNumber,double amount, int interestRate, Date dateCreated  )
    	{
    	    accountId = accountNumber;
    		balance = amount;
    		annualInterestRate = interestRate;
    		dateCreated = dateCreated;
    
    
    	}
    
            // Overloaded constructor for empty balance
            public Account2()
    	{
    		balance = 0.0;
    
    	}
    
            public void deposit( double amount )
    	{
    		balance += amount;
    	}
    
            public double withdraw( double amount )
    	{
                    // See if amount can be withdrawn
    		if (balance >= amount)
    		{
    			balance -= amount;
                            return amount;
    		}
    		else
                    // Withdrawal not allowed
                            return 0.0;
    	}
    
            public double getbalance()
    	{
                    return balance;
    	}
    
    	public int getaccountId ()
    {
    return accountId;
    }
    
    public double getMonthlyInterestRate() {
    return annualInterestRate;
    }
    
    public Date getdateCreated (){
    	return dateCreated;}
    
    }
    
    
    
    
    
    /*
     *
     * 
     * Demonstration of Account2 class
     *
     */
    
    class AccountDemo
    {
            public static void main(String args[])
    	{
                    // Create an empty account
                    Account2 my_account = new Account2();
    
    				my_account.accountId = 1122;
    
    				my_account.balance = 20000;
    
    
                    // Deposit money
    		my_account.deposit(3000.00);
    
                    // Print current balance
    		System.out.println ("Current balance " +
    			my_account.getbalance());
    
                    // Withdraw money
    		my_account.withdraw(2500.00);
    
                    // Print remaining balance
    		System.out.println ("Remaining balance " +
    			my_account.getbalance());
    
    
    			// Print date of account creation
    		System.out.println ("Your account was created on " +
    			my_account.dateCreated.toString());
    
    
    	}
    }
    Last edited by Norm; 01-01-2012 at 10:45 PM. Reason: added code tags

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Date format problem

    Is the problem sovled now?

  6. #6
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default Re: Date format problem

    Hi Norm

    Yes it works a charm. Thank you for your help


    Robbie

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

Similar Threads

  1. Date Format
    By riddhishah28 in forum AWT / Swing
    Replies: 1
    Last Post: 02-19-2011, 03:46 PM
  2. Format date
    By mine0926 in forum New To Java
    Replies: 11
    Last Post: 08-04-2010, 01:56 AM
  3. Date format exception
    By chaudhas in forum New To Java
    Replies: 7
    Last Post: 06-25-2010, 09:31 AM
  4. julian date to full date format
    By judy318 in forum New To Java
    Replies: 7
    Last Post: 11-02-2009, 12:17 PM
  5. Date Format
    By learnspring in forum New To Java
    Replies: 1
    Last Post: 11-16-2008, 05:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •