
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How do I create a java.sql.Date object in Java?
Using the Constructor
The java.sql.Date represents the date value in JDBC. The constructor of this class accepts a long value representing the desired date and creates respective Date object.
Date(long date)
You can create this object using this constructor.
Example
import java.text.ParseException; import java.text.SimpleDateFormat; public class Demo { public static void main(String args[]) throws ParseException { String str = "26-09-1989"; SimpleDateFormat obj = new SimpleDateFormat("dd-MM-yyyy"); long epoch = obj.parse(str).getTime(); System.out.println("Date value: "+epoch); //Creating java.util.Date object java.util.Date date = new java.util.Date(epoch); System.out.println(date); } }
Output
Date value: 622751400000 Tue Sep 26 00:00:00 IST 1989
Using the valueOf() method
The valueOf() method of this class has two variants as shown below −
- valueOf(LocalDate date);
- valueOf(String s);
This method accepts a LocalDate object or a date string value (yyyy-[m]m-[d]d format) representing a desired date and creates/returns a java.sql.Date object.
Example
import java.sql.Date; import java.time.LocalDate; public class Demo { public static void main(String args[]) { LocalDate localDate = LocalDate.of(2014, 9, 11); Date date = Date.valueOf(localDate); System.out.println(date); } }
Output
Date Value: 2014-09-11
Example
import java.sql.Date; public class Demo { public static void main(String args[]) { String str = "2017-12-03"; Date date = Date.valueOf(str); System.out.println("Date Value: "+date); } }
Output
yyyy-[m]m-[d]d
- Related Articles
- Java Program to convert from a java.util.Date Object to a java.sql.Date Object
- Convert the Current Time to a java.sql.Date Object
- How to get LocalDateTime object from java.sql.Date using JDBC?
- How do I create a view in MySQL?
- How do I create a Java string from the contents of a file?
- How do you create a Swift Date object?
- How do I create a Python namespace?
- How to create a JSON Object using Object Model in Java?
- How do I create a namespace package in Python?
- How do I create a popup window in Tkinter?
- How do I create a date picker in tkinter?
- How do I create a multidimensional list in Python?
- How do I create a .pyc file in Python?
- How do you create a list in Java?
- How do I look inside a Python object?

Advertisements