# Assignment #2a

## DateTime

filenames: DateTime.java, DateTime.class

Create the text-based application DateTime.java. This program should instantiate a Date object, then use that object's toString() method to display the current date and time to “Standard Out”. Be sure to upload your platform-independent source code to your mislab account public\_html directory, and be sure to actually test your program on your mislab account.

1\. Use Eclipse or a text editor to create a project named DateTime. (The instructions here assume that you are using Eclipse, but you can certainly type your program using a text editor.)

2\. Within your project, create a class named DateTime. This class file should contain:

\- a class header

\- a main method header

\- a statement to instantiate a Date object

Instantiating a Date object (an instance of the Date class) in Java is exactly like instantiating one in Javascript. Use the “new” operator. Since Java is a strongly typed language, you must define your variable first, such as:

```Java
Date myDate;
myDate=new Date();
```

This defines a variable named myDate, whose data type is Date (it is a Date object). It then uses the “new” operator to instantiate a Date object, and assigns that object to the variable myDate.

\- a statement to call your Date object's toString() method to get a string representation of the Date object (and assign that string to a String variable)

```Java
String dateString;
dateString=myDate.toString();
```

\- a statement to display the string

```Java
System.out.println(dateString);
```

<span class="qowt-font4-CourierNew" id="E1407">4</span><span class="qowt-font4-CourierNew" id="E1408">. You will notice that you have syntax errors – unresolved references – because the Date class is not in a standard </span><span class="qowt-font4-CourierNew" id="E1409">package</span><span class="qowt-font4-CourierNew" id="E1410"> that is automatically available to your program.</span>

<span class="qowt-font4-CourierNew" id="E1413"> Before your class header, import the classes from the java.util package:</span>

```Java
import java.util.*;
```

<span class="qowt-font4-CourierNew" id="E1419">5</span><span class="qowt-font4-CourierNew" id="E1420">. </span><span class="qowt-font4-CourierNew" id="E1421">Run your program. As a side-effect, this will also create your .class file.</span><span class="qowt-font4-CourierNew" id="E1422"> </span><span class="qowt-font4-CourierNew" id="E1423">Hopefull</span><span class="qowt-font4-CourierNew" id="E1424">y it will display the correct date and time</span><span class="qowt-font4-CourierNew" id="E1425"> on the Console</span><span class="qowt-font4-CourierNew" id="E1426">.</span>

<span class="qowt-font4-CourierNew" id="E1429">6</span><span class="qowt-font4-CourierNew" id="E1430">. </span><span class="qowt-font4-CourierNew" id="E1431">U</span><span class="qowt-font4-CourierNew" id="E1432">pload your DateTime.java file from this project</span><span class="qowt-font4-CourierNew" id="E1433"> to your mislab account. </span><span class="qowt-font4-CourierNew" id="E1434">The file will be in the src subdirectory, under your project folder in your designated workspace</span><span class="qowt-font4-CourierNew" id="E1435">.</span><span class="qowt-font4-CourierNew" id="E1436"> In your ftp program, drag just DateTime.java from the left (your computer) to your mislab public\_html directory.</span>

<span class="qowt-font4-CourierNew" id="E1439">7. ssh to mislab, log in, move to your public\_html directory, compile your program, and run your program to make sure it works.</span>

`<span class="qowt-font4-CourierNew" id="E1442"> cd public_html</span>`

`<span class="qowt-font4-CourierNew" id="E1444"> javac DateTime.java</span>`

`<span class="qowt-font4-CourierNew" id="E1446"> java DateTime</span>`