Thursday, 18 December 2014

Java code to extract tweets since any date using Twitter4J API

/* Below is the code to extract tweets since any date or any given date  using twitter 4J API */       

         Twitter twitter = new TwitterFactory().getInstance();
         twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);
         String accessToken = 'asdasdsdsaasdsaedadasdadsadasd'; /* enter your twitter access token */
        String accessTokenSecret = '2020202-asdsadsadasdsadsadsadsa'; /enter your twitter secret token */
         AccessToken oathAccessToken = new AccessToken(accessToken, accessTokenSecret);
        Query query = new Query("Iron Man");
        Date date = new Date();
        String modifiedDate= new SimpleDateFormat("yyyymmdd").format(date);
        QueryResult result;
            do {
            result =;
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                 System.out.println("@"+tweet.getUser().getScreenName() + "|" + tweet.getText()+"|"+ tweet.isRetweeted() );
              } while ((query = result.nextQuery()) != null);

To create twitter app account and get the access tokens, check out the link below: