• 0

[Java] SimpleDateFormat


Question

import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ValidatingDates
{
	public static void DateFormating( String[] cmdArgs ) throws Exception
	{
 ?SimpleDateFormat sdfInput = new SimpleDateFormat( "dd-mm-yy" );
 ?SimpleDateFormat sdfOutput = new SimpleDateFormat ( "dd MMMMM, yyyy" );
 ?String textDate = JOptionPane.showInputDialog( "Enter date in format dd-mm-yy");
 ?
 ?Date date = sdfInput.parse( textDate );
 ?System.out.println( sdfOutput.format( date ) );
	}
}

Can somebody get this working for me? It compiles ok, but this comes up in the Java Console (1.4.2_04)

java.lang.ClassCastException

at sun.applet.AppletPanel.createApplet(Unknown Source)

at sun.plugin.AppletViewer.createApplet(Unknown Source)

at sun.applet.AppletPanel.runLoader(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

java.lang.ClassCastException

at sun.applet.AppletPanel.createApplet(Unknown Source)

at sun.plugin.AppletViewer.createApplet(Unknown Source)

at sun.applet.AppletPanel.runLoader(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Link to comment
Share on other sites

Recommended Posts

  • 0
as in, change it to public String DateFormating( String textDate ) throws Exception ?

Sure, or you could change Date date = sdfInput.parse( textDate ); to Date date = sdfInput.parse( cmdArgs );

Whichever suits you, it's just a name after all.

Link to comment
Share on other sites

  • 0

I just changed it to public String DateFormating( String textDate ) throws Exception, and now when I load the applet in the html file, it just freezes

Link to comment
Share on other sites

  • 0

Just loaded it up in NetBeans 3.6, and it's html file that it produced works fine.

but then it spits out this:

java.lang.Error: Do not use ValidatingDates.add() use ValidatingDates.getContentPane().add() instead

at javax.swing.JApplet.createRootPaneException(JApplet.java:203)

at javax.swing.JApplet.addImpl(JApplet.java:225)

at java.awt.Container.add(Container.java:307)

at ValidatingDates.init(ValidatingDates.java:12)

at sun.applet.AppletPanel.run(AppletPanel.java:353)

at java.lang.Thread.run(Thread.java:534)

Link to comment
Share on other sites

  • 0
Just loaded it up in NetBeans 3.6, and it's html file that it produced works fine.

but then it spits out this:

java.lang.Error: Do not use ValidatingDates.add() use ValidatingDates.getContentPane().add() instead

        at javax.swing.JApplet.createRootPaneException(JApplet.java:203)

        at javax.swing.JApplet.addImpl(JApplet.java:225)

        at java.awt.Container.add(Container.java:307)

        at ValidatingDates.init(ValidatingDates.java:12)

        at sun.applet.AppletPanel.run(AppletPanel.java:353)

        at java.lang.Thread.run(Thread.java:534)

Change it to that then. Not sure why, but I don't get that error anymore with just add();. Maybe they changed something in 1.5.0 (the version I'm using).

Link to comment
Share on other sites

  • 0

nevermind.. I found that.

now it works using NetBeans, but it treats the month as January, all the time.

Also, it doesn't work in the html file. Just fails and prints the above message, about .add()

Link to comment
Share on other sites

  • 0
nevermind.. I found that.

now it works using NetBeans, but it treats the month as January, all the time.

Also, it doesn't work in the html file. Just fails and prints the above message, about .add()

Odd, what's the code look like now?

Link to comment
Share on other sites

  • 0
/* Java applet to validate dates
 * Author: Chris Ede
 * Date: July 6, 2004
 */

// Import 
import java.applet.Applet;
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.*;

public class ValidatingDates extends JApplet
{
	public void init()
	{
  JLabel labelOutput = new JLabel("");
  this.getContentPane().add(labelOutput);
  String textDate = JOptionPane.showInputDialog( "Enter date in format dd-mm-yy");
  try
  {
 	 labelOutput.setText(DateFormating(textDate));
  } catch(Exception e)
  {
 	 labelOutput.setText("Illegal Date");
  }
	}

	public String DateFormating( String textDate ) throws Exception //Probably don't even need this method for something so simple
	{
  SimpleDateFormat sdfInput = new SimpleDateFormat( "dd-mm-yy" );
  SimpleDateFormat sdfOutput = new SimpleDateFormat ( "dd MMMM, yyyy" );
  Date date = sdfInput.parse( textDate );
  return sdfOutput.format( date );
	}
}

Link to comment
Share on other sites

  • 0

and my last question, hopefully.

how do I implement set2DigitYearStart?

It says that s2DigitYearStart goes from the year you insert, to year + 100.

I need it to go from 01-01-1980 to 31-12-2020... that's only 40 years

Link to comment
Share on other sites

  • 0

java.text.SimpleDateFormat

there's a method called set2DigitYearStart

For my applet, I have to make sure the date is between 01-01-1980 and 31-12-2020. Otherwise it's an "invalid date".

I was thinking of trying to use that to make the boundaries, because I've got no other ideas on how to make it happen.

Link to comment
Share on other sites

  • 0

Well, you could separate the string into 3 different parts using String mysubstring = mystring.substring(); and then converting each into an integer using int x = Integer.parseInt(mysubstring);

Link to comment
Share on other sites

  • 0

nah... I think I need to use an intrinsic function, because I need all invalid dates.

Like... instead of 29-02-05 being displayed as 01-03-05, it needs to be Invalid Date.

Link to comment
Share on other sites

  • 0
nah... I think I need to use an intrinsic function, because I need all invalid dates.

Like... instead of 29-02-05 being displayed as 01-03-05, it needs to be Invalid Date.

No, you could still do it that way. I'm not sure you could set a range for any of the built in date functions.

Link to comment
Share on other sites

  • 0

oh ok. I might just wait till school starts up, and ask my java teacher about it.

Thanks for all your help with this.. it's been a great help.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.