Convert String to SQL date in java 8 [2021]
In this tutorial, we will see how to convert string to SQL date in Java 8 with an example using the SQL Date class method.
Java SQL Date Example: Get Current Date
Let's see an example of how to get the current date using java SQL date.
Example:
import java.sql.Date;
public class SqlDateExample {
public static void main(String[] args) {
long milliseconds = System.currentTimeMillis();
Date currentDate = new Date(milliseconds);
System.out.println(currentDate);
}
}
Output:
2021-07-23
String to SQL date in java 8
To Convert Java String to SQL date we will use java.sql.Date.valueOf(String s) method.
Example:
import java.sql.Date;
public class StringToSqlDate {
public static void main(String[] args) {
String stringDate = "2021-07-23";
System.out.println("String format date : " + stringDate);
Date sqlDate = Date.valueOf(stringDate);
System.out.println("String to sql date : " + sqlDate);
}
}
Output:
String format date : 2021-07-23 String to sql date : 2021-07-23
Conclusion:
In this quick tutorial, we have covered how to convert String to SQL date in Java 8.
Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.
Happy Learning!!!
More Examples you may like
Comments
Post a Comment
If you have any doubts, Please let me know.