Convert Jsoup element to string

I'm developing an Android app. I need to change an Element in Jsoup to a string so that I can subtract the two. The code below and String value = valueOf(arrT.val()); do not work. I'm not sure how to convert an Element into a string. My code is:


public class TestStation extends Activity { String URL = ""; @SuppressWarnings("deprecation") @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(; 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 ="eta"); for (Element div : elem) { }Elements elemn ="eta"); for (Element div : elem) { Elements arrT ="arrT"); Elements prdt ="prdt"); try { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); Date date2 = sdf.parse(prdt.val()); Date date1 = sdf.parse(arrT.val()); tv.setText(String.valueOf (prdt)); long dateDiff = (date1.getTime() - date2.getTime())>0 ? (date1.getTime() - date2.getTime()) :(date2.getTime() - date1.getTime()); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); sdf1.format(dateDiff); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

It gives me a compile error saying that prdt cannot change from an Element to a string. I would love any help you could give on this, because there is noting online that I can find on this. Thank you.


Use Jsoup text() function

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); Date date2 = sdf.parse(prdt.text());


try out:

String value = arrT.val(); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

Date date = sdf.parse(value);

see the jsoup apidocs

Note: the date pattern must match with the format you are passing to the <strong>parse</strong> method.

Additional note: as <strong>doc = Jsoup.connect(URL)</strong> does network calls, consider putting it in an AsyncTask instead of calling directly on the UI Thread (in the onCreate() method).


If you are getting date in

Then it should be like this and are you forgetting <strong>prdt.val()</strong>

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss"); Date date = sdf.parse(prdt.val());

人吐槽 人点赞



用户名: 密码:
验证码: 匿名发表


查看评论:Convert Jsoup element to string