×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Sep 2011
    Posts
    73

    Trying to convert a DateField to a string.

    I have a form where the date can be specified using a DateField. I am trying to obtain a string value of that DateField. Instead of getting the string value of the date that is chosen, I get the string value of the current date.

    Does anyone know how to take the value chosen from a date field and convert it into a string?

    Below is the code:

    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import java.util.*;
    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.rms.*;
    
    import java.lang.*;
    
    public class DateToStringTest extends MIDlet implements CommandListener, ItemCommandListener {
    
    private Command exitCommand, saveCommand, cancelDisplaySavedInfoCommand;
    private Display display;
    private Form screen;
    private Form frmDisplaySavedInfo;
    private Alert alert;
    private DateField dateChoiceField;
    private String strDateChoice;
    private Date date;
    
    public DateToStringTest() {
    	// Get the display object for the MIDlet.
    	display = Display.getDisplay(this);
    
    	// Create the Exit, Save, and Cancel commands.
    	exitCommand = new Command("Exit", Command.EXIT, 2);
    	saveCommand = new Command("Save", Command.OK, 2);
    	cancelDisplaySavedInfoCommand = new Command("Cancel", Command.CANCEL, 2);
    
    	// Create the screen form.
    	screen = new Form("Main Screen");
    
    	// Set the Exit and Save commands for the screen.
    	screen.addCommand(exitCommand);
    	screen.addCommand(saveCommand);
    	screen.setCommandListener(this);
    } // This ends "public DateToStringTest()".
    
    public void startApp() {
    
    	// Create a date field for the main screen.
    	dateChoiceField = new DateField("Date Choice:", DateField.DATE);
    	dateChoiceField.setItemCommandListener(this);
    	screen.append(dateChoiceField);
    
    	// Set the current display to the screen.
    	display.setCurrent(screen);
    
    } // This ends startApp().
    
    public void pauseApp() {
    } // This ends pauseApp().
    
    public void destroyApp(boolean unconditional) {
    } // This ends destroyApp().
    
    public void saveCom() {
    
    	// Get the date choice that was entered on the main screen.
    	//strDateChoice = dateChoiceField.getDate();
    	//strDateChoice = dateToString(dateChoiceField.getDate());
    	//String strDateChoice = dateChoiceField.toString();
    	date = new Date();
    	dateChoiceField.setDate(date);
    	strDateChoice = date.toString();
    	System.out.println("The date choice is: " + strDateChoice);
    
    	/*
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(new Date());
    	int year = calendar.get(Calendar.YEAR);
    	int month = calendar.get(Calendar.MONTH)+1;
    	int date = calendar.get(Calendar.DATE);
    	System.out.println("The day is: " + date);
    	*/
    
    	// Create the save form for testing purposes, in order to display what would be saved.
    	frmDisplaySavedInfo = new Form("Saved Info");
    
    	// Set the "Cancel" command for the save form.
    	frmDisplaySavedInfo.addCommand(cancelDisplaySavedInfoCommand);
    	frmDisplaySavedInfo.setCommandListener(this);
    
    	// Display the test information.
    	frmDisplaySavedInfo.append(new StringItem("", "Testing\n"));
    	frmDisplaySavedInfo.append(new StringItem("", strDateChoice + "\n"));
    
    	// Set the current display to the save form.
    	display.setCurrent(frmDisplaySavedInfo);
    } // This ends saveCom().
    
    public void cancelDisplaySavedInfoCom() {
    	display.setCurrent(screen);
    } // This ends cancelDisplaySavedInfoCom().
    
    	public void commandAction(Command c, Item item) {
    		if (c == exitCommand) {
    			destroyApp(false);
    			notifyDestroyed();
    		} else if (c == saveCommand) {
    			saveCom();
    		} else if (c == cancelDisplaySavedInfoCommand) {
    			cancelDisplaySavedInfoCom();
    		}
    	} // This ends "public void commandAction(Command c, Item item)".
    
    	public void commandAction(Command c, Displayable d) {
    		if (c == exitCommand) {
    			destroyApp(false);
    			notifyDestroyed();
    		} else if (c == saveCommand) {
    			saveCom();
    		} else if (c == cancelDisplaySavedInfoCommand) {
    			cancelDisplaySavedInfoCom();
    		}
    	} // This ends "public void commandAction(Command c, Displayable d)".
    
    
    } // This ends "public class DateToStringTest extends MIDlet implements CommandListener, ItemCommandListener".

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Trying to convert a DateField to a string.

    You should be getting the user-modified date out of the field, not setting it. If you define a new Date object, by instantiating you set it to the current date. Then by setting your date field to this date object, is the same as setting the field's date to the current date.

    In the SaveCom method you need to replace this:
    Code:
    	date = new Date();
    	dateChoiceField.setDate(date);
    	strDateChoice = date.toString();
    	System.out.println("The date choice is: " + strDateChoice);
    with this:
    Code:
    	Date date=dateChoiceField.getDate();
    	strDateChoice = date.toString();
    	System.out.println("The date choice is: " + strDateChoice);

  3. #3
    Regular Contributor
    Join Date
    Sep 2011
    Posts
    73

    Re: Trying to convert a DateField to a string.

    Thank you, skalogir. I'll make the changes and check back here.

  4. #4
    Regular Contributor
    Join Date
    Sep 2011
    Posts
    73

    Re: Trying to convert a DateField to a string.

    Below is the properly functioning code that reflects the recommended changes:

    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import java.util.*;
    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.rms.*;
    
    import java.lang.*;
    
    public class DateToStringTest extends MIDlet implements CommandListener, ItemCommandListener {
    
    private Command exitCommand, saveCommand, cancelDisplaySavedInfoCommand;
    private Display display;
    private Form screen;
    private Form frmDisplaySavedInfo;
    private Alert alert;
    private DateField dateChoiceField;
    private String strDateChoice;
    private Date date;
    
    public DateToStringTest() {
    	// Get the display object for the MIDlet.
    	display = Display.getDisplay(this);
    
    	// Create the Exit, Save, and Cancel commands.
    	exitCommand = new Command("Exit", Command.EXIT, 2);
    	saveCommand = new Command("Save", Command.OK, 2);
    	cancelDisplaySavedInfoCommand = new Command("Cancel", Command.CANCEL, 2);
    
    	// Create the screen form.
    	screen = new Form("Main Screen");
    
    	// Set the Exit and Save commands for the screen.
    	screen.addCommand(exitCommand);
    	screen.addCommand(saveCommand);
    	screen.setCommandListener(this);
    } // This ends "public DateToStringTest()".
    
    public void startApp() {
    
    	// Create a date field for the main screen.
    	dateChoiceField = new DateField("Date Choice:", DateField.DATE);
    	dateChoiceField.setItemCommandListener(this);
    	screen.append(dateChoiceField);
    
    	// Set the current display to the screen.
    	display.setCurrent(screen);
    
    } // This ends startApp().
    
    public void pauseApp() {
    } // This ends pauseApp().
    
    public void destroyApp(boolean unconditional) {
    } // This ends destroyApp().
    
    public void saveCom() {
    
    	// Get the date choice that was entered on the main screen.
    	Date date = dateChoiceField.getDate();
    	if (date == null) {
    		System.out.println("The date choice has a null value (the date was not chosen)");
    		alert = new Alert("Date Wasn't Chosen",  // This version does not permit continuing.
    						  "Cannot save. Please choose a date.", null, AlertType.WARNING); // This version does not permit continuing.
    		alert.setTimeout(Alert.FOREVER);
    		display.setCurrent(alert);	
    		return;		
    	} else {
    		strDateChoice = date.toString();		
    		System.out.println("The date choice is: " + strDateChoice);
    	}
    
    	// Create the save form for testing purposes, in order to display what would be saved.
    	frmDisplaySavedInfo = new Form("Saved Info");
    
    	// Set the "Cancel" command for the save form.
    	frmDisplaySavedInfo.addCommand(cancelDisplaySavedInfoCommand);
    	frmDisplaySavedInfo.setCommandListener(this);
    
    	// Display the test information.
    	frmDisplaySavedInfo.append(new StringItem("", "Testing\n"));
    	frmDisplaySavedInfo.append(new StringItem("", strDateChoice + "\n"));
    
    	// Set the current display to the save form.
    	display.setCurrent(frmDisplaySavedInfo);
    } // This ends saveCom().
    
    public void cancelDisplaySavedInfoCom() {
    	display.setCurrent(screen);
    } // This ends cancelDisplaySavedInfoCom().
    
    	public void commandAction(Command c, Item item) {
    		if (c == exitCommand) {
    			destroyApp(false);
    			notifyDestroyed();
    		} else if (c == saveCommand) {
    			saveCom();
    		} else if (c == cancelDisplaySavedInfoCommand) {
    			cancelDisplaySavedInfoCom();
    		}
    	} // This ends "public void commandAction(Command c, Item item)".
    
    	public void commandAction(Command c, Displayable d) {
    		if (c == exitCommand) {
    			destroyApp(false);
    			notifyDestroyed();
    		} else if (c == saveCommand) {
    			saveCom();
    		} else if (c == cancelDisplaySavedInfoCommand) {
    			cancelDisplaySavedInfoCom();
    		}
    	} // This ends "public void commandAction(Command c, Displayable d)".
    
    
    } // This ends "public class DateToStringTest extends MIDlet implements CommandListener, ItemCommandListener".
    Thank you again, skalogir.

Similar Threads

  1. How to convert string to string array
    By murugacse4 in forum Mobile Java General
    Replies: 3
    Last Post: 2011-07-02, 10:12
  2. Convert DateField to string.
    By Erik0 in forum Mobile Java General
    Replies: 1
    Last Post: 2010-10-11, 19:31
  3. How convert String to Hex ?
    By valdonas in forum Symbian
    Replies: 2
    Last Post: 2008-12-03, 10:40
  4. How to convert UTF8 string to WCHAR string.
    By varungu in forum Open C/C++
    Replies: 1
    Last Post: 2008-01-31, 12:36
  5. convert to string
    By pmdr05 in forum Symbian
    Replies: 4
    Last Post: 2007-02-09, 07:15

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×