J2ME Convert Date To String
As in the previous example, you have learned about how to print the current
date on the screen. But in this example we are going to convert the date into
string.
java.util.Hashtable package contains a method called tostring, that is used
to convert the date into string.
Syntax
public String toString()
Output of the Convert Date To String MIDlet will be as follow..
Output of the command prompt
Source Code of DateToString.java
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class DateToString extends MIDlet implements CommandListener{
private Display display;
private Date date;
private DateField currentDate;
private Command exit;
private Form form;
private int index;
String dateinstring;
public DateToString(){
form = new Form("Data and Time");
date = new Date();
exit = new Command("Exit", Command.EXIT, 0);
currentDate = new DateField("", DateField.DATE_TIME);
currentDate.setDate(date);
dateinstring = date.toString();
System.out.println("Date And Time Is In String Format: "+dateinstring);
}
public void startApp(){
form.append("CURRENT TIME IS: ");
index = form.append(currentDate);
form.addCommand(exit);
form.setCommandListener(this);
display = Display.getDisplay(this);
display.setCurrent(form);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){
notifyDestroyed();
}
public void commandAction(Command cmd, Displayable s){
if (cmd == exit){
destroyApp(true);
}
}
}
|
Download Source Code