soflux
Earning Insights Analyst
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
400 XP
Introduction:
This page will teach you about using the Date import in Java to output or get the current Time and/or Date.
When is this needed?
Adding a simple clock or date counter to an application adds a slightly more professional look to the overall appeal of the program, as well as having the time handy is always a good thing.
What imports are needed?
The imports we need to get the current Date and Time are;
these allows us to get the Date and Time, and then format them the way we want to.
Example:
Here is an example of sending the String "Current Date: " followed by the date in Day DayDay.Month.Month.Year.Year.Year.Year format, followed by a similar technique for the date:
As you can probably tell, each character in the SimpleDateFormat stands for something different...
E - Day in full letters
d - Day in numeric values
M - Month in numeric values
y - Year in numeric values
h - Hours in numeric values
m - Minutes in numeric values
s - Seconds in numeric values
a - "AM"/"PM"
z - Timezone.
The output given from the example is:
Current Date: Sun 01.12.2013. Current Time: 09:29:31 PM GMT
Finished!
Book traversal links for Date and Time
This page will teach you about using the Date import in Java to output or get the current Time and/or Date.
When is this needed?
Adding a simple clock or date counter to an application adds a slightly more professional look to the overall appeal of the program, as well as having the time handy is always a good thing.
What imports are needed?
The imports we need to get the current Date and Time are;
- import
java.util.Date
;
- import
java.text.SimpleDateFormat
;
these allows us to get the Date and Time, and then format them the way we want to.
Example:
Here is an example of sending the String "Current Date: " followed by the date in Day DayDay.Month.Month.Year.Year.Year.Year format, followed by a similar technique for the date:
- Date
dNow =
new
Date
(
)
;
- SimpleDateFormat
ft =
new
SimpleDateFormat
(
"E dd.MM.yyyy'. Current Time:' hh:mm:ss a zzz"
)
;
- System
.out
.println
(
"Current Date: "
+
ft.format
(
dNow)
)
;
As you can probably tell, each character in the SimpleDateFormat stands for something different...
E - Day in full letters
d - Day in numeric values
M - Month in numeric values
y - Year in numeric values
h - Hours in numeric values
m - Minutes in numeric values
s - Seconds in numeric values
a - "AM"/"PM"
z - Timezone.
The output given from the example is:
Current Date: Sun 01.12.2013. Current Time: 09:29:31 PM GMT
Finished!
Book traversal links for Date and Time
- ‹ Comments
- Up
- Debugging Programs ›