w3resource logo


Java Programming Exercies

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

Secondary Nav

Java DateTime, Calendar : Exercise-25 with Solution

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

Sample format :

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

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

Output:

Current Year: 2016                                                      
Is current year leap year? true                                         
Length of the year: 366 days

Java Practice online:

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



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number