0

As you can guess im trying to parse a twitter date so I am able to apply it to a table, the code as to how it looks at the moment:

Date d = t.getCreatedAt();
String dateString = d.toString();
 SimpleDateFormat f = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ZZZZZ");
    try {
      Date parsed = f.parse(dateString);
      System.out.println(" 2. " + parsed.toString());
    }
    catch(ParseException pe) {
        System.out.println("ERROR: Cannot parse \"" + dateString + "\"");
}

All I receive is the Error message :(

ERROR: Cannot parse "Thu Mar 26 20:10:48 GMT 2015"

UPDATE: So tried what was linked below to no joy ill add in the whole code`try. Also im using Processing

{
  QueryResult result = twitter.search(query);
  ArrayList tweets = (ArrayList) result.getTweets();

  for (int i = 0; i < tweets.size (); i++) {
  Status t = (Status) tweets.get(i);
  User u=(User) t.getUser();
  String user=u.getName();

  String msg = t.getText();
  Date d = t.getCreatedAt();


  final String TWITTER="EEE MMM dd HH:mm:ss ZZZZZ yyyy";
  SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
  sf.setLenient(true);
  return sf.parse(d);


  println("Tweet by " + user + " at " + d + ": " + msg);
  //Break the tweet into words
  String[] input = msg.split(" ");

  println(input);

  TableRow newRow = table.addRow();
  newRow.setString("Name", u.getName());
   newRow.setString("Date", d.getDate());
  newRow.setString("Tweet", t.getText());

  for (int j = 0; j < input.length; j++) {
    //Put each word into the words ArrayList
    words.add(input[j]);
  }
};

Obviously I want to pass through the formated date to newRow.setString("Date", d.getDate());

UPDATE Got it working as you can see below:

Date d = t.getCreatedAt();
  //Thu Mar 26 20:21:09 GMT 2015 (time format)
  //try catch to test
  //after reformatting using Sdf it works
  String dateString = d.toString();
  SimpleDateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
  try {
    Date parsed = f.parse(dateString);
    System.out.println(" 2. " + parsed.toString());
  }
  catch(ParseException pe) {
    System.out.println("ERROR: Cannot parse \"" + dateString + "\"");
  }
  println("Tweet by " + user + " at " + d + ": " + msg);
  //Break the tweet into words
  String[] input = msg.split(" ");

  println(input);

  TableRow newRow = table.addRow();
  newRow.setString("Name", u.getName());
  newRow.setString("Date", d.toString());
  newRow.setString("Tweet", t.getText());

Thanks as always :)

Nebbyyy
  • 358
  • 4
  • 20

2 Answers2

0

If your intention is to create a date and print it out in the format you've provided, I think what you're looking for is the following:

System.out.println(" 2. " + f.format(d));

But if you actually want to be able to parse dateString, you will have to change your format to match the format dateString is in. i.e.

SimpleDateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy");
Amber
  • 2,413
  • 1
  • 15
  • 20
0

There was a couple of problems firstly the date format which I found here http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html simple enough to work through, and I also had to declare the d.toString() when i was passing the date through to my table.

  String msg = t.getText();
  Date d = t.getCreatedAt();
  //Thu Mar 26 20:21:09 GMT 2015 (time format)
  //try catch to test
  //after reformatting using Sdf it works
  String dateString = d.toString();
  SimpleDateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
  try {
    Date parsed = f.parse(dateString);
    System.out.println(" 2. " + parsed.toString());
  }
  catch(ParseException pe) {
    System.out.println("ERROR: Cannot parse \"" + dateString + "\"");
  }
  println("Tweet by " + user + " at " + d + ": " + msg);
  //Break the tweet into words
  String[] input = msg.split(" ");

  println(input);

  TableRow newRow = table.addRow();
  newRow.setString("Name", u.getName());
  newRow.setString("Date", d.toString());
  newRow.setString("Tweet", t.getText());
Nebbyyy
  • 358
  • 4
  • 20