w3resource

Java: Get the information of current or given year


25. Year Info

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


For more Practice: Solve these Related Problems:

  • Write a Java program to display the current year along with its number of days and leap year status.
  • Write a Java program to extract and show details of a user-specified year, including leap year information.
  • Write a Java program to compute and display the length of a given year and its first and last days.
  • Write a Java program to compare the current year’s length with that of another specified year.

Go to:


PREV : Format Dates and Times.
NEXT : Month Info.

Java Code Editor:

Improve this sample solution and post your code through Disqus

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.