Get current Date and Time

Sometimes we may need to get current Date and Time. There are many ways to get current date and time. Let’s see these with an example. 1) Using java.util.Date class: If you want to get current date and time using java.util.Date class, then just create an instance of Date class and print the instance. 2) Using java.util.Calendar class: If you want to get current date and time using java.util.Calendar class, then get the instance of Date class from Calendar class using the getTime() method. The Calendar.getInstance() method returns the instance of Calendar…

Read More

Compare Dates in Java

While developing applications in Java, sometimes we may need to Compare Dates in Java. There are different ways to compare dates in Java. Let’s see these with an example. 1) Using compareTo() method from java.util.Date class: compareTo() method compares two dates. Syntax:

Parameters: anotherDate – the Date to be compared. Returns: returns 0 if the argument Date is equal to this Date. returns a value less than 0, if this Date is before the Date argument. returns a value greater than 0. if this Date is after the Date argument.…

Read More

Convert Date to String in Java

Many times we need to convert the Date in String format as some of the applications may store the data in String format. In this tutorial we will see how to Convert Date to String in Java with an example. We will create an instance of SimpleDateFormat which is used for formatting the string representation of date. We will get the today’s date and use the format method of SimpleDateFormat class to get the String out of the date. Example:

Let’s run this program and check the output. Output:

In…

Read More

Get current Timestamp in Java

In this tutorial, we will see how to get current Timestamp in Java.  Java provides Timestamp class which can be used to get the current timestamp. Let’s see this with an example. Example:

Let’s run this program and check the output. Output:

Read More