ok looking at simpleDateFormat
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
ok looking at simpleDateFormat
Please show what you are having problems with.not able to print loan details..
See http://docs.oracle.com/javase/tutori...dateintro.html
If you don't understand my answer, don't ignore it, ask a question.
Hia...Still having problems with my codes, nearly done now but the loan class is giving me headache..
/** * This Class would hold all loan details. * * @author () * @version 1.0 */ public class Loan { // fields for the Loan Class. private int bookId; private int memberId; private GregorianCalendar loanDate; private GregorianCalendar dateDueBack; private String month = ""; private String day = ""; private String year = ""; private String currentDate = ""; /** * Constructor for objects of class Loan */ public Loan(int bookId, int memberId) { // initialised fields. this.bookId = bookId; this.memberId = memberId; this.loanDate = new GregorianCalendar(); GregorianCalendar gregorianCalendar=new GregorianCalendar(); month=String.valueOf(gregorianCalendar.get(GregorianCalendar.MONTH)); day=String.valueOf(gregorianCalendar.get(GregorianCalendar.DAY_OF_MONTH)); year=String.valueOf(gregorianCalendar.get(GregorianCalendar.YEAR)); currentDate = (day + " / " + month + " / " + year); }
Here im trying to define date when loan is taken out with gregorian Calendar..when a loan object is created a date stamp should be created to be the loan date at the same time, so i can call the getLoandate an get that time. I want to add 21 days to the loan date and be be able to call it via get dateDueBack. Im not sure how to add 21 days to the loandate to accomplish this. please help....thanks
Use the Calendar class's add() method.Im not sure how to add 21 days
If you don't understand my answer, don't ignore it, ask a question.
I tried it earlier in the construtor but it did not work..ok i ll try it again..have to submit the project on friday...thanx
When you have code that 'does not work', you should post it and explain what it does and ask about how to have it do what you want it to do.but it did not work
If you don't understand my answer, don't ignore it, ask a question.
ok I ll take note..
--- Update ---
public void add(int loanDate, int amount) { Calendar cal = new GregorianCalendar(); cal.add(loanDate, + 21); }
Here is the add method, it says "cannot find symbol class calendar"
All java class names start with Uppercase letters. Try using Calendar."cannot find symbol class calendar"
If you don't understand my answer, don't ignore it, ask a question.
You fooled me with this:Do you have the import statement for the package the Calendar class is in?"cannot find symbol class calendar"
If you don't understand my answer, don't ignore it, ask a question.
no but i do have Gregorian calendar imported
--- Update ---
wow..just imported calendar and its compiled...i see if it works..you are a star...cheers
--- Update ---
at Loan.getDateDueBack(Loan.java:85)
at Loan.toString(Loan.java:95)
java.lang.NullPointerException
at Loan.setDateDueBack(Loan.java:75)
this is what i get with the add method
You left off the first lines of the error message. You need to post the whole message.
In the middle of the message is a NullPointerException at line 75.
What variable on line 75 is null? Look at line 75, find the null variable and then backtrack in the code to find out why that variable does not have a valid value.
If you don't understand my answer, don't ignore it, ask a question.
ok i will
--- Update ---
/** * Get the date of the loan. * @return */ public GregorianCalendar getLoandDate() { return (GregorianCalendar) loanDate.clone(); }
Here is the code on line 75
What variable had the null value? What was the value of loanDate? Print it out with a println if you don't know what its value was.
If you don't understand my answer, don't ignore it, ask a question.
The Loan date isjava.util.GregorianCalendar[time=1355021169274,areFieldsSet=true,areAllFieldsS et=true,lenient=true,zone=sun.util.calendar.ZoneIn fo[id="Europe/London",offset=0,dstSavings=3600000,useDaylight=tr ue,transitions=242,lastRule=java.util.SimpleTimeZo ne[id=Europe/London,offset=0,dstSavings=3600000,useDaylight=tru e,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode =2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,Y EAR=2012,MONTH=11,WEEK_OF_YEAR=49,WEEK_OF_MONTH=1, DAY_OF_MONTH=9,DAY_OF_YEAR=344,DAY_OF_WEEK=1,DAY_O F_WEEK_IN_MONTH=2,AM_PM=0,HOUR=2,HOUR_OF_DAY=2,MIN UTE=46,SECOND=9,MILLISECOND=274,ZONE_OFFSET=0,DST_ OFFSET=0]
This is what i get when i print the loan Date...I think I need to assign the current date to the loanDate, but i dont know how to do it..i ve declared current date in the field.
this.loanDate = new GregorianCalendar(); Im trying to assign the current date to the loanDate here but I dont think the code is right....
--- Update ---
the value for the loandate date should be when the loan object is created.......
If loanDate did not have a null value, then there is something wrong with what you have been posting.
The text of the truncated/mangled error message you posted said there was a variable with a null value on line 75.
The variable on that line was loanDate. Did you add a println between lines 74 and 75 to print out the value of the loanDate variable used on line 75?
Can you Post the full text of the error message that shows everything?
If you don't understand my answer, don't ignore it, ask a question.
/** * This Class would hold all loan details. * * @author () * @version 1.0 */ public class Loan { // fields for the Loan Class. private int bookId; private int memberId; private GregorianCalendar loanDate; private GregorianCalendar dateDueBack; private String month = ""; private String day = ""; private String year = ""; private String currentDate = ""; /** * Constructor for objects of class Loan */ public Loan(int bookId, int memberId) { // initialised fields. this.bookId = bookId; this.memberId = memberId; this.loanDate = new GregorianCalendar(); GregorianCalendar gregorianCalendar=new GregorianCalendar(); month=String.valueOf(gregorianCalendar.get(GregorianCalendar.MONTH)); day=String.valueOf(gregorianCalendar.get(GregorianCalendar.DAY_OF_MONTH)); year=String.valueOf(gregorianCalendar.get(GregorianCalendar.YEAR)); currentDate = (day + " / " + month + " / " + year); } /** * Set date when the loan was taken out. * * @param date the day of the month 1 to 28, 30 or 31 depending on month * @param month 1 (January) to 12 December * @param year the year including century e.g. 2010 */ public void setLoanDtae(int date, int month, int year) { loanDate.set(date, month - 1, year); } public void add(int loanDate, int amount) { Calendar cal = new GregorianCalendar(); cal.add(loanDate, + 21); } /** * Get the date of the loan. * @return */ public GregorianCalendar getLoandDate() { System.out.println(loanDate); return (GregorianCalendar) loanDate.clone(); } /** * Get date when loan is due back. * @return */ public GregorianCalendar getDateDueBack() { return (GregorianCalendar) dateDueBack.clone(); } /** * Return date when loan is due back as a String. */ public String toString() { String s = new String(); s = memberId + " " + bookId + " Loan "; s += getDateDueBack(); return s; } /** * * */ public void printLoanDetails() { System.out.println("Member ID:" + memberId); System.out.println("Book ID:" + bookId); System.out.println("The Loan date is" + loanDate); GregorianCalendar calendar = new GregorianCalendar(); int day = calendar.get(Calendar.DATE); int month = calendar.get(Calendar.MONTH); int year = calendar.get(Calendar.YEAR); // System.out.println("The Loan date is " + calendar.get(Calendar.DATE) + " " + (calendar.get(Calendar.MONTH) + 1) + " " + calendar.get(Calendar.YEAR));
Here is the whole code in the Loan Class..That was the message I was getting......
Sorry, that was not the full text of an error message. You need to post the full text.
I'm done for tonight. Back tomorrow.
If you don't understand my answer, don't ignore it, ask a question.
ok thanks for your help....me too im off to bed...I go to sleep dreaming and thinking of codes...its driving me crazy....
Morning...its a new day and more coding to be done....Today i need to be able to initialise loanDate and give its value to the field..instead of creating a parameter to pass it through..
import java.util.GregorianCalendar; import java.util.Calendar; import java.util.*; import java.util.*; /** * This Class would hold all loan details. * * @author () * @version 1.0 */ public class Loan { // fields for the Loan Class. private int bookId;// The borrower's unique ID. private int memberId;// The book's unique Identification number. private GregorianCalendar loanDate;// The date when loan was taken out. private GregorianCalendar dateLoanDueBack;// The date when loan is due back. /** * Constructor for objects of class Loan */ public Loan(int bookId, int memberId) { // initialised fields. this.bookId = bookId; this.memberId = memberId; loanDate = new GregorianCalendar(); dateLoanDueBack = new GregorianCalendar(); dateLoanDueBack.add(dateLoanDueBack.Date, 21); } public Loan() { } /** * Set date when the loan was taken out. * * @param date the day of the month 1 to 28, 30 or 31 depending on month * @param month 1 (January) to 12 December * @param year the year including century e.g. 2010 */ public void setLoanDtae(int date, int month, int year) { loanDate.set(date, month - 1, year); } public String getDateLoanDueBack() { SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy"); return df.format(dateLoanDueBack.getTime()); } /** * Get the date of the loan. * @return */ public GregorianCalendar getLoanDate() { SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy"); return df.format(loanDate.getTime()); } /** * Get date when loan is due back. * @return */ public GregorianCalendar getDateDueBack() { return (GregorianCalendar) dateDueBack.clone(); } /** * Return date when loan is due back as a String. */ public String toString() { String s = new String(); s = memberId + " " + bookId + " Loan "; s += getDateDueBack(); return s; } /** * * */ public void printLoanDetails() { System.out.println("Member ID:" + memberId); System.out.println("Book ID:" + bookId); System.out.println("The Loan date is" + loanDate); System.out.println("The due back date for this loan is:" + dateLoanDueBack); } }
Hi mate I have re-written the loan class but im getting an error message "cannot find Symbol - variable Date" help please.........
If that message is from the compiler, (it looks different from normal compiler error messages)"cannot find Symbol - variable Date"
then the compiler can not find a definition for the variable: Date.
BTW conventions say variable names should start with lowercase names. Using an uppercase first letter is for class names.
What line in the code is getting the error message? That is shown on the normal compiler error message.
Where is the variable: Date defined?
If you don't understand my answer, don't ignore it, ask a question.
/**
* Constructor for objects of class Loan
*/
public Loan(int bookId, int memberId)
{
// initialised fields.
this.bookId = bookId;
this.memberId = memberId;
loanDate = new GregorianCalendar();
dateLoanDueBack = new GregorianCalendar();
dateLoanDueBack.add(dateLoanDueBack.Date, 21);
}
dateLoanDueBack.add(dateLoanDueBack.Date, 21); Its complaining about the date here(Date) Im trying to use the add method to add 21 days here for the due back date during initialisation.....
What is that value supposed to be?dateLoanDueBack.Date
Does the class for the object referred to by: dateLoanDueBack (GregorianCalendar) define a variable: Date? If not, then the code makes no sense.
What value should the first arg to the add() method be?
If you don't understand my answer, don't ignore it, ask a question.