w3resource logo
Java Programming Exercies

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

Java DateTime, Calendar : Exercise-25 with Solution

Write a Java program to get the information of 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.

Java Code Editor:

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"); 
	}
}

Improve this sample solution and post your code through Disqus