• 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

oh... there's more... chuck this on the end:

java.lang.IllegalArgumentException: null source

at java.util.EventObject.<init>(Unknown Source)

at java.awt.AWTEvent.<init>(Unknown Source)

at java.awt.event.ComponentEvent.<init>(Unknown Source)

at java.awt.event.WindowEvent.<init>(Unknown Source)

at java.awt.event.WindowEvent.<init>(Unknown Source)

at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.SequencedEvent.dispatch(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.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

  • 0

...please somebody...

java applets just elude me. I'm no good with them.

Just in case you don't understand what it's supposed to be doing, I'll explain a little.

The applet is supposed to get a date from the user, in the format dd-mm-yy. Then it uses SimpleDateFormat to output the same date in format dd month (MMMMM), year.

I've also got to change it so that the years are between 01-01-1980 and 31-12-2020. I've got a vague idea on how I'm going to do that, but any help would be greatful.

Link to comment
Share on other sites

  • 0

I wrote the applet as a program first, so all I really need to know is how to convert things so that it's appletable.

The import java.awt.*; got rid of most of the errors, now I'm just getting this:

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

  • 0

Probably because you're not extending applet?

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

public class ValidatingDates extends Applet
{
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 ) );
}
}

Link to comment
Share on other sites

  • 0

ok... so I extended applet. Now nothing happens.

I have a feeling that I'm not doing the UI part right. Can somebody help me with that?

Link to comment
Share on other sites

  • 0

If you are just trying to print the current date and time the code below does the trick. Its javascript, not a Java Applet (just a heads up)

&lt;script language="JavaScript"&gt;

function tS(){ x=new Date(tN().getUTCFullYear(),tN().getUTCMonth(),tN().getUTCDate(),tN().getUTCHours(),tN().getUTCMinutes(),tN().getUTCSeconds()); x.setTime(x.getTime()+dS()-21600000); return x; } 
function tN(){ return new Date(); } 
function dS(){ return ((tN().getTime()&gt;fD(0,3,1,1).getTime())&amp;&amp;(tN().getTime()&lt;fD(0,9,1,-1).getTime()))?3600000:0; } 
function fD(d,m,h,p){ var week=(p&lt;0)?7*(p+1):7*(p-1),nm=(p&lt;0)?m+1:m,x=new Date(tN().getUTCFullYear(),nm,1,h,0,0),dOff=0; if(p&lt;0){ x.setTime(x.getTime()-86400000); } if(x.getDay()!=d){ dOff=(x.getDay()&lt;d)?(d-x.getDay()):0-(x.getDay()-d); if(p&lt;0&amp;&amp;dOff&gt;0){ week-=7; } if(p&gt;0&amp;&amp;dOff&lt;0){ week+=7; } x.setTime(x.getTime()+((dOff+week)*86400000)); } return x; } 
function lZ(x){ return (x&gt;9)?x:'0'+x; } 
function tH(x){ if(x==0){ x=12; } return (x&gt;12)?x-=12:x; } 
function dT(){ if(fr==0){ fr=1; document.write(' &lt;font face="Futura XBlk BT" &gt;&lt;b&gt;&lt;span id="tP"&gt;'+eval(oT)+'&lt;/span&gt;&lt;/b&gt;&lt;/font&gt;'); } tP.innerText=eval(oT); setTimeout('dT()',1000); } 
function aP(x){ return (x&gt;11)?'PM':'AM'; } 
function y4(x){ return (x&lt;500)?x+1900:x; } 
var mN=new Array('January','February','March','April','May','June','July','August','September','October','November','December'),fr=0,oT="mN[tS().getMonth()]+' '+tS().getDate()+','+' '+y4(tS().getYear())+' '+tH(tS().getHours())+':'+lZ(tS().getMinutes())+' '+aP(tS().getHours())";
&lt;/script&gt;

&lt;!-- Clock Part 1 - Ends Here  --&gt;


&lt;!-- Clock Part 2 - This Starts/Displays Your Clock --&gt;

&lt;script language="JavaScript"&gt;dT();&lt;/script&gt;

&lt;!-- Clock Part 2 - Ends Here --&gt;&lt;!--webbot bot="Timestamp" i-checksum="34911" endspan --&gt;

Link to comment
Share on other sites

  • 0
The import java.awt.*; got rid of most of the errors,

It sounds like there's more to the problem then you're showing us. Adding the said import statement could only have solved a compile problem but you said that it compiled fine before.

Link to comment
Share on other sites

  • 0
ok... so I extended applet. Now nothing happens.

I have a feeling that I'm not doing the UI part right. Can somebody help me with that?

