I want to insert current date in SQLite database datetime type field.
How can I do this?
UPDATE : Using mDb.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES (datetime()) ");
I am able to insert like date_created=2012-10-10 13:02:08 and I want this format 10 Oct 2012 12:48 how to achive this?
I prefer the following ways to get it done:
Create the table with a default settings to put current datetime
automatically. For example:
ColumnName DATETIME DEFAULT CURRENT_TIMESTAMP
Create SimpleDateFormat and convert current date into SQL format
string. For example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = sdf.format(new Date());
ContentValues values = new ContentValues();
values.put("ColumnName", strDate);
You can use:
mDb.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES (datetime()) ");
check: https://stackoverflow.com/a/819605/1434631
ContentValues coloumnVlues = new ContentValues();
//SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
Date date = new Date();
coloumnVlues.put("fname", fname);
// inserted date in database like May 31, 2013 10:42:53 AM it's simulator datetime
coloumnVlues.put("created_date", DateFormat.getDateTimeInstance(). format(date));
their is no need to use SimpleDateFormat.
you get datetime from database the example given in below link
http://www.sqlite.org/lang_datefunc.html
Related
I am trying to send a date and time to my SQL oracle database through my REST service. However, the field in the SQL database is getting null.
Here is my code to get the date and time:
public void getCurrentDateandTime() throws ParseException {
Calendar c = Calendar.getInstance();
c.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
currentDateandTime = dateFormat.format(c.getTime());
CDAT = dateFormat.parse(currentDateandTime);
}
In my REST service, this field is specified as a date datatype, however my the temporal I am using is TIMESTAMP as I want to get the date and time.
When I use postman sending through test data like this:
"createdTimestamp": "2018-02-12T09:27:39"
It is being received and shown in my SQL database.
Why is the date I am sending from Android receiving null?
Try this buddy:
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
And then send this formattedDate string. Make it string type and then parse your string as datetime object. Hope this works. Happy Coding :)
Most REST APIs stick with iso8601 format as a standard.
The java code would be like:
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
df.setTimeZone(tz);
String nowAsISO = df.format(new Date());
Then in Oracle to get back is:
to_timestamp('2018-02-14T00:00:00.000Z', 'YYYY-MM-DD"T"HH24:MI:SS.ff3"Z"')
When I run this query in sqlite tools I have a correct answer but when I run this query in my android project it returns 0.
Do you know what is problem?
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Calendar strDate = Calendar.getInstance();
Calendar dateAgo = Calendar.getInstance();
dateAgo.add(Calendar.DATE, -7);
String query1 = "SELECT sum(Income.Money) AS 'Report' " +
" FROM Income " +
" WHERE Income.[Date] > '"+dateFormat.format(dateAgo.getTime())+
"' AND Income.[Date] <= '"+dateFormat.format(strDate.getTime())+"'";
I checked all of my code but they don't have any mistake.
MM/dd/yyyy is not a valid Time String (see: https://www.sqlite.org/lang_datefunc.html).
Use this format: yyyy-MM-dd.
I need to subtract an X number of minutes from the current date and format it for a SQLlite query in my Android app. This is what I have so far:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, -60); //one hour back
final SimpleDateFormat std = new SimpleDateFormat("yyyy-mm-dd hh:mm");
std.setCalendar(cal);
String date = std.format(cal.getTime());
String sql = "SELECT * FROM [tbl_name] WHERE [datefield] >= " + date;
datefield is stored as a DATETIME in the SQLlite table. I don't want to use Joda time because I want to keep the number of dependencies in my app to a minimum.
With my current code, the date date variable is coming out as: '2012-43-05 07:43'
You are using minutes twice in your format. mm actually needs to be MM in the date portion.
final SimpleDateFormat std = new SimpleDateFormat("yyyy-MM-dd hh:mm");
I have a requirement that I need to compare two Dates. One Date will come from DB which is String in "YYYY-DD-MM" firm and I need to compare this String Date with current Date.
for this I am converting Date String into Date object.
Now I need current Date also in "YYYY-MM-DD" format and it should be Date object so that I can use.compareTo() method compare two dates..
Please help me how to do that...
Date cDate = new Date();
String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);
You can do it in following way
// pick current system date
Date dt = new Date();
// set format for date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// parse it like
String check = dateFormat.format(dt);
System.out.println("DATE TO FROM DATEBASE " +
arrayOfStringDate[d].toString());
System.out.println("CURRENT DATE " + check);
// and compare like
System.out.println("compare "+
arrayOfStringDate[d].toString().equals(check));
Calendar c = Calendar.getInstance();
SimpleDateFormat tf = new SimpleDateFormat("yyyy-MM-dd");
String time=DB time;
Date parseTime= tf.parse(time);
Integer dayNow=c.getTime().getDate();
Integer dayDb=parseTime.getDate();
then you can compare dayNow and dayDb.
If your current date is actually an instance of the java.util.Date class, you don't need to specify a format for it; it's just a millisecond value that represents a specific moment in time.
You can get the current date like so:
Date currentDate = new Date();
You can use 2 ways:
DateFormat object. Use parse method.
Make your own parser of the Date. I mean, you convert the year, month and day in an integer each, and use Date constructor to get the Date.
in my Android application, i am taking date and time from database. but i am not able to get the date in the "Date" Format from the database into my application, the date is in string format, so i am not able to compare the system date to database date.
if i convert the system date into string then i am not able to update the date into the database in recurring case. i also want to update the database date if the system date and database date is matched.
how can i achieve this is android.
Thanks in advance.
You can convert String to Date like this:
String str = "12/12/1912";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(str);
And back to String
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Date is : " + formatter.format(date));
And Date has before and after methods and can be compared to each other.
By the way there is also a library called Joda, you can also check it out.
Try this code:
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("dd-MMMM");
formattedDate = df.format(c.getTime());