你可以猜测我试图解析一个推特日期,这样我就可以将它应用到一个表格,代码是关于它当前的样子:
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 + "\"");
}
我收到的只是错误信息:(
ERROR: Cannot parse "Thu Mar 26 20:10:48 GMT 2015"
更新: 所以尝试下面链接的内容,没有任何喜悦加入整个代码。我也在使用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]);
}
};
显然,我希望将格式化日期传递给newRow.setString("Date", d.getDate());
更新让它正常工作,如下所示:
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());
一如既往地谢谢:)
答案 0 :(得分:0)
如果您打算创建一个日期并以您提供的格式打印出来,我认为您正在寻找的是以下内容:
System.out.println(" 2. " + f.format(d));
但是如果你真的希望能够解析dateString,你将不得不改变你的格式以匹配dateString所在的格式。即
SimpleDateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy");
答案 1 :(得分:0)
首先出现了几个问题我在这里找到了http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html的日期格式
很简单,可以通过,当我把日期传递到我的桌子时,我还必须声明d.toString()
。
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());