Sqlite : Comparing timestamp value stored in a column to current Date - java

I store information in a sqlite database table as follows:
ActionDate column is of type : DEFAULT CURRENT_TIMESTAMP
private String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
I want to write an SQL query that returns all rows in my table having ActionDate value as today's date from sqlite DB. However as ActionDate is of type timestamp what is the most appropriate way to convert it to todays date.

If you want current date then use the following query :
SELECT date('now');
To get all columns in your table having ActionDate equal to todays date use the below query :
select * from table where strftime('%Y-%m-%d ', datetime(ActionDate, 'unixepoch')) = date('now)' ;
more info on SQLite Date & Time queries can be found here

Related

How to convert SQL Date to String in Java?

So I'm building a simple app which shows a list of dates in a ListView.
The dates are being fetched from a database in JSON format.
My database has a column 'AttendanceDate' of 'date' data type.
I think the problem is with 'date' data type of SQL. If so, how do I convert the date ( in 'date') to date ( in 'String').
I tried this but it didn't work:
jsonObject.getString("AttendanceDate").toString();
I also tried to typecast the date to string but that too didn't work:
String date = (String) jsonObject.get("AttemdanceDate");
Link to Json string: http://sasananshul.16mb.com/test.php
Below I have given a code snippet that could help you to change the Date to String format.
//Creating Date in java with today's date.
Date dateNow = new Date();
//change date into string yyyyMMdd format example "20110914"
SimpleDateFormat dateformatyyyyMMdd = new SimpleDateFormat("yyyyMMdd");
String date_to_string = dateformatyyyyMMdd.format(dateNow);
System.out.println("date into yyyyMMdd format: " + date_to_string);

Timestamp not correctly getting inserted into database

I am inserting, through my below java code, a date value into a column of my table called Source.
Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String todayDate = df.format(today).toString();
myVO.setMyDate(todayDate);
myDao.saveOrUpdateVO(myVO);
Later, I am retrieving the date value stored in Source table's column and inserting that date into destination table.
Now, I noticed that the date value stored in Source table is like below,
18-Oct-2015 00:00:0
Whereas the value stored in destination table is - 18-Oct-2015 08:15:0.
Why is this difference? If I want to store date value in Source table just like how it is getting stored in destination, what I need to do?
Datatype of the column in both tables is date default sysdate

Date - Java to Sql table and sql table to Java through Joda Time

I am looking for a way to get today's date and pass to sql table and save there. Call the saved date and do some task with JODA TIME API. The changed Joda time Date to sql table and save there and process continues..
I tried this way,
//prints todays date
java.sql.Date sqlDate = new java.sql.Date(new Date().getTime());
//passes wrong date to the table like 1970-07-01 instead of 2013-03-01
String insert = "INSERT INTO TEST_TABLE VALUES(1,"+sqlDate+")";
pStmt = conn.prepareStatement(insert);
pStmt.executeUpdate();
//converting to joda time
LocalDate ld = new LocalDate(sqlDate);
//some calculations, and how to convert back to sql date?
What I am trying to do here is, A table with 3 columns (id, startdate, finishdate). id will be entered by user, start date should be automatically entered todays date. after some calculations with joda time and finish date will be set to date it is finished.
Code
String insert = "INSERT INTO TEST_TABLE VALUES(2,'"+timestamp+"')";
Error
Data type mismatch in criteria expression
//I have created table using MS access
//the format of the date column is Date/Time.
You Can use Timestamp here. java.sql.Timestamp extends java.util.Date, so anything you can do with a java.util.Date you can also do with a java.sql.Timestamp.
To convert LocalDateTime to Timestamp
Timestamp timestamp = new Timestamp(localDateTime.toDateTime().getMillis());
But if You still want to convert Timestamp into java.sql.Date then use this
java.sql.Date date = new java.sql.Date(timeStamp.getTime());

How to get system date without time-stamp in the data type of Date?

i need to compare two dates
task.deadline have Date data type but when i get system date it gives me in string how to get system date in Date data type
if (!task.deadline.before(systemDate)) {
// add row to to do tab
model_1.addRow(row);
} else {
// add row to done tab
model_2.addRow(row);
}
Parse the systemDate to a Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date sysDate = sdf.parse(systemDate);
Now you can use sysDate in the comparison.

How to find last / 7 /30 days records of sqlite db when datetime is stored as text?

I have an activity where i save some info to my database.
I have two textviews one for date (yyyy-MM-dd) and one for time (HH:mm).
if i save datetime as a TEXT, i can sort then desc properly,but CAN i find with sqlite query last/7/30 days records?An example of this query when are the datetime TEXT?
First you should calculate the date range you want to analize, then convert its boundaries into the text format (you can use some date formatting ustilities). Then you should query the Db with converted dates as parameters.
Suppose you want last 30 days list:
Calendar theEnd = Calendar.getInstance();
Calendar theStart = (Calendar) theEnd.clone();
theStart.add(Calendar.DAY_OF_MONTH, -30);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String start = dateFormat.format(theStart.getTime());
String end = dateFormat.format(theEnd.getTime());
// Now you have date boundaries in TEXT format
Cursor cursor = db.rawQuery("SELECT * FROM table WHERE timestamp BETWEEN "+start+" AND "+end);

Categories