I'm trying to add the system date with a Label. What is wrong with the code

import java.util.*;

import javax.swing.*;

public class CurrentDateApplet extends JApplet

{

Calendar currentCalendar = Calendar.getInstance();

JLabel dateLabel = new JLabel();

JPanel mainPanel = new JPanel();

int dayInteger = currentCalendar.get(Calendar.DATE);

int monthInteger = currentCalendar.get(Calendar.MONTH)+1;

int yearInteger = currentCalendar.get(Calendar.YEAR);

public void init()

{

mainPanel.add(dateLabel);

setContentPane(mainPanel);

dateLabel.append(currentCalendar.get(Calendar.HOUR) + currentCalendar.get

(Calendar.MINUTE);

}

}

[652 byte] By [tadow126a] at [2007-11-27 1:04:37]
# 1

As for what's wrong with the code, it would be easier if you said: it doesn't show the date (it does this instead), it doesn't compile (I get this message) etc.

Anyway I'll assume you want to display the time in a label...

> dateLabel.append(currentCalendar.get(Calendar.HOUR) + currentCalendar.get

> (Calendar.MINUTE);

This won't compile: the parentheses are mismatched, and there is simply no such thing as append(). So we could trydateLabel.setText("" + currentCalendar.get(Calendar.HOUR) + currentCalendar.get(Calendar.MINUTE));

This wroks, but looks pretty nasty and it's not how you are supposed to format dates and times. Here's the unofficial party line, nicked from one of jverd's posts:

[url=http://www.javaworld.com/jw-12-2000/jw-1229-dates.html]Calculating Java dates: Take the time to learn how to create and use dates[/url]

[url=http://www.javaalmanac.com/egs/java.text/FormatDate.html]Formatting a Date Using a Custom Format[/url]

[url=http://www.javaalmanac.com/egs/java.text/ParseDate.html]Parsing a Date Using a Custom Format[/url]

From those links you should be able to find those applicable to times like this: http://www.exampledepot.com/egs/java.text/FormatTime.html

Using this approach you would end up with something like:import java.text.Format;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.swing.JApplet;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class CurrentDateApplet extends JApplet

{

private Date date;

private JLabel timeLabel;

private JPanel mainPanel;

public void init()

{

mainPanel = new JPanel();

timeLabel = new JLabel();

mainPanel.add(timeLabel);

setContentPane(mainPanel);

date = new Date();

Format formatter = new SimpleDateFormat("HH:ss a");

timeLabel.setText(formatter.format(date));

}

}

pbrockway2a at 2007-7-11 23:39:42 > top of Java-index,Java Essentials,Java Programming...