The Date class of java. util package implements Serializable, Cloneable, and Comparable interface. It provides constructors and methods to deal with date and time with java. Date(): Creates date object representing the current java date and time.
Parameter | Explanation |
No parameter | Creates a new Date object using the allocation time (to the nearest millisecond) |
long date | Creates a new Date object with the time set to the number of milliseconds since “the epoch” (January 1, 1970, 00:00:00 GMT) |
Convert java.util.Date to java.sql.Date
“java.util.Date” to “java.sql.Date” conversion is usually necessary when a Date object needs to be written in a database.
java.sql.Date is a wrapper around millisecond value and is used by JDBC to identify an SQL DATE type
In the below example, we use the java.util.Date() constructor, that creates a Date object and initializes it to represent time to the nearest millisecond. This data is used in the convert(java.util.Date utilDate) method to return a java.sql.Date object
Example
public class UtilToSqlConversion { public static void main(String args[]) { java.util.Date utilDate = new java.util.Date(); System.out.println("java.util.Date is : " + utilDate); java.sql.Date sqlDate = convert(utilDate); System.out.println("java.sql.Date is : " + sqlDate); DateFormat df = new SimpleDateFormat("dd/MM/YYYY - hh:mm:ss"); System.out.println("dateFormated date is : " + df.format(utilDate)); } private static java.sql.Date convert(java.util.Date uDate) { java.sql.Date sDate = new java.sql.Date(uDate.getTime()); return sDate; } }
Output
java.util.Date is : Fri Jul 22 14:40:35 IST 2016
java.sql.Date is : 2016-07-22
dateFormated date is : 22/07/2016 - 02:40:35
java.util.Date has both date and time information, whereas java.sql.Date only has date information
A basic date output
Using the following code with the format string yyyy/MM/dd hh:mm.ss, we will receive the following output
2016/04/19 11:45.36
// define the format to use String formatString = "yyyy/MM/dd hh:mm.ss"; // get a current date object Date date = Calendar.getInstance().getTime(); // create the formatter SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatString); // format the date String formattedDate = simpleDateFormat.format(date); // print it System.out.println(formattedDate); // single-line version of all above code System.out.println(new SimpleDateFormat("yyyy/MM/dd hh:mm.ss").format(Calendar.getInstance().getTime()));
Java 8 LocalDate and LocalDateTime objects
Date and LocalDate objects can not be compared exactly to each other because a Date object represents both a particular day and date, while a LocalDate object does not contain information about time or time zone. Yet switching between the two can be helpful if you only think about the actual date information and not the time information.
Creates a LocalDate
// Create a default date LocalDate lDate = LocalDate.now(); // Creates a date from values lDate = LocalDate.of(2017, 12, 15); // create a date from string lDate = LocalDate.parse("2017-12-15"); // creates a date from zone LocalDate.now(ZoneId.systemDefault());
Creates a LocalDateTime
// Create a default date time LocalDateTime lDateTime = LocalDateTime.now(); // Creates a date time from values lDateTime = LocalDateTime.of(2017, 12, 15, 11, 30); // create a date time from string lDateTime = LocalDateTime.parse("2017-12-05T11:30:30"); // create a date time from zone LocalDateTime.now(ZoneId.systemDefault());
LocalDate to Date and vice-versa
Date date = Date.from(Instant.now()); ZoneId defaultZoneId = ZoneId.systemDefault(); // Date to LocalDate LocalDate localDate = date.toInstant().atZone(defaultZoneId).toLocalDate(); // LocalDate to Date Date.from(localDate.atStartOfDay(defaultZoneId).toInstant());
LocalDateTime to Date and vice-versa
Date date = Date.from(Instant.now()); ZoneId defaultZoneId = ZoneId.systemDefault(); // Date to LocalDateTime LocalDateTime localDateTime = date.toInstant().atZone(defaultZoneId).toLocalDateTime(); // LocalDateTime to Date Date out = Date.from(localDateTime.atZone(defaultZoneId).toInstant());