Home » Core Java » util » Date

Date

java.util.Date to java.sql.Date

In this example, we shall show you how to convert a java.util.Date object to a java.sql.Date object. This conversion is usually necessary when a Date object needs to be written in a database. java.util.Date represents a specific instant in time, with millisecond precision. It represents both date and time. java.sql.Date is a wrapper around millisecond value and is used by ...

Read More »

Java – Get current time and date

There are a couple of ways to get the current time and date in Java. You may get the number of milliseconds since January 1, 1970, 00:00:00 GMT using System.currentTimeMillis() and calculate the time by yourself (a problem given to beginners, but too much work if you are developing something), you may use a library not included in the JDK ...

Read More »

java.util.Date Example – How to use Date

In this example we will show how to use java.util.Date class. Class Date represents a specific instant in time, with millisecond precision. 1. Date constructors The java.util.Date class supports two constructors: Date() This constructor initializes a Date object with the current date and time. Time is measured to the nearest millisecond. Date(long millisec) This constructor initializes a Date object so ...

Read More »

Get Date

With this example we are going to demonstrate how to get a Date in Java. The class Date represents a specific instant in time, with millisecond precision. In short, to get a Date object you should: Create a new Date object, using the Date() constructor. The constructor allocates a Date object and initializes it so that it represents the time ...

Read More »

Date from specific time

This is an example of how to get a Date from a specific time. The class Date represents a specific instant in time, with millisecond precision. Getting a Date from a specific long time implies that you should: Create a new Date object, using the Date(long date) constructor, with a long parameter. The constructor allocates a Date object and initializes ...

Read More »

Convert Date to millisec

In this example we shall show you how to convert a Date to millisec, that means get the long number of millisec that represent a Date object. A Date object represents a specific instant in time, with millisecond precision. To convert a Date to millisec one should perform the following steps: Create a new Date object, using the Date() constructor, ...

Read More »

Compare Date objects with compareTo method

With this example we are going to demonstrate how to compare Date objects with compareTo method of the Date class. In short, to compare a Date object with another you should: Create a new Date object, using the Date() constructor, that allocates a Date object and initializes it so that it represents the time at which it was allocated, measured ...

Read More »

Compare Date objects with before method

This is an example of how to compare Date objects with before(Date when) API method of Date. Comparing a Date with another Date using before(Date when) method implies that you should: Create a new Date object, using the Date() constructor, that allocates a Date object and initializes it so that it represents the time at which it was allocated, measured ...

Read More »

Compare Date objects with after method

In this example we shall show you how to compare Date objects with after(Date when) method of Date, that tests if a Date is after the specified one. To compare a Date object with another Date object, using after(Date when) API method of Date one should perform the following steps: Use the simple Date() constructor, to allocate a Date object ...

Read More »