I am working on android project. I am setting date and time but it is displaying in the following format.
Mon Nov 19 11:00:00 GMT+05:30 2012
In my database table the datetime column datatype is datetime. So how can I change the above output to datetime format to store it in database.
Any help in this regard will be thankful.
you should probably read a bit about SimpleDateFormat, it's the way to parse String into Date.
the way to do this is to create a pattern for the formatter, then create the formatter and after that parse the Strings.
String pattern = "EEE, dd MMM yyyy HH:mm:ss z";
SimpleDateFormat format = new SimpleDateFormat(pattern);
Date myDate = format.parse(str);
Related
I have a requirement, wherein column having date time information must be compared against present date & time. If earlier then this particular record must be deleted.
I have progressed with below implementation, but not getting any error & expected behavior is not met.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault());
Calendar nowCalendar = Calendar.getInstance();
String strNowTime = dateFormat.format(nowCalendar.getTime());
// Delete the records at Manager_Dashboard SQLiteDB
String whereClause = "DATETIME(End_Time) < DATETIME(?)";
String whereArgs[] = {strNowTime};
dbOfflineRange.delete("Offline_Range",whereClause,whereArgs);
As you could notice, if I have a record with End_Time in text within SQLite table (as 30 Nov 2018 21:35), then this should be compared against current datetime (as 30 Nov 2018 23:35)
As End_Time being earlier than Now, I am expecting deletion. But its not happening.
The Date and Time SQL functions require the date to be in a recognised format.
Time Strings A time string can be in any of the following formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
SQL As Understood By SQLite - Date And Time Functions
Your best(simplest) option would be to store them as YYYY-MM-DD HH:MM (i.e. change the SimpleDateFormat accordingly).
Note you would not even need the datetime function as direct comparison of the values would result in the desired outcome.
Storing the unix timestamp value would be the more efficient storage/processing time wise.
You can then use the strftime function to return the date in the format you wish.
Otherwise you can convert the store value to a recognised format using SQL functions such as substr and then do the comparison.
SQL As Understood By SQLite - Core Functions
here's an example of how you could handle data stored in DD MMM YYYY HH:MM format
assuming that the value to compare against is provided in YYYYMMDDHH:MM format :-
DROP TABLE IF EXISTS Offline_Range;
CREATE TABLE IF NOT EXISTS Offline_Range (end_time);
-- Add some test data dd MMM yyyy HH:mm
INSERT INTO Offline_Range VALUES
('21 May 2018 10:30'),
('21 Jun 2018 10:30'),
('21 Jul 2018 10:30'),
('21 Aug 2018 10:30')
;
SELECT * FROM Offline_Range
WHERE
substr(end_time,8,4)||
CASE
WHEN instr(end_time,'Jan') THEN '01'
WHEN instr(end_time,'Feb') THEN '02'
WHEN instr(end_time,'Mar') THEN '03'
WHEN instr(end_time,'Apr') THEN '04'
WHEN instr(end_time,'May') THEN '05'
WHEN instr(end_time,'Jun') THEN '06'
WHEN instr(end_time,'Jul') THEN '07'
WHEN instr(end_time,'Aug') THEN '08'
WHEN instr(end_time,'Sep') THEN '09'
WHEN instr(end_time,'Oct') THEN '10'
WHEN instr(end_time,'Nov') THEN '11'
WHEN instr(end_time,'Dec') THEN '12'
END
||
substr(end_time,1,2)||substr(14,5)
<
'2018062200:00' -- 22 Jun 2018 00:00 amended to suit i.e. the value input
This creates a table :-
The result from the query would be :-
I have stored dates' date as String in my database in the following format :
dateFormater = new SimpleDateFormat("dd MMM yyyy");
Now i need to access data between the two dates startWeekDate and endWeekDate both formatted as above. Currently , the where clause of my query is :
at_date BETWEEN Datetime( '"+startWeekDate+"') AND Datetime( '"+endWeekDate+"')
Where startWeekDate is '10 Aug 2014' and endWeekDate is '16 Aug 2014'. But I am getting the null result on my TextView. Can any one guide me how can i do this.
This is not one of the supported date formats.
Just use yyyy-MM-dd.
I have a table in SQLITE with a DATETIME column.
I do a SQL statement which populates it with now()
I want to retreive it and parse it as a Date object in java, with following code:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat
("yyyy-MM-ddHH:mm:ss",Locale.ENGLISH);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
then I get the date via:
Date d = simpleDateFormat.parse
(recordset.getString(recordset.getColumnIndex("storedate")));
I get parse exception: unparceable date (and I guess it has to do with the format. Anyone can tell me which format it should be or where the error is?
Try this :
simpleDateFormat.format(
new Date(
recordset.getString(
recordset.getColumnIndex("storedate")
)));
I found out what it was. I had to change the format to this one:
"EEE MMM dd HH:mm:ss Z yyyy"
now it works. Guess SQLite is storing datetime objects in this format by default.
I have a twitter feed in setup in my app.
But it the date isn't formatted right.
It now displays like this : Sat, 25 feb 2012 22:39:32 +0000 but I would like it to be in dutch and like this: 22:39u 25 februari 2012 (Dutch format and naming).
Or maybe if possible: "about 2 hours ago"
I used the code of this tutorial:
http://codehenge.net/blog/2011/05/android-programming-tutorial-a-simple-twitter-feed-reader/
And I added:
((JSONObject)t).get("profile_image_url").toString()
For those who are still looking for solution:
Step-1 First we need to convert the twitter feed format to Date Obj. Like from twitter JSON response we are getting the created at value as:--
Sat Apr 02 17:14:28 +0000 2016
The corresponding format will be "EEE MMM dd hh:mm:ss Z yyyy"
Refer here for how to use formatting pattern letters
So the code to cobert it to date object will be:
//Existing Format
SimpleDateFormat createdDateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss Z yyyy");;
Date dateObj = createdDateFormat.parse(date);
Step-2 Convert this date Obj to your required format using new format pattern as below:
//changing to new format
createdDateFormat = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
date = createdDateFormat.format(dateObj);
The final output is:-
Apr 03,2016 03:06 AM
As the comments say on that page, you need to use SimpleDateFormat
I have this date in string:
"2011-08-28 08:30:00 +0000"
I want this to convert to a java.util.Date in hungarian Locale, so I try to use this formatter:
DateFormat currentDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", new Locale("hu"));
I am expecting that with currentDateFormat.parse I get "2011-08-28 10:30:00" as date (Hungary is GMT+2) but it is still "2011-08-28 08:30:00". I've tried to use setTimeZone(TimeZone.getDefault()) but didn't help.
Any ideas?
Android only uses UTC, no more GMT.
The problem is the date you have there is not clearly UTC and Android doesn't know what to do with it so it's default behavior is to just assume your date is in the current time zone.