2

I am developing an Android app. I am setting a difference of 2 dates to a TextView. It doesn't display anything. Somewhere, it isn't getting to the TextView. I would love if you could tell me where I'm going wrong. My code is below. Now after putting in new code, it force closes. Code updated with new code

TestStation.java

public class TestStation extends Activity {
String URL = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=201412abc85d49b2b83f907f9e329eaa&mapid=40380";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.test_station);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 



Document doc = null;

TextView tv = (TextView) findViewById(R.id.tv);

try {
    doc = Jsoup.connect(URL).userAgent("Mozilla/5.0 (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10").get();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Elements elem = doc.select("eta");
for (Element div : elem) {

}Elements elemn = doc.select("eta"); for (Element div : elem) {
Elements arrT = div.select("arrT");
Elements prdt = div.select("prdt");

String value = arrT.val(); 
String valu = prdt.val();

Date date1 = new Date(valu);
Date date2 = new Date(value);

long dateDiff = (date1.getTime() - date2.getTime())>0 ? (date1.getTime() - date2.getTime()) :(date2.getTime() - date1.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:00");
sdf.format(dateDiff);
tv.setText(String.valueOf (dateDiff));

}

}

The LogCat shows lots of red (errors):

09-22 12:39:38.348: E/AndroidRuntime(9713): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dev.chicagotraintracker/com.dev.chicagotraintracker.TestStation}:     java.lang.IllegalArgumentException: Parse error: 
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.os.Looper.loop(Looper.java:137)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.lang.reflect.Method.invokeNative(Native Method)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.lang.reflect.Method.invoke(Method.java:525)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at dalvik.system.NativeStart.main(Native Method)
09-22 12:39:38.348: E/AndroidRuntime(9713): Caused by: java.lang.IllegalArgumentException: Parse error: 
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.util.Date.parseError(Date.java:367)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.util.Date.parse(Date.java:563)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.util.Date.<init>(Date.java:156)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at com.dev.chicagotraintracker.TestStation.onCreate(TestStation.java:53)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.Activity.performCreate(Activity.java:5133)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)

How can I solve this?

4

1 回答 1

1

Problem is not with the TextView

The main problem is with date parsing

You do not catch the first parsing which trows an exception it may not be in correct format to be catched check both value and vlau

     try{
        Date date = sdf.parse(value);
        try
        {
            Date dat = sdf.parse(valu);
            long diff = dat.getTime() - date.getTime();
            tv.setText(String.valueOf(diff));
        }
        catch(ParseException f)
        {
            f.printStackTrace();
        }
        finally
        {}
    }
    catch(ParseException f)
    {
        f.printStackTrace();
    }
    finally
    {}

EDIT 1:

You can get the DateDifference this way

Date date1 = new SimpleDateFormat("GiveYourFormate", Locale.ENGLISH).parse(stringDate1);
Date date2 = new SimpleDateFormat("GiveYourFormate", Locale.ENGLISH).parse(stringDate2);
long dateDiff = (date1.getTime() - date2.getTime())>0 ? (date1.getTime() - date2.getTime()) :(date2.getTime() - date1.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:00");
sdf.format(dateDiff);
于 2013-09-22T03:21:46.093 回答