- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Date and Time parts separately in Java
To get the time parts separately, use the date-time fields.
To get the date, use the following fields.
Calendar.YEAR Calendar.MONTH Calendar.DATE
To get the time, use the following fields.
Calendar.HOUR_OF_DAY Calendar.HOUR Calendar.MINUTE Calendar.SECOND Calendar.MILLISECOND
The following is an example.
Example
import java.util.Calendar; public class Demo { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); // current date and time System.out.println(cal.getTime().toString()); // date information System.out.println("
Date Information.........."); System.out.println("Year = " + cal.get(Calendar.YEAR)); System.out.println("Month = " + (cal.get(Calendar.MONTH) + 1)); System.out.println("Date = " + cal.get(Calendar.DATE)); // time information System.out.println("
Time Information.........."); System.out.println("Hour (24 hour format) : " + cal.get(Calendar.HOUR_OF_DAY)); System.out.println("Hour (12 hour format) : " + cal.get(Calendar.HOUR)); System.out.println("Minute : " + cal.get(Calendar.MINUTE)); System.out.println("Second : " + cal.get(Calendar.SECOND)); System.out.println("Millisecond : " + cal.get(Calendar.MILLISECOND)); } }
Output
Mon Nov 19 14:46:29 UTC 2018 Date Information.......... Year = 2018 Month = 11 Date = 19 Time Information.......... Hour (24 hour format) : 14 Hour (12 hour format) : 2 Minute : 46 Second : 29 Millisecond : 762
- Related Articles
- How to get the current date and time in Java?
- How to get Time in Milliseconds for the Given date and time in Java?
- Java Program to Get Current Date/Time
- How to get the date and time in JShell in Java 9?
- How to get (format) date and time from mill seconds in Java?
- How to get current date/time in milli seconds in Java?
- Sorting parts of array separately in JavaScript
- Get current time and date on Android
- How to get current date and time in Python?
- How to get formatted date and time in Python?
- How to get current date and time in JavaScript?
- How to get current time and date in C++?
- Modify Date and Time from GregorianCalendar in Java
- Get current date/time in seconds - JavaScript?
- Java Program to parse date and time

Advertisements