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.
Pictorial Presentation:
Flowchart:
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.
Java: Tips of the Day
How to sort an ArrayList?
Collections.sort(testList); Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
Ref: https://bit.ly/32urdSe
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework