Date
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 at which it was allocated, measured to the nearest millisecond.
We can print the date to check its value.
Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.core; import java.util.Date; public class getDate { public static void main(String[] args) { // Create date object kai print its value Date date = new Date(); System.out.println("Today is " + date); } }
Output:
Today is Sun Jul 22 19:13:44 EEST 2012
This was an example of how to get a Date in Java.