Namespaces

Variants
Actions

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.

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

From Wiki
Jump to: navigation, search
Article Metadata
Article
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);
dfTime.setDate(currentTime);
 
cmCheckDate = new Command("Check Date", Command.SCREEN, 1);
cmReset = new Command("Reset", Command.SCREEN, 1);
cmExit = new Command("Exit", Command.EXIT, 1);
 
fmMain.append(dfTime);
fmMain.addCommand(cmCheckDate);
fmMain.addCommand(cmReset);
fmMain.addCommand(cmExit);
fmMain.setCommandListener(this);
fmMain.setItemStateListener(this);
}
 
public void startApp ()
{
display.setCurrent(fmMain);
}
 
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;
}
else
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");
else
ShowAlertBox("Future date is selected");
}
else if (c == cmReset)
{
dfTime.setDate(currentTime = new Date());
}
else if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
}
 
public void ShowAlertBox(String msg)
{
Alert al = new Alert("Info", msg, null, null);
al.setTimeout(Alert.FOREVER);
al.setType(AlertType.INFO);
display.setCurrent(al);
}
}

Further Reading

  1. Date class in J2ME
  2. Calendar class in J2ME
This page was last modified on 29 September 2013, at 14:17.
170 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.

×