Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 14:17, 29 September 2013 by lpvalente (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to check if a date is in the future or past in Java ME

From Wiki
Jump to: navigation, search
Article Metadata
Created: dinanath.d (06 Jan 2010)
Last edited: lpvalente (29 Sep 2013)

The following code is used to check if the entered date is a future date or past date or today's date. In simple terms, this code demonstrates Time input validation. This is important in various scenarios, like when date- of Birth if given as a future date needs to requested for a correction from the user.

import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class DateDemo extends MIDlet implements ItemStateListener, CommandListener
private Display display;
private Form fmMain;
private Command cmCheckDate;
private Command cmReset;
private Command cmExit;
private DateField dfTime;
private Date currentTime;
private int dateOK = 0;
public DateDemo()
display = Display.getDisplay(this);
fmMain = new Form("Date Verifications");
currentTime = new Date();
dfTime = new DateField("", DateField.DATE);
cmCheckDate = new Command("Check Date", Command.SCREEN, 1);
cmReset = new Command("Reset", Command.SCREEN, 1);
cmExit = new Command("Exit", Command.EXIT, 1);
public void startApp ()
public void pauseApp()
{ }
public void destroyApp(boolean unconditional)
{ }
public void itemStateChanged(Item item)
if (item == dfTime) {
if (dfTime.getDate().getTime() < currentTime.getTime()){
if(((currentTime.getTime() - dfTime.getDate().getTime()) / (1000*60*60*24) ) == 0)
dateOK = 0;
else dateOK = 1;
dateOK = 2;
public void commandAction(Command c, Displayable s)
if (c == cmCheckDate)
if (dateOK == 0)
ShowAlertBox("Same date is selected");
else if (dateOK == 1)
ShowAlertBox("Past date is selected");
ShowAlertBox("Future date is selected");
else if (c == cmReset)
dfTime.setDate(currentTime = new Date());
else if (c == cmExit)
public void ShowAlertBox(String msg)
Alert al = new Alert("Info", msg, null, null);

Further Reading

  1. Date class in J2ME
  2. Calendar class in J2ME
This page was last modified on 29 September 2013, at 14:17.
96 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.