Twitter with Java

Get list of tweets (timeline)

Requires

Imports

import java.net.URL;
 
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.json.JSONArray;
import org.json.JSONObject;

Code

String username = "google";	// update
 
// get tweets as JSON
URL url = new URL("https://twitter.com/statuses/user_timeline/" + username + ".json");
ByteArrayOutputStream urlOutputStream = new ByteArrayOutputStream();
IOUtils.copy(url.openStream(), urlOutputStream);
String urlContents = urlOutputStream.toString();
 
// parse JSON
JSONArray jsonArray = new JSONArray(urlContents); 
 
// use
for (int i = 0; i < jsonArray.length(); i++) {
	JSONObject jsonObject = jsonArray.getJSONObject(i);
 
	System.out.println(jsonObject.getString("id"));
	System.out.println(jsonObject.getString("text"));				
	System.out.println(jsonObject.getString("created_at"));		
}

Convert Twitter date to human friendly string

Example: from “Wed Feb 17 20:30:11 +0000 2010” to “yesterday”

Imports

import java.text.SimpleDateFormat;
import java.util.Date;

Code

	public static String twitterHumanFriendlyDate(String dateStr) {
		// parse Twitter date
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"EEE MMM dd HH:mm:ss ZZZZZ yyyy", Locale.ENGLISH);
		dateFormat.setLenient(false);
		Date created = null;
		try {
			created = dateFormat.parse(dateStr);
		} catch (Exception e) {
			return null;
		}
 
		// today
		Date today = new Date();
 
		// how much time since (ms)
		Long duration = today.getTime() - created.getTime();
 
		int second = 1000;
		int minute = second * 60;
		int hour = minute * 60;
		int day = hour * 24;
 
		if (duration < second * 7) {
			return "right now";
		}
 
		if (duration < minute) {
			int n = (int) Math.floor(duration / second);
			return n + " seconds ago";
		}
 
		if (duration < minute * 2) {
			return "about 1 minute ago";
		}
 
		if (duration < hour) {
			int n = (int) Math.floor(duration / minute);
			return n + " minutes ago";
		}
 
		if (duration < hour * 2) {
			return "about 1 hour ago";
		}
 
		if (duration < day) {
			int n = (int) Math.floor(duration / hour);
			return n + " hours ago";
		}
		if (duration > day && duration < day * 2) {
			return "yesterday";
		}
 
		if (duration < day * 365) {
			int n = (int) Math.floor(duration / day);
			return n + " days ago";
		} else {
			return "over a year ago";
		}
	}

Note: logic from the the Twitter Profile Widget Javascript file.

Advanced

For a more advanced use (updating status, search, etc), use Twitter4J

 

Feedback

Unparseable date: "Thu Apr 29 01:29:38 +0000 2010"
Victor Nunes
Apr 29, 2010
#1
I think this pattern don't be able to parse this kind of date.
=/

but your code turns me on again, i'm trying very hard to convert this f... twitter date in java.util.Date but i have no success yet.

anyway, thanks a lot
Victor Nunes
Apr 29, 2010
#2
Victor, "Sat May 01 15:36:50 +0000 2010" works with twitterHumanFriendlyDate().
Jérôme Jaglale
May 5, 2010
#3
Very nice! Worked fine!
Karol Nunes
May 7, 2010
#4
if your locale is not ENGLISH, it will fail.
Insert Locale.ENGLISH

ex)
DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
hagun
Dec 29, 2010
#5
this is great!!

nice job!
Fabian Reyes
Sep 26, 2011
#6
Thank you. Really helpfull.

For the non english, do not forget like hagun said to put the local format English.

I also recommend to replace the int type by a long. I had some issues beacause of that. Now, everything great.
Kerwan
Dec 22, 2011
#7
I think the pattern is "MMM dd, yyyy h:mm:ss a". This pattern work it
fedeguede
Mar 14, 2012
#8
Thanks a lot
Nandu
Mar 14, 2013
#9
Actually twitter date pattern is "EEE, d MMM yyyy HH:mm:ss Z".
thanks..it very usefull.....
Nandu
Mar 14, 2013
#10
Thank you, Nandu and Jerome.
I just needed to convert it to java.util.Date, so I tried if someone already found a solution. Better than coding some kind of parser on my own.
Vlasec
Jul 8, 2013
#11