title: How to store date with milliseconds and sort using collections in java
body: <p>In my use case I would like to store date in an object and sort the object based on date[custom comparator] in java. I tried the following code,</p>
  
  <pre><code>    class Tweet implements Comparable&lt;Tweet&gt;{
          int tweetId;
          Date dateTime;
  
          public Tweet(int tweetId, Date dateTime)
          {
              this.tweetId = tweetId;
              this.dateTime = dateTime;
          }
  
          public Date getDateTime()
          {
              return dateTime;
          }
  
          public int getTweetId()
          {
              return tweetId;
          }
  
          @Override
          public int compareTo(Tweet o)
          {
              if (getDateTime() == null || o.getDateTime() == null)
                  return 0;
              return getDateTime().compareTo(o.getDateTime());
          }
      }
  
      public class ApplicationObj
      {
          /** Compose a new tweet. */
          public void postTweet(int userId, int tweetId) {
              Date date = new Date();
              User user;
              Date dTime = new Date();
              try {
                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
                  String dateTime = sdf.format(date);
                  dTime = sdf.parse(dateTime);
              } catch (ParseException  e)
              {
                  ;
              }
              if(!userMap.containsKey(userId))
              {
                  user = new User(userId);
                  userMap.put(userId, user);
              } else {
                  user = userMap.get(userId);
              }
              ArrayList&lt;Tweet&gt; tweets = user.getTweets();
              tweets.add(new Tweet(tweetId, dTime));
          }
  
       public List&lt;Integer&gt; getNewsFeed(int userId) {
          if(!userMap.containsKey(userId))
          {
              return new ArrayList&lt;Integer&gt;();
          }
          User user = userMap.get(userId);
          ArrayList&lt;Tweet&gt; newsFeedTweets = new ArrayList&lt;Tweet&gt;();
          ArrayList&lt;Tweet&gt; userTweets = user.getTweets();
          Set&lt;User&gt; followers = user.getFollowers();
          ArrayList&lt;Tweet&gt; followersTweets = getFollowersTweets(followers);
  
          newsFeedTweets.addAll(userTweets);
          newsFeedTweets.addAll(followersTweets);
  
          Collections.sort(newsFeedTweets, new Comparator&lt;Tweet&gt;() {
            public int compare(Tweet o1, Tweet o2) {
              if (o1.getDateTime() == null || o2.getDateTime() == null)
                  return 0;
              return o2.getDateTime().compareTo(o1.getDateTime());
            }
          });
  
          ArrayList&lt;Tweet&gt; newsFeed;
          if(newsFeedTweets.size() &gt; 10)
          {
              newsFeed = new ArrayList&lt;Tweet&gt;(newsFeedTweets.subList(0,10));
          }else {
              newsFeed = newsFeedTweets;
          }
  
          for(Tweet tweet : newsFeed)
          {
              System.out.println("id "+tweet.getTweetId());
          }
          return getTweetIds(newsFeed);
      }
  
   }
  </code></pre>
  
  <p>I am trying to store the date with milliseconds after parsing with simple date format.  </p>
  
  <p>But it is not working. Could someone help me with this?</p>