Java Twitter Timestamp to Unix Timestamp

The Twitter API is very unorganized, everything is in obscure formats that need to be converted into something that can easily be worked with.
The Date stamp returned on all tweets is in a format that is not easily search able through a database, or compared to.

Here is a simple method that will convert the Twitter time string to a unix timestamp:

  public long twitterStampToTimestamp(String twitterStamp){
    long timestamp = 0;
    try {
      DateFormat formatter ;
      Date date;
      formatter = new SimpleDateFormat("EEE MMM d kk:mm:ss Z yyyy");
      date = (Date)formatter.parse(twitterStamp);
      java.sql.Timestamp timeStampDate = new Timestamp(date.getTime());
      timestamp = date.getTime() / 1000;
    } catch (Exception e){e.printStackTrace();}
    return timestamp;

That should help anyone who needs to convert the twitter timestamp to a unix timestamp in Java.

Also, this can easily be adapted to other languages, as the main structure is found in the Date format: "EEE MMM d kk:mm:ss Z yyyy"

Java Parsing Twitter Tweets

Another method I needed for my Twitter Client I am developing was a way to parse the tweets. By this, I mean make links, hashtags and usernames clickable.

I did this using Regex.
To make use of the regex library, you will need to import:

import java.util.regex.*;

Here is the full code, pass in the tweet, and it will return the tweet with the proper linking.

  public String parseTweet(String inTweet){
    String patternStr = "(?:\\s|\\A)[##]+([A-Za-z0-9-_]+)";
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inTweet);
    String foundValue = "";
    //hash tags
    while (matcher.find()){
      foundValue =;
      foundValue = foundValue.replace(" ","");
      inTweet = inTweet.replace(foundValue, "<a href='" + foundValue + "'>" + foundValue + "</a>");
    patternStr = "(?:\\s|\\A)[@]+([A-Za-z0-9-_]+)";
    pattern = Pattern.compile(patternStr);
    matcher = pattern.matcher(inTweet);
    while (matcher.find()){
      foundValue =;
      foundValue = foundValue.replace(" ","");
      String rawName = foundValue.replace("@","");
      inTweet = inTweet.replace(foundValue, "<a href='" + rawName + "'>" + foundValue + "</a>");
    patternStr = "(^|[ \t\r\n])((ftp|http|https|mailto|aim|webcal|skype):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))";
    pattern = Pattern.compile(patternStr);
    matcher = pattern.matcher(inTweet);
    while (matcher.find()){
      foundValue =;
      foundValue = foundValue.replace(" ","");
      inTweet = inTweet.replace(foundValue, "<a href='" + foundValue + "' target='_blank'>" + foundValue + "</a>");
    return inTweet;

And thats it! One method to handle everything.

Java XML to Map Class (XML2MAP)

Wow, It has been such a long time since I made a post here!
I am going to try and start posting more often instead of letting this blog die out.

I have been developing my own Twitter client, and I needed a simple way to access elements from an XML document. Unlike PHP, Java can not use strings as array indexes, so I took the route of maps. I made this class to convert an XML file to a Map, where you can access the individual elements by using the key name. This is ONLY useful if the XML document only has one set of elements, as they will be overwritten.

You need the Org.Json library from for this to work.

import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.json.*;
public class XMLToMap{
  String inXML;
  public XMLToMap(String inXML){
    this.inXML = inXML;
  public Map toMap(){
    Map<String, String> xmlmap = new HashMap<String, String>();
    ByteArrayInputStream bs = new ByteArrayInputStream(this.inXML.getBytes());
    try {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(bs);
      NodeList mainNode = (NodeList)doc.getDocumentElement();
      Node fstNode = (Node)mainNode;
      NodeList list = ((Element)fstNode).getElementsByTagName("*");
      Element fstNmElmnt;
      String elementValue = "";
      for (int i=0; i
<list.getLength(); i++) {
        Element element = (Element)list.item(i);
          fstNmElmnt = (Element) ((NodeList)((Element)fstNode).getElementsByTagName(element.getNodeName())).item(0);
          elementValue = ((Node) fstNmElmnt.getChildNodes().item(0)).getNodeValue();
          xmlmap.put(element.getNodeName(), elementValue);
        }catch(Exception e){}
    } catch (Exception e) {
    return xmlmap;

How do you use it?
Declare a new XMLToMap object and pass the full XML document to the constructor.

XMLToMap xmlmap = new XMLToMap(fullXMLDocument);

Then call tomap and place the data back into a map object, and then you can access whatever elements you need:

Map mymap = xmlmap.toMap();

Now, I am sure this can be implemented in a much cleaner way, but it worked perfectly in this way for what I needed it for.

Free Best Buy Gift Card

I just came across this where you can get a free $1000 Best Buy gift card. Check it out: Free Best Buy Gift Card.