create a date in java code example

Example 1: taking date as input in java

import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample1 {
public static void main(String[] args)throws Exception {
String sDate1="31/12/1998";
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
System.out.println(sDate1+"\t"+date1);
}

Example 2: how to create date class in java

// This creates a new Date instance with current system time. Internally It uses System.currentTimeMillis()
Date date = new Date();
// This creates a new Date instance with time that you specify in milliseconds since the epoch time
Date date = new Date(1672556400000L); // This time is Jan 1 2023 in GMT

Tags:

Java Example