Difficulty: 1/10
Purpose: To teach you how to display time in Java formats.
Reason: To show the time?
Result:
Procedure:
Step 1. Make sure you have the following at the top of your Java document.
Code:
import java.io.*;
import java.util.*;
import java.util.StringTokenizer;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.*;
import java.security.*;
What do they do?:
Those imports are what initiates Java into your document. There are many Java imports, as you can see, util.Calendar is what brings your computers calendar into Java.
Why do we need them?:
Because we are using Java, so we need the Java parts, otherwise it wouldn't make sense or do anything.
Step 2.
Using the time.
Now that we have imported Java, we need to use it. This will make it work properly. Find any void that is called by update();, for this I'm going to use writePlayers();
Step 3.
Implementing the time into a void.
First of all, we need to make the said void know what we are doing. Add these 2 lines at the top of your void.
Code:
SimpleDateFormat sdf = new SimpleDateFormat("h:mm:ss a");
String date = sdf.format(new Date());
What are they?:
The first line is an object, followed by a new instance being created of this object. The string is what our void is going to use to get the time. Every time the string "date" is called, it will update the "Date" using the SimpleDateFormat.
Why do we need them?:
To make our void know what terms we are on about.
Step 4.
Sending the time.
As shown in the picture, at the top of my screen is the time. Now you can move this to anywhere you like, but for easiness I will give you the frames in the picture. So in your void, add What does this do?:
This sends the time through a frame.
Why do we need this?:
To show the time at the top of the screen.
I hope you enjoyed this simple tutorial, I have tried my best to explain it and make you learn something. Any suggestions for improving this topic are welcome, and remember, if you can't say soemthing nice, don't say anything at all. Thanks.