Code
/*
The result I get is a little different, but I think my code works.
The println displays:
Wed Nov 05 00:00:00 GMT 2008
Milliseconds since 1/1/1970 =
1225843200000
*/
int monthInt = 0;
String S = " <pubDate>Wed, 05 Nov 2008 21:33:50 +0000</pubDate>";
String T = S.trim();
int pref1 = T.indexOf(' ');
int pref2 = T.indexOf(' ', (pref1+4));
int pref3 = T.indexOf(' ', (pref1+2));
String D = T.substring(pref1+1, pref1+3);
int dayInt = Integer.parseInt(D);
String Y = T.substring(pref2+1, pref2+5);
int yearInt = Integer.parseInt(Y);
String M = T.substring(pref3+1, pref3+4);
String monthNames[] =
{ "Jan","Feb","Mar","Apr",
"May","Jun","Jul","Aug",
"Sep","Oct","Nov","Dec"};
for (int i = 0; i < monthNames.length; i++){
if (M.equals(monthNames[i])){
monthInt = i;
}
}
GregorianCalendar greg = new GregorianCalendar(yearInt, monthInt, dayInt);
Date someDate = greg.getTime();
long millisecondsSince1970 = someDate.getTime();
println(someDate);
println("Milliseconds since 1/1/1970 = " + millisecondsSince1970);
0702 - String method refresher: Scraping and parsing dates
Statement:This five-part assignment is a collection of simple exercises to make sure that you understand (and have used) some of the many String methods that are available to you. In the next assignment (0703), you will use the results of this assignment to produce a display of Twitter posts over time, using data scraped from my (or someone else's) Twitter RSS feed. It goes without saying that you will need to refer to the Java String reference for this assignment.
For this assignment: suppose you have encountered a String S formatted as follows. (This String is from my Twitter RSS feed.)
String S = " <pubDate>Wed, 05 Nov 2008 21:33:50 +0000</pubDate>";
int dayInt = Integer.parseInt(D);
String monthNames[] =
{ "Jan","Feb","Mar","Apr",
"May","Jun","Jul","Aug",
"Sep","Oct","Nov","Dec"};
Using a for{} loop, check the month String M to see if it is the same as any of the Strings in the monthNames[] array. If it is, report an integer monthInt, which is the index of the identical String in the monthNames array. IMPORTANT NOTE: In order to make the String comparison test, the == operator will not work; you must use the String.equals() method instead!Below is some code which will convert these integers into a single number, which represents (for this date) the number of milliseconds since 1970. This code uses the GregorianCalendar object and the Date object, both of which are built into Java. (You can get Java documentation about these objects from the links).
GregorianCalendar greg = new GregorianCalendar(yearInt, monthInt, dayInt);
Date someDate = greg.getTime();
long millisecondsSince1970 = someDate.getTime();
println(someDate);
println("Milliseconds since 1/1/1970 = " + millisecondsSince1970);
To confirm that your previous code works correctly, check that the last two println() commands produce the following results:
Wed Nov 05 00:00:00 EST 2008 Milliseconds since 1/1/1970 = 1225861200000
hide statement