Date time difference determine which is earlier Android Java example

Date difference in millisecond between two date, GregorianCalendar, Calendar, after(), befor(), getTimeInMillis().

 
public class HoriziontalScrollActivity extends Activity {
TextView txtV;
	Context cntx;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtV = (TextView)findViewById(R.id.idLabel);
        cntx = this;
        StringBuilder strBuild = new StringBuilder();
        Calendar firstDate = new GregorianCalendar(2011, Calendar.DECEMBER, 31);
        Calendar secondDate = new GregorianCalendar(2012, Calendar.JANUARY, 1);
 
 
        Boolean bDetermine = firstDate.after(secondDate);            // false
        strBuild.append("Is firsDate after secondDate? " + bDetermine + "\n");
        bDetermine = firstDate.before(secondDate);                   // true
        strBuild.append("Is firsDate before secondDate? " + bDetermine + "\n");
 
 
        long differenceInMillisecond = 0L;
         differenceInMillisecond = secondDate.getTimeInMillis()-firstDate.getTimeInMillis();
         // second == 1000 millisecond
         long second = differenceInMillisecond / 1000L;
         strBuild.append("Difference between two dates is: " + second + "\n");
 
         txtV.setText(strBuild);
 
    }
 
}
This entry was posted in Android Examples Code and tagged , , , , . Bookmark the permalink.


Comments are closed.



7,273 views