When you retrieve a Date/Time field value via UCanAccess you get a java.sql.Timestamp
object. It contains the Date/Time value, but it does not have a format per se.
If you want to represent that Date/Time value in a particular way you just need to format it to your liking. Probably the simplest way to do that is with java.text.SimpleDateFormat
. For example:
try (ResultSet rs = s.executeQuery("SELECT DateJoined FROM Members WHERE MemberID=1")) {
rs.next();
System.out.printf(
" \"raw\" value (via .toString()): %s%n",
rs.getTimestamp(1).toString());
SimpleDateFormat mmddFormat = new SimpleDateFormat("MM-dd");
System.out.printf(
"formatted value (via SimpleDateFormat): %s%n",
mmddFormat.format(rs.getTimestamp(1)));
}
gives us
"raw" value (via .toString()): 2014-01-23 00:00:00.0
formatted value (via SimpleDateFormat): 01-23
(Note that when the UCanAccess page talks about the "Access date format" it is referring to Date/Time literals enclosed in hash marks like this: #11/22/2003 10:42:58 PM#
. However, you should almost never need to include date literals in your queries because you should be using a PreparedStatement
with appropriate .setTimestamp()
parameters.)
Addendum
Similarly, when inserting Date/Time values into the Access database: The formatting of the Date/Time value in Java makes no difference to the way the Date/Time value is stored in Access (provided that it is interpreted correctly), and the way it is displayed in Access is a function of the format settings in Access. For example, if we run the following Java code
try (Statement s = conn.createStatement()) {
s.executeUpdate(
"INSERT INTO tblDates " +
"(ID, mmddyyyy) " +
"VALUES " +
"('literal', #12/25/2014#)"
);
}
SimpleDateFormat mmddyyyyFormat = new SimpleDateFormat("MM/dd/yyyy");
Timestamp mmddyyyyXmas =
new Timestamp(mmddyyyyFormat.parse("12/25/2014").getTime());
SimpleDateFormat ddmmyyyyFormat = new SimpleDateFormat("dd/MM/yyyy");
Timestamp ddmmyyyyXmas =
new Timestamp(ddmmyyyyFormat.parse("25/12/2014").getTime());
SimpleDateFormat yyyymmddFormat = new SimpleDateFormat("yyyy/MM/dd");
Timestamp yyyymmddXmas =
new Timestamp(yyyymmddFormat.parse("2014/12/25").getTime());
try (PreparedStatement ps = conn.prepareStatement(
"INSERT INTO tblDates " +
"(ID, mmddyyyy, ddmmyyyy, yyyymmdd) " +
"VALUES " +
"('parameters', ?, ?, ?)"
)) {
ps.setTimestamp(1, mmddyyyyXmas);
ps.setTimestamp(2, ddmmyyyyXmas);
ps.setTimestamp(3, yyyymmddXmas);
ps.executeUpdate();
}
and then open the Access database with Windows set to use the default formats for "English (United States)" we see
If we change the format settings in Windows to "English (United Kingdom)" then we see
If we want to use a particular format for one or more fields we need to use a specific Format setting in Access, e.g., with something like this
we see
PreparedStatement.setDate()
expects ajava.sql.Date
parameter which does not have a format (and it's not used for doing "output" either) – a_horse_with_no_namesetString()
for dates. usesetDate()
. The formatting of dates should be handled when displaying (=reading) the data in your frontend, not when saving it. – a_horse_with_no_name