Well, we'd need to see what you're trying to do for the UI first.

Link to comment
Share on other sites

  • 0

I just want an applet where somebody inserts a date, in the format dd-mm-yy.

Then it uses SimpleDateFormat to process it, and output onto the screen the same date, but in the format dd month, yyyy

that's the bare bones basic description of this applet.

Link to comment
Share on other sites

  • 0
ok... so I extended applet. Now nothing happens.

I have a feeling that I'm not doing the UI part right. Can somebody help me with that?

Well, if that was your complete code, no wonder. You need an init() method for an Applet, this is like the regular main method in some ways. You may want to change the System.out.println to a label or something unless you plan to run it in appletviewer all the time, or they plan to have the console enabled in their browser.

This works though:

JAVA CODE:

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

public class ValidatingDates extends Applet
{
public void init() {
String s[] = {"Hello", "Bob"};
try {
DateFormating(s);
} catch(Exception e) { }
}
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 ) );
}
}

HTML FILE:

&lt;APPLET CODE=ValidatingDates.class WIDTH=800 HEIGHT=600&gt;
&lt;/APPLET&gt;

Link to comment
Share on other sites

  • 0

yeah... I have no idea on how to do applets, so I made the thing as a program first.

Even reading all the links that people have given me, I still couln't do applets.

..how do I set the output to a label? I have a copy of NetBeans here, too. If that helps.

Link to comment
Share on other sites

  • 0
yeah... I have no idea on how to do applets, so I made the thing as a program first.

Even reading all the links that people have given me, I still couln't do applets.

..how do I set the output to a label? I have a copy of NetBeans here, too. If that helps.

It's just like a normal GUI program. You use applet.add(child);. There's also JApplet which does Swing components under javax.swing.

JLabel x = new JLabel("");

this.add(x);

x.setText("blahblah");

Should do it.

Link to comment
Share on other sites

  • 0

ok... I'll try and give that a go.

I'm only doing Java, because it's a part of my web design course. This is just some stuff that I have to do over the holidays (ends next week).

We were supposed to be learning out of Deitel & Deitel's "Java? How to Program: 5th Edition", but the school only had 1 copy, which the teacher took. And he told us that he'll order in Ed 6 when it's released... but that's useless to me, because my Java component is now over!

Link to comment
Share on other sites

  • 0

ValidatingDates.java [42:1] non-static variable this cannot be referenced from a static context

this.add(output);

^

1 error

Errors compiling ValidatingDates.

oh... this is what I put in:

   JLabel output = new JLabel("");
    this.add(output);
    output.setText("The validated date is:\n" + sdfOutput.format(date));

Link to comment
Share on other sites

  • 0

It should actually be in your init() method. Is there any reason why you're making your DateFormating method static? Do you want it to be called outside without creating an object? It's probably better to pass the date in to the DateFormating method and return it in the new format.

Edited by kjordan2001
Link to comment
Share on other sites

  • 0

I think I've really stuffed this up::

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

public class ValidatingDates extends Applet
{
	public void init()
	{
  String s[] = {"Hello", "Bob"};
  try {
    DateFormating(s);
 	 }
  catch(Exception e)
  {
  }

	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");
  
  JLabel dateOutput = new JLabel("");
  this.add(dateOutput);
  dateOutput.setText("The formatted date is:\n" + sdfOutput.format(date));

//  Date date = sdfInput.parse( textDate );
//  System.out.println( sdfOutput.format( date ) );
	};
	};
}

Link to comment
Share on other sites

  • 0

import javax.swing.*;

import java.text.SimpleDateFormat;

import java.util.Date;

public class ValidatingDates extends JApplet

{

public void init() {

JLabel x = new JLabel("");

this.add(x);

String textDate = JOptionPane.showInputDialog( "Enter date in format dd-mm-yy");

try {

x.setText(DateFormating(textDate));

} catch(Exception e) {

x.setText("Invalid input");

}

}

public String DateFormating( String cmdArgs ) 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 MMMMM, yyyy" );

Date date = sdfInput.parse( textDate );

return sdfOutput.format( date );

}

}

Link to comment
Share on other sites

  • 0

cannot resolve symbol textDate.

does that mean that I need to move the String textDate = JOptionPane.showInputDialog( "Enter date in format dd-mm-yy"); down to the DateFormatting part?

Link to comment
Share on other sites

  • 0
cannot resolve symbol textDate.

does that mean that I need to move the String textDate = JOptionPane.showInputDialog( "Enter date in format dd-mm-yy"); down to the DateFormatting part?

Or you could use cmdArgs.

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.