Results 1 to 7 of 7
Thread: Date format problem
- 01-01-2012, 12:18 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
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
- 01-01-2012, 01:03 PM #2
Re: Date format problem
get the date and time to appear as year , month , day , time.
Have you looked at the DateFormat class?
- 01-01-2012, 01:53 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
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
- 01-01-2012, 10:34 PM #4
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
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
- 01-01-2012, 10:45 PM #5
Re: Date format problem
Is the problem sovled now?
- 01-01-2012, 10:47 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
Re: Date format problem
Hi Norm
Yes it works a charm. Thank you for your help
Robbie
- 01-01-2012, 10:50 PM #7
Similar Threads
-
Date Format
By riddhishah28 in forum AWT / SwingReplies: 1Last Post: 02-19-2011, 03:46 PM -
Format date
By mine0926 in forum New To JavaReplies: 11Last Post: 08-04-2010, 01:56 AM -
Date format exception
By chaudhas in forum New To JavaReplies: 7Last Post: 06-25-2010, 09:31 AM -
julian date to full date format
By judy318 in forum New To JavaReplies: 7Last Post: 11-02-2009, 12:17 PM -
Date Format
By learnspring in forum New To JavaReplies: 1Last Post: 11-16-2008, 05:16 PM
Bookmarks