"I remembered that I wanted to tell you a little about working with dates."

"You already told me that Java has the Date class, and I can use that class to work with dates."

"Hmm. Well, the Date class has been outdated for a while."

"Now it's recommended to instead use the Calendar class, which has a getTime() method that returns the current date."

"This is a Calendar object is usually created:"

Create a Calendar object
Calendar cal = Calendar.getInstance();

"When you call this method, the correct calendar is created based on your computer settings."

"The 'correct' calendar? Does that mean there are several?"

"Yep. Well, it would be more accurate to say 'relevant'. The fact is that Earth has not one, but many calendars. Almost every one of them is associated with some religion or country."

"The year may be different depending on what country you are in."

"Here are examples of the most common calendars."

Calendar class Calendar name
GregorianCalendar Christian Gregorian calendar
BuddhistCalendar Buddhist calendar
JapaneseImperialCalendar Japanese Imperial Calendar

"There is also the Chinese calendar, Islamic calendar, and many more."

"I see."

"To get the current date, you need to write code like this:"

Get the current time
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();

"The Calendar class has many methods that let you quickly get any information about a date and time."

Code Comments
Calendar calendar = Calendar.getInstance();

int era = calendar.get(Calendar.ERA);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);

int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);

era
year
month
day of the month

day of the week (Mon, Tue, Wed, …)
hour
minute
second

"Sometimes you really only need to get a piece of the available information. For example, the current year or day of the week."

"But sometimes you simply need to display the date in the right format."

"For example, in the log file or somewhere else."

"Or make a format that the user can customize. What do you do then?"

"There are special classes for this, too. The SimpleDateFormat class is perfectly suited for the task you described:"

How to display a date in the desired format
Calendar calendar = Calendar.getInstance();

DateFormat formatter = new SimpleDateFormat("MM-DD-YY");

String message = formatter.format(calendar.getTime());

"Ah. I remember. You already explained some about SimpleDateFormat to me, but I honestly don't remember much."

"It's all very simple. You create a SimpleDateFormat object and pass it the date pattern you want to get. Then you call the format method, and it gives you the passed date in the desired form."

"Sounds interesting. I'd like more details."

"Here you go. The details. Here are some of the letters that can be used in the date pattern:"

Letter Description
G Replaced by the era (AD or BC)
y Replaced by the year
M Replaced by the month
w Number of week in the year
W Number of week in the month
D Number of day in the year
d Day of the month
F Day of the week in the month
E Day of the week
a AM/PM (before or after noon)
H Hour in 24-hour format (0-23)
k Hour in 24-hour format (1-24)
K Hour in 12-hour format (0-11)
h Hour in 12-hour format (1-12)
m Minutes
s Seconds
S Milliseconds
z Time zone, formatted like this: Pacific Standard Time, PST
Z Time zone, formatted like this: -0800/td>

"Cool! That's just about everything you need."

"There are still some nuances related to repeating these letters."

"If you write YY, you'll get the last two digits of the year. If you write YYYY, you get all four digits of the year."

"There is also some complexity with months. MM is the number of the month. MMM is a three-letter abbreviation of the month, i.e. Jan, Feb, Mar, Apr, May, etc. MMMM stands for the full name of the month."

"You can also display the full day of the week (using EEEE) or just the first two letters (using EE)."

"Thanks, Rishi. This SimpleDateFormat class really is useful. Now I know."

"Enjoy using it! And good luck!"