We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › date format conversion
Page Index Toggle Pages: 1
date format conversion (Read 965 times)
date format conversion
Oct 23rd, 2007, 12:38am
 
Hi!
I just worked out how to get information from rss feeds to processing using the XML Import library. The date imported from the rss file looks something like this: "Tue, 23 Oct 2007 00:27:21 MET". To compare the dates of different entries, i need the months to be converted into digits. So  i wrote the following code that doesn't work. What went wrong?
Code:

// date = Tue, 23 Oct 2007 00:27:21 MET
String date_splitted[] = split(date, " ");

// [0]Thu,
// [1]18
// [2]Oct
// [3]2007
// [4]03:02:01
// [5]EDT

if(date_splitted[2] == "Jan"){
date_splitted[2] = "1";
}

else if(date_splitted[2] == "Feb"){
date_splitted[2] = "2";
}

else if(date_splitted[2] == "Mar"){
date_splitted[2] = "3";
}

else if(date_splitted[2] == "Apr"){
date_splitted[2] = "4";
}

else if(date_splitted[2] == "May"){
date_splitted[2] = "5";
}

else if(date_splitted[2] == "Jun"){
date_splitted[2] = "6";
}

else if(date_splitted[2] == "Jul"){
date_splitted[2] = "7";
}

else if(date_splitted[2] == "Aug"){
date_splitted[2] = "8";
}

else if(date_splitted[2] == "Sep"){
date_splitted[2] = "9";
}

else if(date_splitted[2] == "Oct"){
date_splitted[2] = "10";
}

else if(date_splitted[2] == "Nov"){
date_splitted[2] = "11";
}

else if(date_splitted[2] == "Dec"){
date_splitted[2] = "12";
}

Re: date format conversion
Reply #1 - Oct 23rd, 2007, 1:03am
 
Because you can't compare Strings using the == operator.

Problem:
http://processing.org/reference/troubleshooting/#strings
Solution:
http://processing.org/reference/String_equals_.html
Re: date format conversion
Reply #2 - Oct 23rd, 2007, 1:17am
 
works great, many thanks!
Re: date format conversion
Reply #3 - Oct 23rd, 2007, 10:52am
 
You may be able to save yourself a lot of work by checking out the Java "SimpleDateFormat" class: http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html

I think you should be able to create a SimpleDateFormat object that understands your format, and then you use the parse(...) method to convert a string to a Date object you can use.

I think your time format would be "EEE, d MMM yyyy HH:mm:ss z" in SimpleDateFormat terms.

Page Index Toggle Pages: 1