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
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 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);
I have a String this:-
Tue Oct 30 13:22:58 GMT+05:30 2012;
I want to divide time & date from SimpleDateFormate:-
DateFormat f = new SimpleDateFormat("EEE,MM/dd/yyyy hh:mm:ss);
Date d = f.parse(my string);
DateFormat date = new SimpleDateFormat("MM/dd/yyyy");
DateFormat time = new SimpleDateFormat("hh:mm:ss a");
System.out.println("Date: " + date.format(d));
System.out.println("Time: " + time.format(d));
I am getting this Error:-
java.text.ParseException: Unparseable date: "Tue Oct 30 13:22:58 GMT+05:30 2012"
Please Tell me whats the problem.
Thanks,
Deepanker
Your timestamp string does not match your pattern:
Tue Oct 30 13:22:58 GMT+05:30 2012
is no way like (not to mention syntax error in the SimpleDataFormat initialization line):
EEE,MM/dd/yyyy hh:mm:ss
So you need to make the pattern matching input data. All fields supported by SimpleDateFormat are described here.
Are you sure the date is correct??
because I tried to parse it
it gives error on GMT+05:30
just remove the :30 and it will work, see here:
String time = "Tue Oct 30 13:22:58 GMT+05 2012";
long f=Date.parse(time);
System.out.println("Time:" + f);
As it stands, your timezone offset does not match the RFC 822 standard, so I don't think you can directly parse the date without performing some cleansing on it first.
If you just want an offset, then the string should look like:
Tue Oct 30 13:22:58 +0530 2012
Note, there is no "GMT", and no colon within the offset. The corresponding pattern is then:
new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy")
Alternatively, you can specify the timezone:
Tue Oct 30 13:22:58 IST 2012
For which the pattern is:
new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy")
I am stuck in one date format
i want my date should look like this, 18th Mar 2011
and it can be 1st, 2nd,3rd that means i want to resolve for all the aspects
Plz help me out for this ASAP
Thanks in advance to all.
I think you want to change date in a 18th, 2nd, 1st, 3rd dated way, if i am not wrong then you can use simpleDateFormat class to convert date in different formats.
Before using SimpleDateFormat, just refer the SDK documentation: http://developer.android.com/reference/java/text/SimpleDateFormat.html.
To have day number with nd, th, rd (i.e. 2nd, 4th, 3rd, etc.), you can use:
F - day of week in month (Number) - 2 (2nd Wed in July)
(given in the documentation).
For example using SimpleDateFormat:
String dateStr = "03/08/2010";
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
Date dateObj = curFormater.parse(dateStr);
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");
String newDateStr = postFormater.format(dateObj);
This is the term you are looking for: Quantity Strings (Plurals)
here is a link for it in the documentation:
Plurals
Here is a link with examples:
Examples
And One more:
Android Pluralization not working, need help
Hope this helps. so you just need to reformat the string with the date by using the examples provided and thats it.