sqlite date format for android - android

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.

Related

Compare Dates Stored as String using Datetime

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.

How can I save string type into android SQLite datetime field

I've got following string "2013-04-30T00:55:25.855-07:00" from Google blogger feed. I try to save this string to SQLite datetime field as following command
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
initialValues.put(newsdatemodified, dateFormat.format(datemodified));
But I found that data cannot insert into sqlite table and no encounter any errors. Any solution will be appreciated.
Update
When I've tried to use following coding
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date1 = (Date) dateFormat.parse(entry.updated);
but following this error
05-02 09:56:05.383: E/AndroidRuntime(31634): Caused by: java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
Your format is wrong, it needs to me yyyy-MM-dd'T'HH:mm:ss.SSSZ
as you can see the format you had does not match what the incoming string format was
It seems that issue is with the format. May be, you can try using yyyy-MM-dd'T'HH:mm:ss.SSSZ. SSS stands for fractional seconds and Z stands for time zone.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Hope it helps else please comment. You can see other formats and try to work around with them by seeing here.

How to change string to datetime format in android?

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);

android date toString

I"m following a tutorial where they have this line:
int callDate = c.getInt(dateColumn);
Next, they pass the callDate into the Date object to get a readable format.
But, I think the example may be old, because I get a compile error.
String myDate = DateUtils.dateString(callDate).toString();;
So, I'm looking at the API. how do I do it.
http://developer.android.com/reference/java/util/Date.html
String myString = DateFormat.getDateInstance().format(new Date());
or
Date date = new Date();
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));
Look at DateFormat and SimpleDateFormat.
You might want to use SimpleDateFormat for that
If I have understood your question correctly, you need to get the date in readable format.In that case you already have the answer you just need to read the link http://developer.android.com/reference/java/util/Date.html carefully for finding the answer. Let me provide you few hint for the same, see for the constructors on this link for initializing the date of your choice. The methods given helps you perform the operations on it. To answer your question specifically refer below snippet.
Date d1=new Date(); //Initializes this Date instance to the current time.
String date= d1.toString(); //this String var date can be used for printing date in readable format
the format of string will be "EEE MMM dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00 PDT 1999".
See the below link. It will definitely help.
Simple java date conversion
the problem is that you are getting from a Integer
int callDate = c.getInt(dateColumn);
the date is a Long type and you're losing precision on this conversion.
try changing to: long callDate = c.getLong(dateColumn);

Converting date in string to specified locale on Android

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.

Categories

Resources