Core JAVA
Java Runtime Environment (JRE)
Java Virtual Machine (JVM)
Java overview
Java basics
Java Objects and classes
Java Constructors
Java basic datatypes
Java variable types
Java modifiers/Access Modifiers In Java
Java Basic Operators
Java Loops and Controls
Java conditions
Java numbers and characters
Java strings
Java arrays
Java date time
Java methods
Java file and IO operations
Java exceptions
Inner class
Java OOPs Concepts
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java Interface
Cohesion and Coupling
Association, Aggregation and Composition
Java Collections
Java ArrayList
Java LinkedList
Set and HashSet
LinkedHashSet and TreeSet
Queue and PriorityQueue
Deque and PriorityQueue
Java Map Interface
Java HashMap
Internal Working Of Java HashMap
Java Mutithread
Methods of Thread In Java
Join , run & Start Method in Threads
Difference b/w start & run Methods in Threads
Java Concurrency Package & its Features
CountDownLatch, CyclicBarrier, Semaphore and Mutex in Thread
Java - Data Time
Java date time is very useful in declaring different date formats and doing manipulation on data and time.
Java Date class is available in java library package java.util.
Java Date Constructor
Java date class has two constructors one with arguments and another without any argument:
- Date( ) –This constructor does not take any input and creates date object with current system date.
- Date(long millisec) – This constructor takes time in milli seconds and creates date object of that time.
Example of Java Data Constructors
import java.util.Date; public class DateTimeExample { public static void main(String[] args) { Date date = new Date(); Date dateWithTimeInMillies = new Date(92121312); System.out.println("date without argument="+date); System.out.println("date with arguments="+dateWithTimeInMillies); } }
Output:
date without argument=Tue Mar 23 10:54:33 IST 2021
date with arguments=Fri Jan 02 07:05:21 IST 1970
Methods in Java Date Class
Below are few useful methods in date class:
- toString() – Converts date to string object.
- equals() – This method belongs to Object class and inherited by Date class which is used to compare two dates hash codes.
- getDate() – Returns the number of milliseconds that have elapsed since 1st Jan 1970
- setDate() – Sets date in milliseconds.
- hashCode() – Retursn hash code of java object.
Java SimpleDateFormat
Java has SimpleDateFormat class which is used to format date into desired or supported formats, for example if some want to format date into YYYY-mm-dd or YY-mm-dd or so one, with help of SimpleDateFormat class we can format date.
Syntax:
SimpleDateFormat <variable name for date formatter> = new SimpleDateFormat(<desired supported date format>);
Example:
Below example shows different java data formatters.
import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatter { public static void main(String[] args) { SimpleDateFormat sfd1 = new SimpleDateFormat("yyyy-MM-dd"); //this will format date in this format SimpleDateFormat sfd2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dateToBeFormatted = new Date(); String formattedDate1 = sfd1.format(dateToBeFormatted); String formattedDate2 = sfd2.format(dateToBeFormatted); System.out.println("formattedDate1 = "+formattedDate1); System.out.println("formattedDate2 = "+formattedDate2); } }
Output:
formattedDate1 =2021-01-01
formattedDate1 =2021-01-01 12:12:12
Example of SimpleDateFormat to parse date string to Date object:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatter { public static void main(String[] args) throws ParseException { SimpleDateFormat sfd1 = new SimpleDateFormat("yyyy-MM-dd"); Date parsedDate = sfd1.parse("2021-01-01"); //parsing this input date string to object System.out.println("parsedDate = "+parsedDate); } }
Output:
parsedDate = Fri Jan 01 00:00:00 IST 2021
GregorianCalendar Class
Java GregorianCalendar class is used to get different date related fields for example year, month, day and etc.
This is an implementation of Calendar class in java.
Constructors of GregorianCalendar class:
- GregorianCalendar() – Creates object with current date.
- GregorianCalendar(int year, int month, int date) – Creates object with parameters year, month and date.
- GregorianCalendar(int year, int month, int date, int hour, int minute) – Creates object with parameters year, month, date, hour and minute.
- GregorianCalendar(int year, int month, int date, int hour, int minute, int second) – Creates object with parameters year, month, date, hour, minute and seconds.
- GregorianCalendar(Locale aLocale) – Creates object with parameters mentioned.
- GregorianCalendar(TimeZone zone) – Creates object with parameters mentioned.
- GregorianCalendar(TimeZone zone, Locale aLocale) – Creates object with parameters mentioned.
Example of GregorianCalendar:
import java.util.Calendar; import java.util.GregorianCalendar; public class GregorianCalendarExample { public static void main(String[] args) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); //New calendar with current system date time int year = gregorianCalendar.get(Calendar.YEAR); int month = gregorianCalendar.get(Calendar.MONTH); int day = gregorianCalendar.get(Calendar.DATE); int hour = gregorianCalendar.get(Calendar.HOUR); int minute = gregorianCalendar.get(Calendar.MINUTE); int seconds = gregorianCalendar.get(Calendar.SECOND); System.out.println("year="+year); System.out.println("month="+month); System.out.println("day="+day); System.out.println("hour="+hour); System.out.println("minute="+minute); System.out.println("seconds="+seconds); } }
OUTPUT:
year=2021
month=2
day=23
hour=0
minute=49
seconds=37
References
Next Article To Read
You may be interested in our next articles: