DateFormat syntax error

Using the Java SDK with TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
User avatar
jon80
Posts: 24
Joined: Thu May 28, 2009 10:03 am

DateFormat syntax error

Post by jon80 »

The following code does not compile in TextPad 5.4.2, although similar code compiles within Eclipse.

Code: Select all

import java.text.*;
import java.util.*;
class DateFormatPlay
{
	Calendar c = Calendar.getInstance();
	c.set(2012, 11, 25); //christmas day due to zero based indexed month
	Locale l = new Locale("it", "IT");
	DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
	System.out.println(df.format(c.getTime()));
}
Error
C:\Documents and Settings\Jon\Desktop\DateFormatPlay.java:6: error: <identifier> expected
c.set(2012, 11, 25); //christmas day due to zero based indexed month
^
C:\Documents and Settings\Jon\Desktop\DateFormatPlay.java:6: error: illegal start of type
c.set(2012, 11, 25); //christmas day due to zero based indexed month
^
C:\Documents and Settings\Jon\Desktop\DateFormatPlay.java:6: error: illegal start of type
c.set(2012, 11, 25); //christmas day due to zero based indexed month
^
C:\Documents and Settings\Jon\Desktop\DateFormatPlay.java:6: error: illegal start of type
c.set(2012, 11, 25); //christmas day due to zero based indexed month
^
C:\Documents and Settings\Jon\Desktop\DateFormatPlay.java:9: error: <identifier> expected
System.out.println(df.format(c.getTime()));
^
C:\Documents and Settings\Jon\Desktop\DateFormatPlay.java:9: error: <identifier> expected
System.out.println(df.format(c.getTime()));
^
C:\Documents and Settings\Jon\Desktop\DateFormatPlay.java:9: error: ';' expected
System.out.println(df.format(c.getTime()));
^
C:\Documents and Settings\Jon\Desktop\DateFormatPlay.java:9: error: illegal start of type
System.out.println(df.format(c.getTime()));
^
C:\Documents and Settings\Jon\Desktop\DateFormatPlay.java:9: error: ';' expected
System.out.println(df.format(c.getTime()));
^
9 errors

Tool completed with exit code 1

Usually when I post this as a bug, the technical support team refuses it, for some reason unknown, so I posted the issue here. The code within Eclipse reads as follows:


Code: Select all

package code;
import java.text.*;
import java.util.*;
class DateFormatPlay
{
	public static void main (String[] args)
	{	Calendar c = Calendar.getInstance();
		c.set(2012, 11, 25); //Christmas Day
		Locale l = new Locale("it", "IT");
		DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
		System.out.println(df.format(c.getTime()));
	}
}

Output
25-Dec-2012

Is there a way I can re-write this code in single lines, other than writing a function? :)
Jon
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Re: DateFormat syntax error

Post by MudGuard »

jon80 wrote: Usually when I post this as a bug, the technical support team refuses it
Does this surprise you? Why should textpad support team fix your programming errors?

Even if it were not your fault, but a fault of the java compiler: the java compiler is no helios product, so why should helios care about bugs in non-helios products?

Java code never compiles in Textpad, simply because Textpad is a text editor, not a compiler. All it does with your (faulty) code is pass it on to the java compiler.


For your Java code:

It is not allowed in Java to put "normal" code in the class without wrapping it in a function.
It is allowed to put member declarations there, together with member initialisation.
That is why the
Calendar c = Calendar.getInstance();
line is ok.

So your first example is simply wrong. It will not compile not regarding which program you use to start the compiler.

The algorithm in your second example is wrapped in a function. This is a very important difference.
User avatar
jon80
Posts: 24
Joined: Thu May 28, 2009 10:03 am

Re: DateFormat syntax error

Post by jon80 »

Oops, sorry about that, I should take coffee before coding :D
Jon
Post Reply