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 › Unhandled exception type... using SimpleDateFormat
Page Index Toggle Pages: 1
Unhandled exception type... using SimpleDateFormat (Read 2339 times)
Unhandled exception type... using SimpleDateFormat
Jun 16th, 2009, 12:26pm
 
Hi, I am having a little trouble getting to grips with dates in processing.

Currently trying to convert a date stamp in the format yyyyMMdd to an integer Unix epoch time in seconds, and convert it back. Then display the results of each step on the screen to make sure it works.

I am using some functions of the Java class SimpleDateFormat to handle the conversions, however when I run my code (below) I get an error which states Unhandled exception type ParseException.

I have read that this is a Java exception ad I should use a try catch exception handling routine to prevent this, which will remove the error but won't produce the desired result. I'm really stumped - can anybody see where I am going wrong?

Martin


import java.text.SimpleDateFormat;

String initialDateStamp = "20090612";

DateFormat stampFormat = new SimpleDateFormat("yyyyMMdd");

Date initialDate = stampFormat.parse(initialDateStamp);
long initialDateMillis = initialDate.getTime();

String originalDateStamp;
Date date = new Date(initialDateMillis);
originalDateStamp = stampFormat.format(date);

long oneThousand = 1000;
long initialDateSeconds;
initialDateSeconds = initialDateMillis/oneThousand;

int initialDateInteger = (int)initialDateSeconds;

println(initialDateStamp);
println(initialDateInteger);
println(originalDateStamp);
 
Re: Unhandled exception type... using SimpleDateFormat
Reply #1 - Jun 16th, 2009, 1:20pm
 
Careful with that post button. Wink

The parse function is able to throw an exception if there is a problem parsing the string.

What this means is that Java wants code to handle that exception.
Code:
//Variable must be declared outside of the try{} statement for the rest of your code to use it
Date initialDate = null;
try {
initialDate = stampFormat.parse(initialDateStamp);
} catch (Exception e) {
//do anything you want to handle the exception
println("Unable to parse date stamp");
}
Re: Unhandled exception type... using SimpleDateFormat
Reply #2 - Jun 17th, 2009, 2:33am
 
I feel the shame of multiple posts - is there any way to get the duplicates deleted? There was an error screen displayed when posting and I panicked and clicked again, and again.

That works NoahBuddy, thanks. But could I just clarify that when using the parse function you must use a try and catch loop in case an exception is generated? Even if running it will not generate an exception it still needs the routine.

I notice when formatting the date back to the date stamp format yyyyMMdd on line

Code:
originalDateStamp = stampFormat.format(date); 



that the catch/try routine is not necessary, but surely an exception could be generated from that too in some situations so why no exception handling routine required? Is there any way of knowing which Java functions require exception handling code and which do not? Or would it be a case of trying them and seeing if an Unhandled exception type ParseException error appears?
Re: Unhandled exception type... using SimpleDateFormat
Reply #3 - Jun 17th, 2009, 4:22am
 
baxtardo wrote on Jun 17th, 2009, 2:33am:
I feel the shame of multiple posts
You are not the only one, far from it. Phenomenon seems quite common since forum was upgraded. In the old forum there was a Delete message button (in the thread view), check if it is there (I don't create threads often, I can't verify right now...). Otherwise forum moderators will probably clean it up.

Quote:
when using the parse function you must use a try and catch loop in case an exception is generated Even if running it will not generate an exception it still needs the routine.
Yes, that's the way Java works, it is called "checked exceptions" and it is quite controversial, lot of people think Java should have only unchecked exceptions (like ArithmeticException or ArrayOutOfBoundsException, they are runtime exceptions).
Checked exceptions force you to try/catch them (the compiler enforce the requirement) or to declare your method to throw the same exception.

Quote:
when formatting the date back to the date stamp format yyyyMMdd [...] the catch/try routine is not necessary
One could call this inconsistency of design. In general, parsing an external string might throw an exception, and you want to catch it, eg. to display feedback to user (in an interactive application) and ask to do the input again. Or just to supply a default parsed value.
If format sees some unknown characters, in general it just ignore them (or leave them as is).

Quote:
Is there any way of knowing which Java functions require exception handling code and which do not
1) Check the API: for example in BufferedReader, we have:
Quote:
public int read()
        throws IOException

because I/O is unreliable (resource not available, permission issue, etc.).
2) Well, you mention it, you have a compiler error.
3) You use a smart IDE (like Eclipse or NetBeans) which warns you while you type, and can even automatically add the try/catch block! Perhaps PDE will do the same some day.
Re: Unhandled exception type... using SimpleDateFormat
Reply #4 - Jun 17th, 2009, 7:28am
 
Great thanks PhiLho that makes it a lot clearer in my head now, plus I've covered up my foolish ways by deleting those multiple posts.

Now that I am happy with my conversion from a date stamp to a Date object I am trying to have the day of the week for that date.

The long integer version of the time in milliseconds is named initialDateMillis and I attempt to set a Calendar object using the line

Code:
Calendar cal = Calendar.setTimeInMillis(initialDateMillis); 



and then calculate the day of the week using

Code:
initialDateDay = cal.get(Calendar.DAY_OF_WEEK); 



however this returns an error Cannot make a static reference to the non-static method setTimeInMillis(long) from the type Calendar which I don't understand - can anyone help explain this and/or offer a solution?

My full code is below...

Code:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

String initialDateStamp = "20090624";

int initialDateDay;

DateFormat stampFormat = new SimpleDateFormat("yyyyMMdd");
Date initialDate = null;

try {  
initialDate = stampFormat.parse(initialDateStamp);
} catch (Exception e) {
 println("Unable to parse date stamp");
}

long initialDateMillis = initialDate.getTime();

Calendar cal = Calendar.setTimeInMillis(initialDateMillis);

initialDateDay = cal.get(Calendar.DAY_OF_WEEK);

println(initialDateStamp);
println(initialDateDay);
Re: Unhandled exception type... using SimpleDateFormat
Reply #5 - Jun 17th, 2009, 7:48am
 
If not already done, I suggest you take a look at the Calendar reference.

In Java, you have basically two kinds of methods (ie. functions attached to a class): static methods, which you can call like you do, ClassName.doStuff(), because they don't depend of class instance data (they are close of generic functions, but encapsulated in a namespace); and instance methods, the most common, that will need an instance of the class (an object) because they access instance specific data.

setTimeInMillis(), as the name implies, changes instance data, so must be applied to a Calendar object.

You probably want to do:
Code:
Calendar cal = Calendar.getInstance(); // Initialize at current time
cal.setTimeInMillis(initialDateMillis);
Page Index Toggle Pages: 1