Java DateTime, Calendar Exercises: Display the dates in the specified formats
Java DateTime, Calendar: Exercise-24 with Solution
Write a Java program to display the dates in the specified formats.
Sample format:
Default format of LocalDate=2016-09-16 16::Sep::2016 Default format of LocalDateTime=2016-09-16T11:46:01.455 16::Sep::2016 11::46::01 Default format of Instant=2016-09-16T06:16:01.456Z Default format after parsing = 2014-04-27T21:39:48
Sample Solution:
Java Code:
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateParseFormatExercise24 {
public static void main(String[] args) {
//Format examples
LocalDate date = LocalDate.now();
//default format
System.out.println("\nDefault format of LocalDate="+date);
//specific format
System.out.println(date.format(DateTimeFormatter.ofPattern("d::MMM::uuuu")));
LocalDateTime dateTime = LocalDateTime.now();
//default format
System.out.println("Default format of LocalDateTime="+dateTime);
//specific format
System.out.println(dateTime.format(DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss")));
Instant timestamp = Instant.now();
//default format
System.out.println("Default format of Instant="+timestamp);
//Parse examples
LocalDateTime dt = LocalDateTime.parse("27::Apr::2014 21::39::48",
DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss"));
System.out.println("Default format after parsing = "+dt+"\n");
}
}
Sample Output:
Default format of LocalDate=2017-06-21 21::Jun::2017 Default format of LocalDateTime=2017-06-21T11:46:21.543 21::Jun::2017 11::46::21 Default format of Instant=2017-06-21T06:16:21.543Z Default format after parsing = 2014-04-27T21:39:48
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 get the months remaining in the year.
Next: Write a Java program to get the information of current/given year.
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