I'm trying to change the user inputted date into a date object so that i can format it into a date format then display it in the selected format in the textarea

import java.awt.*; 
import java.awt.event.*;
import java.awt.CheckboxGroup;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class radiobutton extends Frame { 

  TextField textfield1; 
  TextField textfield2; 
  Button button;
  TextArea textarea = new TextArea ("Enter a Date. \nThen Slect \nwhich Formant",15,15,15);
  
  String temp; 


  public radiobutton() { 

    setSize(600, 400);
	setTitle("Date Format Program"); 
    setLayout(new FlowLayout()); 
 
    textfield1 = new TextField("", 8); 
     
    final CheckboxGroup checkbox = new CheckboxGroup();
	 
    button = new Button("Click Me");
	  
    add(textfield1);
    add(new Checkbox("MM/dd/yy", checkbox, false));
    add(new Checkbox("dd-MMM-yy", checkbox, false));
    add(new Checkbox("yy/MM/dd", checkbox, false));
	add(button); 
    add(textarea);
	 

    addWindowListener( new WindowAdapter() 
	 { 
      public void windowClosing(WindowEvent e) 
		{ 
        System.exit(0); 
      } 
    }
	 );

	  
    button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 

    	  	DateFormat df = new SimpleDateFormat();
    	  
    	  	checkbox.getSelectedCheckbox();
    	  	
    	  	String choice = checkbox.toString(), date, datechanger;
    	  	date = textfield1.getText();
    	  	
    	  	Date today = null;
    	  	
    	  	try {
				today = df.parse(date);
			} catch (ParseException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
    	  	
    	  	
    	  	
    	  	if(choice.equals("java.awt.CheckboxGroup[selectedCheckbox=java.awt.Checkbox[checkbox0,177,146,40x23,label=one,state=true]]"))
    	  	{
    	  		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy"); 
    	  		datechanger = simpleDateFormat.format(today);
    	  		textarea.append("\n");
    	  		textarea.append(datechanger );

    	  	}
			
			//System.out.println( checkbox );
    	  	else{
    	  		
    	  		textarea.append("\n");
    	  		textarea.append( choice );
    	  	}
      } 
    }); 
  } 

  public static void main(String[] args) 
  { 
	radiobutton myGuiExampleObject = new radiobutton(); 
    myGuiExampleObject.setVisible(true);	 
	  
  } 





}

You [arse using a default SimpleDateFormat - which is OK for printing, but far too detailed and complete to use as a parsing template for user date input. Better to create a SimpleDateFormat with a format that matches the current choice in your checkboxes.

You [arse using a default SimpleDateFormat - which is OK for printing, but far too detailed and complete to use as a parsing template for user date input. Better to create a SimpleDateFormat with a format that matches the current choice in your checkboxes.

I'm going to do that eventually but right now i need the program to be able to format of the user inputted date to a format of one of the selected checkboxes. The problem is that every time i try to format it, I get an error saying that it cannot parse the inputted data

For so,me reason the word "parse" came out as {****. I have no idea why!

Anyway. SimpleDateFormat parses the input according to the pattern/format string. The default format is far too detailed and complete to use as a parsing template.
You need to create a SimpleDateFormat with a format that matches what the user can type in. The user will expect to enter data according to the format that's checked in the check boxes, so that's the format you should use for parsing.

Member Avatar for hfx642

Whew... Good thing you said that. lol
I thought you were calling the OP an explitive name!!! lol

This is an assignment and the requirements are
- Ask users to enter a date into a text field box
- Ask the user to specify which date format to display as (using drop-down list or radio box with 3 options for 3 different date formats)
- When the 'Display' button is clicked, the program should display the correct date format in a text area box

Glad to see this "solved". Just for interest - what solution did you chose for parsing the user input? Thanks. J

Glad to see this "solved". Just for interest - what solution did you chose for parsing the user input? Thanks. J

I just solved it on my own. The problem was that I was formatting it incorrectly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.