w3resource

Java: Get the information of current or given year

Java DateTime, Calendar: Exercise-25 with Solution

Write a Java program to get the information of the current/given year.

Sample format:

Current Year: 2001
Is current year leap year? false                                             
Length of the year: 365 days

Sample Solution:

Java Code:

import java.time.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateParseFormatExercise25 {

	public static void main(String[] args) {
	
	 //Current year	
	 Year yr = Year.now();
     //given year
     //Year yr = Year.of(2001);
     System.out.println("\nCurrent Year: " + yr);  
     boolean isLeap = yr.isLeap(); // false
     System.out.println("Is current year leap year? " + isLeap);  
     int length = yr.length(); // 365
     System.out.println("Length of the year: " + length+" days\n"); 
	}
}

Sample Output:

Current Year: 2017                                                                                          
Is current year leap year? false                                                                              
Length of the year: 365 days

N.B.: The result may varry for your system date and time.

Pictorial Presentation:

Java Exercises: Java DateTime, Calendar Exercises - Get the information of current or given year.

Flowchart:

Flowchart: Java DateTime, Calendar Exercises - Get the information of current or given year

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to display the dates in the specified formats.
Next: Write a Java program to to get the information of current/given month.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.