×

Discussion Board

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

    Data typed into a date field is not saved when the cursor is not advanced.

    The code further below is for a MIDlet that obtains date information from a date field and some text fields, and then displays the sample information on another screen for testing purposes. Both the date field and the three text fields have information from the current date inside of them by default. When the "Save" button is pressed, a new screen appears with date information displayed. Near the top of that screen is a complete set of date information acquired from the date field. Below that is a version of the same date field data which has been parsed and formatted to appear in a mm/dd/yyyy format. Beneath that are three lines of data obtained from the text fields.

    Three similar things are occurring with the date field:

    On the main screen in the date field, if only one digit is typed for the month instead of two digits and the "Save" button is then immediately pressed, the newly entered month is not displayed on the next screen. Instead, whatever had been entered there previously is displayed.

    Likewise, if only one digit is typed for the day in the date field, when the "Save" button is immediately pressed thereafter, the newly entered day is also not displayed on the next screen.

    Similarly, if three or fewer digits are typed for the year in the date field, and the "Save" button is then immediately pressed, the newly entered year is also not displayed.

    If the cursor is moved to another part of the date field after typing any of the above, or if the cursor is moved from the date field to a text field, then the typed information is correctly displayed on the next screen after the "Save" button is pressed. (Typing two digits for the month, two digits for the day, or four digits for the year advances the cursor to another part of the date field.)

    In contrast to the date field, if only one digit is typed for the month or the day in the text fields, the correct information is displayed on the next screen. (The text field for the year is a special case; the MIDlet code is such that four digits must be entered there.)

    Does anyone know of a way for the one-digit month or day date field entries, or the year entries with three or fewer digits, to be saved without advancing the cursor?

    Below is the top half of the code from the MIDlet:

    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 DateToStringYearMonthDayTest extends MIDlet implements CommandListener, ItemStateListener, ItemCommandListener {
    //public class DateToStringYearMonthDayTest 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;
    //-----------------
    private int intCurrentYear;
    private int intCurrentMonth;
    private int intCurrentDayOfMonth;
    private String strCurrentYear;
    private String strCurrentMonth;
    private String strCurrentDayOfMonth;
    private TextField yearField;
    private TextField monthField;
    private TextField dayOfTheMonthField;
    private String strYearChoice;
    private String strMonthChoice;
    private String strDayOfTheMonthChoice;
    private int intSelectedIndex;
    private int intYearChoice;
    private int intMonthChoice;
    private int intDayOfTheMonthChoice;
    private int intTheYearModulo4;
    private int intTheYearModulo100;
    private int intTheYearModulo400;
    private int intSizeOfTheYear;
    private int intSizeOfTheMonth;
    private int intSizeOfTheDayOfTheMonth;
    private String strLeapYear;
    private String strParsedYear;
    private String strParsedMonth;
    private String strParsedDayOfTheMonth;
    private Date dtToday;
    private String strParsed2DigitMonth;
    
    public DateToStringYearMonthDayTest() {
    	// 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);
    	screen.setItemStateListener(this);
    } // This ends "public DateToStringYearMonthDayTest()".
    
    public void startApp() {
    
    	// Create a date field for the main screen.
    	dateChoiceField = new DateField("Date Choice:", DateField.DATE);
    	dateChoiceField.setItemCommandListener(this);
    	
    	// Fill the date field with today's date.
    	dtToday = new Date(System.currentTimeMillis());
    	dateChoiceField.setDate(dtToday);
    	
    	// Append the date field to the main screen.
    	screen.append(dateChoiceField);
    	
    	// Obtain the current date.
    	Calendar calendar = Calendar.getInstance();
    	
    	// Get the current year.
    	intCurrentYear = calendar.get(Calendar.YEAR);
    	strCurrentYear = Integer.toString(intCurrentYear);
    	
    	// Get the current month.
    	intCurrentMonth = calendar.get(Calendar.MONTH) + 1; // The months might be numbered 0-11 instead of 1-12.
    	strCurrentMonth = Integer.toString(intCurrentMonth);
    	
    	// Get the current day of the month.
    	intCurrentDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
    	strCurrentDayOfMonth = Integer.toString(intCurrentDayOfMonth);
    	
    	// Create a text field wherein the year can be entered, with the current year as the default value.
    	yearField = new TextField("Year: ", strCurrentYear, 4, TextField.NUMERIC);
    
    	// Create a text field wherein the month can be entered, with the current month as the default value.
    	monthField = new TextField("Month: ", strCurrentMonth, 2, TextField.NUMERIC);
    
    	// Create a text field wherein the day of the month can be entered, with the current day of the month as the default value.
    	dayOfTheMonthField = new TextField("Day of the Month: ", strCurrentDayOfMonth, 2, TextField.NUMERIC);
    	
    	// Display the text fields for entering the year, the month, and the day of the month.
    	screen.append(yearField);
    	screen.append(monthField);
    	screen.append(dayOfTheMonthField);
    	
    	// 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() {
    
    	// Determine the number of digits in the year, the month, and the day of the month that were entered.
    	intSizeOfTheYear = yearField.size();
    	intSizeOfTheMonth = monthField.size();
    	intSizeOfTheDayOfTheMonth = dayOfTheMonthField.size();
    	System.out.println("The size of " + intYearChoice + " is " + intSizeOfTheYear + ".");
    	System.out.println("The size of " + intMonthChoice + " is " + intSizeOfTheMonth + ".");
    	System.out.println("The size of " + intDayOfTheMonthChoice + " is " + intSizeOfTheDayOfTheMonth + ".");
    
    	// 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);
    	}
    	
    	// Parse strDateChoice to get the year, the month, and the day.
    	// Do an if statement to test for whether or not "-05:00" or some other number is after "GMT".
    	int intLengthOfDateChoice = strDateChoice.length();
    	// If "-05:00" or some other number is not present after "GMT", there will be fewer than 33 characters in the date string.
    	if (intLengthOfDateChoice < 33) {
    		// Parse for the 4-digit year value.
    		strParsedYear = strDateChoice.substring(24,28);
    	} else {
    		// Parse for the 4-digit year value.
    		strParsedYear = strDateChoice.substring(30,34);
    	}
    	// The below parsed month value consists of 3 letters.
    	strParsedMonth = strDateChoice.substring(4,7);
    	// Convert the 3-letter parsed month value to a 2-digit value.
    	if (strParsedMonth.equals("Jan")) {
    		strParsed2DigitMonth = "01";
    	} else if (strParsedMonth.equals("Feb")) {
    		strParsed2DigitMonth = "02";
    	} else if (strParsedMonth.equals("Mar")) {
    		strParsed2DigitMonth = "03";
    	} else if (strParsedMonth.equals("Apr")) {
    		strParsed2DigitMonth = "04";
    	} else if (strParsedMonth.equals("May")) {
    		strParsed2DigitMonth = "05";
    	} else if (strParsedMonth.equals("Jun")) {
    		strParsed2DigitMonth = "06";
    	} else if (strParsedMonth.equals("Jul")) {
    		strParsed2DigitMonth = "07";
    	} else if (strParsedMonth.equals("Aug")) {
    		strParsed2DigitMonth = "08";
    	} else if (strParsedMonth.equals("Sep")) {
    		strParsed2DigitMonth = "09";
    	} else if (strParsedMonth.equals("Oct")) {
    		strParsed2DigitMonth = "10";
    	} else if (strParsedMonth.equals("Nov")) {
    		strParsed2DigitMonth = "11";
    	} else if (strParsedMonth.equals("Dec")) {
    		strParsed2DigitMonth = "12";
    	}
    	// The below parsed day value consists of 2 digits.
    	strParsedDayOfTheMonth = strDateChoice.substring(8,10);
    	System.out.println("The parsed year is: " + strParsedYear + ".");
    	System.out.println("The parsed month is: " + strParsedMonth + ".");
    	System.out.println("The parsed 2-digit month is: " + strParsed2DigitMonth + ".");
    	System.out.println("The parsed day of the month is: " + strParsedDayOfTheMonth + ".");

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

    Re: Data typed into a date field is not saved when the cursor is not advanced.

    Below is the bottom half of the code:

    Code:
    	// Get the year, month, and day of the month as strings from the text fields.
    	strYearChoice = yearField.getString();
    	strMonthChoice = monthField.getString();
    	strDayOfTheMonthChoice = dayOfTheMonthField.getString();
    
    	// Get the year, month, and day of the month as integers from the strings.
    	if (strYearChoice.equals("")) {
    		intYearChoice = 0;
    	} else {
    		intYearChoice = Integer.parseInt(strYearChoice);
    	}
    	if (strMonthChoice.equals("")) {
    		intMonthChoice = 0;
    	} else {
    		intMonthChoice = Integer.parseInt(strMonthChoice);
    	}
    	if (strDayOfTheMonthChoice.equals("")) {
    		intDayOfTheMonthChoice = 0;
    	} else {
    		intDayOfTheMonthChoice = Integer.parseInt(strDayOfTheMonthChoice);
    	}
    		
    	// Display alert messages if the year has less than 4 digits, the month is higher than 12, or the day is higher than certain nummbers.
    	if (intSizeOfTheYear < 4) {
    		alert = new Alert("Invalid Year Number",  // This version does not permit continuing.
    						  "Cannot save. Please enter a year value with 4 digits.", null, AlertType.WARNING); // This version does not permit continuing.
    		alert.setTimeout(Alert.FOREVER);
    		display.setCurrent(alert);			
    	} else if (intSizeOfTheMonth < 1) {
    		alert = new Alert("Invalid Month Number",  // This version does not permit continuing.
    						  "Cannot save. Please enter a month value with 1 or 2 digits.", null, AlertType.WARNING); // This version does not permit continuing.
    		alert.setTimeout(Alert.FOREVER);
    		display.setCurrent(alert);			
    	} else if (intSizeOfTheDayOfTheMonth < 1) {
    		alert = new Alert("Invalid Day Number",  // This version does not permit continuing.
    						  "Cannot save. Please enter a day value with 1 or 2 digits.", null, AlertType.WARNING); // This version does not permit continuing.
    		alert.setTimeout(Alert.FOREVER);
    		display.setCurrent(alert);			
    	} else if (intMonthChoice > 12) {
    		alert = new Alert("Invalid Month Number",  // This version does not permit continuing.
    						  "Cannot save. Please enter a valid month value.", null, AlertType.WARNING); // This version does not permit continuing.
    		alert.setTimeout(Alert.FOREVER);
    		display.setCurrent(alert);	
    		
    	} else if (intMonthChoice == 2) {
    		intTheYearModulo4 = intYearChoice % 4;
    		intTheYearModulo100 = intYearChoice % 100;
    		intTheYearModulo400 = intYearChoice % 400;
    		System.out.println(intYearChoice + " modulo 4 is " + intTheYearModulo4 + ".");
    		System.out.println(intYearChoice + " modulo 100 is " + intTheYearModulo100 + ".");
    		System.out.println(intYearChoice + " modulo 400 is " + intTheYearModulo400 + ".");
    		if ((intYearChoice % 4 == 0) && (intYearChoice % 100 == 0) && (intYearChoice % 400 == 0)) {
    			// It is leap year.
    			strLeapYear = "Yes";
    			System.out.println("The year " + intYearChoice + " is a leap year.");
    		} else if ((intYearChoice % 4 == 0) && (intYearChoice % 100 == 0) && (intYearChoice % 400 != 0)) {
    			// It is not leap year.
    			strLeapYear = "No";
    			System.out.println("The year " + intYearChoice + " is not a leap year.");
    		} else if (intYearChoice % 4 == 0) {
    			// It is leap year.
    			strLeapYear = "Yes";
    			System.out.println("The year " + intYearChoice + " is a leap year.");
    		} else {
    			// It is not leap year.
    			strLeapYear = "No";
    			System.out.println("The year " + intYearChoice + " is not a leap year.");
    		}
    		//if (strLeapYear == "Yes" && intDayOfTheMonthChoice > 29) {
    		if ((strLeapYear == "Yes" && intDayOfTheMonthChoice > 29) || (strLeapYear != "Yes" && intDayOfTheMonthChoice > 28)) {
    			alert = new Alert("Invalid Day Number",  // This version does not permit continuing.
    							  "Cannot save. Please enter a valid day value.", null, AlertType.WARNING); // This version does not permit continuing.
    			alert.setTimeout(Alert.FOREVER);
    			display.setCurrent(alert);	
    		} else {
    			displaySavedInfo();
    		}
    	} else if ((intMonthChoice == 4 || intMonthChoice == 6 || intMonthChoice == 9 || intMonthChoice == 11) && intDayOfTheMonthChoice > 30) {
    			alert = new Alert("Invalid Day Number",  // This version does not permit continuing.
    							  "Cannot save. Please enter a valid day value.", null, AlertType.WARNING); // This version does not permit continuing.
    			alert.setTimeout(Alert.FOREVER);
    			display.setCurrent(alert);	
    	} else if (intDayOfTheMonthChoice > 31) {
    			alert = new Alert("Invalid Day Number",  // This version does not permit continuing.
    							  "Cannot save. Please enter a valid day value.", null, AlertType.WARNING); // This version does not permit continuing.
    			alert.setTimeout(Alert.FOREVER);
    			display.setCurrent(alert);	
    	} else {
    		displaySavedInfo();
    	}
    	
    } // This ends saveCom().
    
    public void displaySavedInfo() {
    	// 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")); // This line is for testing purposes.
    	frmDisplaySavedInfo.append(new StringItem("", strDateChoice + "\n")); // This line is for testing purposes.
    	frmDisplaySavedInfo.append(new StringItem("", strParsed2DigitMonth + "/" + strParsedDayOfTheMonth + "/" + strParsedYear + "\n")); // This line is for testing purposes.
    	frmDisplaySavedInfo.append(new StringItem("", "Year Choice:  " + strYearChoice + "\n"));
    	frmDisplaySavedInfo.append(new StringItem("", "Month Choice:  " + strMonthChoice + "\n"));
    	frmDisplaySavedInfo.append(new StringItem("", "Day of the month Choice:  " + strDayOfTheMonthChoice + "\n"));
    	
    	// Set the current display to the save form.
    	display.setCurrent(frmDisplaySavedInfo);
    } // This ends public void displaySavedInfo().
    
    public void cancelDisplaySavedInfoCom() {
    	display.setCurrent(screen);
    } // This ends cancelDisplaySavedInfoCom().
    
    public void itemStateChanged(Item item) {	
    	if (item == dateChoiceField) {
    		
    	}
    } // This ends "public void itemStateChanged(Item item)".
    	
    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 DateToStringYearMonthDayTest extends MIDlet implements CommandListener, ItemCommandListener".

Similar Threads

  1. Replies: 1
    Last Post: 2011-02-08, 14:41
  2. Form field--date and time in same field
    By wailel in forum Symbian
    Replies: 0
    Last Post: 2010-02-13, 02:46
  3. Compare DATE-Field with actual DATE
    By jensesaat in forum Symbian
    Replies: 2
    Last Post: 2007-01-10, 12:53
  4. Replies: 0
    Last Post: 2003-11-16, 21:13
  5. how to get the sum of digits of a date from a date field
    By SangeetaVepa in forum Mobile Java General
    Replies: 0
    Last Post: 2003-09-04, 08:37

Posting Permissions

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