This site requires JavaScript, please enable it in your browser!
Greenfoot back
coderr
coderr wrote ...

2014/2/24

Using the current date in greenfoot

coderr coderr

2014/2/24

#
hi, as part as a little project I'm trying to use the current date and time in my code. I'm having some difficulty displaying it, as i dont want it to appear in a terminal window, but for it to appear in the screen. I'm trying to do it by assigning the date time to an actor, but for some reason it doesnt seem to work. If someone could help me and tell me what is wrong that would be much appreciated! Here is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.awt.Color;
import javax.swing.*;
/**
 * Write a description of class Date here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TimeDate extends Actor
{
    /**
     * Act - do whatever the Date wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int x=1;
        if (x==1);
        {
            Date date1=Calendar.getInstance().getTime();
            GreenfootImage SetImage = new GreenfootImage(date1, 25, Color.GREEN, Color.BLACK);
            setImage(SetImage);
        }
    }
}
Hersov Hersov

2014/2/24

#
You want it as a string so in line 26 I advise changing ' date1 ' to ' ""+date1 '
You need to login to post a reply.