例如,我正在尝试获取两个不同日期之间的交易记录 使用以下查询从 javafx 日期选择器获取 2024-07-01 至 2024-08-20 期间的销售记录
SELECT * FROM customerreceipt WHERE date Between '2024-08-01 00:00' AND '2024-08-20 00:00';
因此,当执行查询时,它不会返回任何记录,而当我解析日期时
sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
它会抛出异常
java.text.ParseException:无法解析的日期:“2024-07-29”
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
Date date3 = sdf1.parse(fromDatePicker.getValue().toString());
// sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
String dateStr1 = sdf1.format(date3);
// String dateStr2 = toDatePicker.getValue().toString();
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
Date date4 = sdf2.parse(toDatePicker.getValue().toString());
// sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
String dateStr2 = sdf2.format(date4);
使用
DATE_FORMAT()
函数过滤%Y-%m-%d %H:%i:%s
格式的数据值。
哪里
例如:我在
db<>fiddler示例中使用
customerreceipt
,其中 transaction_time
是 customerreceipt
表的日期时间列。那么您可以使用如下查询:
select * from customerreceipt
where DATE_FORMAT(transaction_time, '%Y-%m-%d %H:%i:%s')
between '2021-01-12 05:49:02' and '2021-01-18 00:06:01'
order by DATE_FORMAT(transaction_time, '%Y-%m-%d %H:%i:%s') desc;
select * from customerreceipt
where DATE_FORMAT(transaction_time, '%Y-%m-%d %H:%i')
between '2021-01-12 05:49' and '2021-01-18 00:06'
order by DATE_FORMAT(transaction_time, '%Y-%m-%d %H:%i:%s') desc;
select * from customerreceipt
where DATE_FORMAT(transaction_time, '%Y-%m-%d')
between '2021-01-12' and '2021-01-18'
order by DATE_FORMAT(transaction_time, '%Y-%m-%d %H:%i:%s') desc;
示例查询:db<>fiddler
检查查询并查看输出的差异