Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, how can i make method calculates how many years have passed since the publication of the book. I don't know how to get today year so i can take it out from bookDateOfMade 


That is the code

    public class Book {

	private String bookName;
	private String bookAuthor;
	private LiteratureGenre bookGenre;
	private Locale bookLanguage;
	private  LocalDate bookDateOfMade;
	
	public Book(String bookName, String bookAuthor, LiteratureGenre bookGenre, Locale bookLanguage, LocalDate bookDateOfMade) {
		
		this.bookName = bookName;
		this.bookAuthor = bookAuthor;
		this.bookGenre = bookGenre;
		this.bookLanguage = bookLanguage;
		this.bookDateOfMade = bookDateOfMade;
		
	}

	
	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public String getBookAuthor() {
		return bookAuthor;
	}

	public void setBookAuthor(String bookAuthor) {
		this.bookAuthor = bookAuthor;
	}

	public LiteratureGenre getBookGenre() {
		return bookGenre;
	}

	public void setBookGenre(LiteratureGenre bookGenre) {
		this.bookGenre = bookGenre;
	}

	public Locale getBookLanguage() {
		return bookLanguage;
	}

	public void setBookLanguage(Locale bookLanguage) {
		this.bookLanguage = bookLanguage;
	}

	public LocalDate getBookDateOfMade() {
		return bookDateOfMade;
	}

	public void setBookDateOfMade(LocalDate bookDateOfMade) {
		this.bookDateOfMade = bookDateOfMade;
	}

	
	@Override
	public String toString() {
		return "The Book " + bookName + " written by " + bookAuthor + " with genre " + bookGenre
				+ "the copy language " + bookLanguage + " was made on " + bookDateOfMade;
	}
	
	
	public String toShortString() {
		return "The Book " + bookName + " written by " + bookAuthor ;
	}
	
	
	public static getAge() {
	
	}
}


What I have tried:

considered different codes
Posted
Updated 10-Dec-21 3:02am

Get the current date as a Calendar object (e.g. How to Get Current Date and Time in Java - Javatpoint[^] ) then extract the year
Java
myCal.get(Calendar.YEAR);
If you have Java8 you can use LocalDate class, see - https://www.baeldung.com/java-year-month-day[^]

That's fine if you are only working in full years but you probably want to work out the difference between the two dates in months, then divide by 12 to get the full years. https://www.baeldung.com/java-date-difference. E.g. [^]
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900