当我尝试使用sqlite3查询通过JDBC插入的日期时,我看到了意外的结果。这个java代码:
try (Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db")) {
conn.setAutoCommit(true);
try (Statement statement = conn.createStatement()) {
statement.executeUpdate("create table dates (date DATETIME);");
}
try (PreparedStatement insert = conn
.prepareStatement("insert into dates values (?)")) {
insert.setTimestamp(1, Timestamp.valueOf(LocalDateTime.now()));
insert.executeUpdate();
}
}
似乎没有插入正确的日期时间 - 当我通过sqlite3查询时,我无法获得人类可读的值:
sqlite3 test.db "select date, datetime(date, 'unixepoch') from dates;"
1507819362296|
这是有道理的,因为sqlite文档说有效的unix纪元范围是" -62167219200到106751991167" (https://sqlite.org/lang_datefunc.html)。
我应该如何在JDBC和Sqlite中使用日期?
答案 0 :(得分:0)
SQLite中没有正式的日期或时间戳类型,而是由字符串表示。因此,如果您想将当前时间戳插入表中,可以尝试以下操作:
try (Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db")) {
conn.setAutoCommit(true);
try (Statement statement = conn.createStatement()) {
statement.executeUpdate("CREATE TABLE dates (date TEXT);");
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
String ts = sdf.format(timestamp);
try (PreparedStatement ps = conn
.prepareStatement("INSERT INTO dates (date) VALUES (?)")) {
ps.setString(1, ts);
ps.executeUpdate();
}
}
答案 1 :(得分:0)
@Mark W我们一直把sqlite数据库中的日期作为字符串放入,但它们看起来不像10-18-2017这样的字符串就像10182017那样,并且它们都是8个字符长。但当你把它们拉出来时,他们会全身心地打扮起来并转变回到这个10-18-2017。我们已经测试了我们的搜索,并且所有搜索似乎都按照需要使用SELECT * FROM TABLE_NAME WHERE Col_Date> ="来自" AND Col_Date< =" to"下面是使日期不那么漂亮的代码
public void findByDate(View view){
use= false;
// custom dialog /* THIS R.style.DatePickerThem is mandatory to use res/values/styles */
final Dialog dialog = new Dialog(MainActivity.this,R.style.DatePickerTheme);
dialog.setContentView(R.layout.datepickerview);
dialog.setTitle("");
DatePicker picker = dialog.findViewById(R.id.datePicker);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
//picker.updateDate(2017, 10, 13);//year month day
picker.updateDate(mYear, mMonth, mDay);// Keeps Calendar initial view what ever today is!
System.out.println("Month " + mMonth+1);
picker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
//picker.init(Integer.valueOf(mMonth),Integer.valueOf(mDay),Integer.valueOf(mYear), new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker picker, int year, int monthOfYear, int dayOfMonth) {
if (etFromDate.getText().toString().isEmpty()) {
System.out.println("I am Not Empty");
//etFromDate.setText(String.valueOf(monthOfYear+1 ) + String.valueOf(dayOfMonth) + String.valueOf(year));
//v1 = Long.valueOf(etFromDate.getText().toString());
//etFromDate.setText(String.valueOf(v1));
//String theDATE = (monthOfYear+1) + "-" + (dayOfMonth) + "-" + (year);
String searchFrom = (String.valueOf(monthOfYear+1))+(String.valueOf(dayOfMonth))+String.valueOf(year);
if(searchFrom.length()==7){
StringBuilder str = new StringBuilder(searchFrom);
str.insert(2, '0');
etFromDate.setText(str);
}else if (searchFrom.length() == 6){
StringBuilder str = new StringBuilder(searchFrom);
str.insert(0,'0');
str.insert(2,'0');
etFromDate.setText(str);
}else {
etFromDate.setText(searchFrom);
}
//setDATE = etFromDate.getText().toString();
dialog.dismiss();
然后,这里有一种方法可以将同一个字符串10182017视为一个高级时装日期
helper = new DBHelper(this);
dbList = new ArrayList<>();
dbList = helper.getDataFromDB();
if (dbList.size() > 0 && tORf.equalsIgnoreCase("false")) {
btnSave.setVisibility(View.INVISIBLE);
String NVrowid = String.valueOf(dbList.get(position).getRowid());
String NVstation = dbList.get(position).getStation_Name();
String NVpurchasedate = dbList.get(position).getDate_of_Purchase();
String s = NVpurchasedate;
String month = s.substring(0, 2);
String day = s.substring(2, 4);
String year = s.substring(4, 8);
String date3 = month + "-" + day + "-" + year;
String NVcost = dbList.get(position).getGas_Cost();
etStation.setText(NVstation);
etPurchaseDate.setText(date3);
//etPurchaseDate.setText(NVpurchasedate);
etCost.setText(NVcost);
}