Convert String To Java.sql.Date
After looking through many of the threads in stackoverflow and cant seem to find the correct method im looking for. If anyone could shine a bit of light it would be great.
So I populated a JCombo Box with SQL Dates but in doing so I formatted them to using SimpleDateFormat which converted them into a string with the format dd/MM/yyyy.
When the program is running he can select a certain date to query the database and so here the problem
Ive managed to make a String with a format of yyyy/MM/dd using string.split saving it into an array and ordering them in another string
Is There A Way To Convert This String (2014/04/13) into an SQL Date so I can query my database OR am I going about it incorrect
Thank You,
java mysql string date jdbc
add a comment |
After looking through many of the threads in stackoverflow and cant seem to find the correct method im looking for. If anyone could shine a bit of light it would be great.
So I populated a JCombo Box with SQL Dates but in doing so I formatted them to using SimpleDateFormat which converted them into a string with the format dd/MM/yyyy.
When the program is running he can select a certain date to query the database and so here the problem
Ive managed to make a String with a format of yyyy/MM/dd using string.split saving it into an array and ordering them in another string
Is There A Way To Convert This String (2014/04/13) into an SQL Date so I can query my database OR am I going about it incorrect
Thank You,
java mysql string date jdbc
add a comment |
After looking through many of the threads in stackoverflow and cant seem to find the correct method im looking for. If anyone could shine a bit of light it would be great.
So I populated a JCombo Box with SQL Dates but in doing so I formatted them to using SimpleDateFormat which converted them into a string with the format dd/MM/yyyy.
When the program is running he can select a certain date to query the database and so here the problem
Ive managed to make a String with a format of yyyy/MM/dd using string.split saving it into an array and ordering them in another string
Is There A Way To Convert This String (2014/04/13) into an SQL Date so I can query my database OR am I going about it incorrect
Thank You,
java mysql string date jdbc
After looking through many of the threads in stackoverflow and cant seem to find the correct method im looking for. If anyone could shine a bit of light it would be great.
So I populated a JCombo Box with SQL Dates but in doing so I formatted them to using SimpleDateFormat which converted them into a string with the format dd/MM/yyyy.
When the program is running he can select a certain date to query the database and so here the problem
Ive managed to make a String with a format of yyyy/MM/dd using string.split saving it into an array and ordering them in another string
Is There A Way To Convert This String (2014/04/13) into an SQL Date so I can query my database OR am I going about it incorrect
Thank You,
java mysql string date jdbc
java mysql string date jdbc
asked Apr 13 '14 at 19:12
OMUIRIOMUIRI
1613
1613
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Is There A Way To Convert This String (2014/04/13) into an SQL Date?
Sample code:
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
try {
java.util.Date utilDate = format.parse("2014/04/13");
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println(sqlDate);
} catch (ParseException e) {
e.printStackTrace();
}
output:
2014-04-13
use
SimpleDateFormatto parse a string intojava.util.Date.
use java.sql.Date(miliseconds) constructor.
Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again
– OMUIRI
Apr 13 '14 at 19:30
yes you can do it in same mannerjava.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());
– Braj
Apr 13 '14 at 19:32
I logic behind is the milliseconds that is used in constructor of both the classes.
– Braj
Apr 13 '14 at 19:33
getTime()returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
– Braj
Apr 13 '14 at 19:34
Is there any date in your database that is older than January 1, 1970?
– Braj
Apr 13 '14 at 19:34
|
show 4 more comments
Use java.util.SimpleDateFormat
An example here:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
java.lang.Date langDate = sdf.parse("2014/04/13");
java.sql.Date sqlDate = new java.sql.Date(langDate.getTime());
Sorry you have usedmmin place ofMM. That is wrong. I have already posted it. Please look into again.
– Braj
Apr 13 '14 at 19:19
FYI, the terribly-designedjava.sql.Dateclass has been supplanted byjava.time.LocalDate.
– Basil Bourque
Nov 15 '18 at 5:44
add a comment |
tl;dr
myPreparedStatement.setObject(
… ,
LocalDate.parse(
"23/01/2018" ,
DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
)
) ;
java.time
The modern approach uses the java.time classes that supplanted the terrible Date/Calendar/SimpleDateFormat classes.
The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.
Define a formatting pattern to match your input strings.
String input = "23/01/2018" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate localDate = LocalDate.parse( input , f ) ;
String output = localDate.toString() ; // Generate text in standard ISO 8601 format.
JDBC 4.2
As of JDBC 4.2, we can exchange java.time objects with the database. Use smart objects rather than dumb text for database interactions.
myPreparedStatement.setObject( … , localDate ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23047200%2fconvert-string-to-java-sql-date%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is There A Way To Convert This String (2014/04/13) into an SQL Date?
Sample code:
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
try {
java.util.Date utilDate = format.parse("2014/04/13");
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println(sqlDate);
} catch (ParseException e) {
e.printStackTrace();
}
output:
2014-04-13
use
SimpleDateFormatto parse a string intojava.util.Date.
use java.sql.Date(miliseconds) constructor.
Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again
– OMUIRI
Apr 13 '14 at 19:30
yes you can do it in same mannerjava.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());
– Braj
Apr 13 '14 at 19:32
I logic behind is the milliseconds that is used in constructor of both the classes.
– Braj
Apr 13 '14 at 19:33
getTime()returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
– Braj
Apr 13 '14 at 19:34
Is there any date in your database that is older than January 1, 1970?
– Braj
Apr 13 '14 at 19:34
|
show 4 more comments
Is There A Way To Convert This String (2014/04/13) into an SQL Date?
Sample code:
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
try {
java.util.Date utilDate = format.parse("2014/04/13");
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println(sqlDate);
} catch (ParseException e) {
e.printStackTrace();
}
output:
2014-04-13
use
SimpleDateFormatto parse a string intojava.util.Date.
use java.sql.Date(miliseconds) constructor.
Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again
– OMUIRI
Apr 13 '14 at 19:30
yes you can do it in same mannerjava.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());
– Braj
Apr 13 '14 at 19:32
I logic behind is the milliseconds that is used in constructor of both the classes.
– Braj
Apr 13 '14 at 19:33
getTime()returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
– Braj
Apr 13 '14 at 19:34
Is there any date in your database that is older than January 1, 1970?
– Braj
Apr 13 '14 at 19:34
|
show 4 more comments
Is There A Way To Convert This String (2014/04/13) into an SQL Date?
Sample code:
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
try {
java.util.Date utilDate = format.parse("2014/04/13");
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println(sqlDate);
} catch (ParseException e) {
e.printStackTrace();
}
output:
2014-04-13
use
SimpleDateFormatto parse a string intojava.util.Date.
use java.sql.Date(miliseconds) constructor.
Is There A Way To Convert This String (2014/04/13) into an SQL Date?
Sample code:
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
try {
java.util.Date utilDate = format.parse("2014/04/13");
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println(sqlDate);
} catch (ParseException e) {
e.printStackTrace();
}
output:
2014-04-13
use
SimpleDateFormatto parse a string intojava.util.Date.
use java.sql.Date(miliseconds) constructor.
edited Apr 13 '14 at 19:27
answered Apr 13 '14 at 19:15
BrajBraj
40.7k53959
40.7k53959
Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again
– OMUIRI
Apr 13 '14 at 19:30
yes you can do it in same mannerjava.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());
– Braj
Apr 13 '14 at 19:32
I logic behind is the milliseconds that is used in constructor of both the classes.
– Braj
Apr 13 '14 at 19:33
getTime()returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
– Braj
Apr 13 '14 at 19:34
Is there any date in your database that is older than January 1, 1970?
– Braj
Apr 13 '14 at 19:34
|
show 4 more comments
Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again
– OMUIRI
Apr 13 '14 at 19:30
yes you can do it in same mannerjava.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());
– Braj
Apr 13 '14 at 19:32
I logic behind is the milliseconds that is used in constructor of both the classes.
– Braj
Apr 13 '14 at 19:33
getTime()returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
– Braj
Apr 13 '14 at 19:34
Is there any date in your database that is older than January 1, 1970?
– Braj
Apr 13 '14 at 19:34
Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again
– OMUIRI
Apr 13 '14 at 19:30
Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again
– OMUIRI
Apr 13 '14 at 19:30
yes you can do it in same manner
java.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());– Braj
Apr 13 '14 at 19:32
yes you can do it in same manner
java.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());– Braj
Apr 13 '14 at 19:32
I logic behind is the milliseconds that is used in constructor of both the classes.
– Braj
Apr 13 '14 at 19:33
I logic behind is the milliseconds that is used in constructor of both the classes.
– Braj
Apr 13 '14 at 19:33
getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.– Braj
Apr 13 '14 at 19:34
getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.– Braj
Apr 13 '14 at 19:34
Is there any date in your database that is older than January 1, 1970?
– Braj
Apr 13 '14 at 19:34
Is there any date in your database that is older than January 1, 1970?
– Braj
Apr 13 '14 at 19:34
|
show 4 more comments
Use java.util.SimpleDateFormat
An example here:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
java.lang.Date langDate = sdf.parse("2014/04/13");
java.sql.Date sqlDate = new java.sql.Date(langDate.getTime());
Sorry you have usedmmin place ofMM. That is wrong. I have already posted it. Please look into again.
– Braj
Apr 13 '14 at 19:19
FYI, the terribly-designedjava.sql.Dateclass has been supplanted byjava.time.LocalDate.
– Basil Bourque
Nov 15 '18 at 5:44
add a comment |
Use java.util.SimpleDateFormat
An example here:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
java.lang.Date langDate = sdf.parse("2014/04/13");
java.sql.Date sqlDate = new java.sql.Date(langDate.getTime());
Sorry you have usedmmin place ofMM. That is wrong. I have already posted it. Please look into again.
– Braj
Apr 13 '14 at 19:19
FYI, the terribly-designedjava.sql.Dateclass has been supplanted byjava.time.LocalDate.
– Basil Bourque
Nov 15 '18 at 5:44
add a comment |
Use java.util.SimpleDateFormat
An example here:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
java.lang.Date langDate = sdf.parse("2014/04/13");
java.sql.Date sqlDate = new java.sql.Date(langDate.getTime());
Use java.util.SimpleDateFormat
An example here:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
java.lang.Date langDate = sdf.parse("2014/04/13");
java.sql.Date sqlDate = new java.sql.Date(langDate.getTime());
edited Jun 24 '18 at 15:34
Kurohige
5981520
5981520
answered Apr 13 '14 at 19:15
AndresAndres
8,57732547
8,57732547
Sorry you have usedmmin place ofMM. That is wrong. I have already posted it. Please look into again.
– Braj
Apr 13 '14 at 19:19
FYI, the terribly-designedjava.sql.Dateclass has been supplanted byjava.time.LocalDate.
– Basil Bourque
Nov 15 '18 at 5:44
add a comment |
Sorry you have usedmmin place ofMM. That is wrong. I have already posted it. Please look into again.
– Braj
Apr 13 '14 at 19:19
FYI, the terribly-designedjava.sql.Dateclass has been supplanted byjava.time.LocalDate.
– Basil Bourque
Nov 15 '18 at 5:44
Sorry you have used
mm in place of MM. That is wrong. I have already posted it. Please look into again.– Braj
Apr 13 '14 at 19:19
Sorry you have used
mm in place of MM. That is wrong. I have already posted it. Please look into again.– Braj
Apr 13 '14 at 19:19
FYI, the terribly-designed
java.sql.Date class has been supplanted by java.time.LocalDate.– Basil Bourque
Nov 15 '18 at 5:44
FYI, the terribly-designed
java.sql.Date class has been supplanted by java.time.LocalDate.– Basil Bourque
Nov 15 '18 at 5:44
add a comment |
tl;dr
myPreparedStatement.setObject(
… ,
LocalDate.parse(
"23/01/2018" ,
DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
)
) ;
java.time
The modern approach uses the java.time classes that supplanted the terrible Date/Calendar/SimpleDateFormat classes.
The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.
Define a formatting pattern to match your input strings.
String input = "23/01/2018" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate localDate = LocalDate.parse( input , f ) ;
String output = localDate.toString() ; // Generate text in standard ISO 8601 format.
JDBC 4.2
As of JDBC 4.2, we can exchange java.time objects with the database. Use smart objects rather than dumb text for database interactions.
myPreparedStatement.setObject( … , localDate ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
add a comment |
tl;dr
myPreparedStatement.setObject(
… ,
LocalDate.parse(
"23/01/2018" ,
DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
)
) ;
java.time
The modern approach uses the java.time classes that supplanted the terrible Date/Calendar/SimpleDateFormat classes.
The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.
Define a formatting pattern to match your input strings.
String input = "23/01/2018" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate localDate = LocalDate.parse( input , f ) ;
String output = localDate.toString() ; // Generate text in standard ISO 8601 format.
JDBC 4.2
As of JDBC 4.2, we can exchange java.time objects with the database. Use smart objects rather than dumb text for database interactions.
myPreparedStatement.setObject( … , localDate ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
add a comment |
tl;dr
myPreparedStatement.setObject(
… ,
LocalDate.parse(
"23/01/2018" ,
DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
)
) ;
java.time
The modern approach uses the java.time classes that supplanted the terrible Date/Calendar/SimpleDateFormat classes.
The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.
Define a formatting pattern to match your input strings.
String input = "23/01/2018" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate localDate = LocalDate.parse( input , f ) ;
String output = localDate.toString() ; // Generate text in standard ISO 8601 format.
JDBC 4.2
As of JDBC 4.2, we can exchange java.time objects with the database. Use smart objects rather than dumb text for database interactions.
myPreparedStatement.setObject( … , localDate ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
tl;dr
myPreparedStatement.setObject(
… ,
LocalDate.parse(
"23/01/2018" ,
DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
)
) ;
java.time
The modern approach uses the java.time classes that supplanted the terrible Date/Calendar/SimpleDateFormat classes.
The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.
Define a formatting pattern to match your input strings.
String input = "23/01/2018" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate localDate = LocalDate.parse( input , f ) ;
String output = localDate.toString() ; // Generate text in standard ISO 8601 format.
JDBC 4.2
As of JDBC 4.2, we can exchange java.time objects with the database. Use smart objects rather than dumb text for database interactions.
myPreparedStatement.setObject( … , localDate ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
answered Nov 15 '18 at 5:42
Basil BourqueBasil Bourque
113k29388547
113k29388547
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23047200%2fconvert-string-to-java-sql-date%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown