Exercise:
Write a Java Program to get current date and time.
1.DISPLAY IN default format
Click Here to View the Solution!
import java.time.LocalDateTime;
public
class CurrentDateTime {
public static void main(String[] args) {
LocalDateTime current = LocalDateTime.now();
System.out.println(
"Current Date and Time is: " + current);
}
}
Click Here to View the Output!
Current Date and Time is: 2020-11-28 T13:04:31.579
Click Here to View the Explanation!
- This program is used to get the current date and time by using the time package in Java.
- The
LocalDateTime
class is used for initializing a variable current that holds a LocalDateTime methodLocalDateTime.now()
. This method is used to store the current date and time in the variable current. - Finally, for displaying the current date and time in the default format, the LocalDateTime object is internally converted into a string through the
toString()
method.
2.DISPLAY with pattern
Click Here to View the Solution!
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public
class CurrentDateTime {
public static void main(String[] args) {
LocalDateTime current = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"yyyy-MM-dd HH:mm:ss.SSS");
String formatted = current.format(formatter);
System.out.println(
"Current Date and Time is: " + formatted);
}
}
Click Here to View the Output!
Current Date and Time is: 2020-11-28 13:06:57.489
Click Here to View the Explanation!
- This program is used for getting and displaying the current date and time pattern specified through a
formatter
. - For a specific format, the
DateTimeFormatter
class of the java.time package is used. The pattern of the format Year-Month-Day Hours:Minutes:Seconds:Milliseconds is specified in the program through theDateTimeFormatter
object and stored in theformatter
variable. - After the pattern format specification, the format() method of the
LocalDateTime
class is used to make the program use the definedformatter
. - Finally, the program gets the local date time in the pattern according to the
formatter
.
3.DISPLAY using predefined constants
Click Here to View the Solution!
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public
class CurrentDateTime {
public static void main(String[] args) {
LocalDateTime current = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
String formatted = current.format(formatter);
System.out.println(
"Current Date is: " + formatted);
}
}
Click Here to View the Output!
Current Date is: 20201128
Click Here to View the Explanation!
- This program is used to get the local data and time by using the predefined constant
BASIC_ISO_DATE
. - A formatter variable is initialized through the
DateTimeFormatter
class and holds a predefined constant of a format, BASIC_ISO_DATE that is used to get the date and time in the currentISO
date format. - After the ISO date format specification, the
format()
method of theLocalDateTime
class is used to make the program use the definedformatter
. - Finally, the program gets the local date time in the ISO date format according to the
formatter
.
2.DISPLAY in localized style
Click Here to View the Solution!
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public
class CurrentDateTime {
public static void main(String[] args) {
LocalDateTime current = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"yyyy-MM-dd HH:mm:ss.SSS");
String formatted = current.format(formatter);
System.out.println(
"Current Date and Time is: " + formatted);
}
}
<!-- /* Font Definitions */ @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-536870145 1073786111 1 0 415 0;} @font-face {font-family:Consolas; panose-1:2 11 6 9 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:-520092929 1073806591 9 0 415 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin-top:0in; margin-right:0in; margin-bottom:8.0pt; margin-left:0in; line-height:107%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:Calibri; mso-fareast-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} code {mso-style-noshow:yes; mso-style-priority:99; font-family:"Courier New"; mso-ascii-font-family:"Courier New"; mso-fareast-font-family:"Times New Roman"; mso-hansi-font-family:"Courier New"; mso-bidi-font-family:"Courier New";} pre {mso-style-noshow:yes; mso-style-priority:99; mso-style-link:"HTML Preformatted Char"; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Courier New"; mso-fareast-font-family:"Times New Roman";} span.HTMLPreformattedChar {mso-style-name:"HTML Preformatted Char"; mso-style-noshow:yes; mso-style-priority:99; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:"HTML Preformatted"; mso-ansi-font-size:10.0pt; mso-bidi-font-size:10.0pt; font-family:"Courier New"; mso-ascii-font-family:"Courier New"; mso-fareast-font-family:"Times New Roman"; mso-hansi-font-family:"Courier New"; mso-bidi-font-family:"Courier New";} span.hljs-keyword {mso-style-name:hljs-keyword; mso-style-unhide:no;} span.hljs-class {mso-style-name:hljs-class; mso-style-unhide:no;} span.hljs-title {mso-style-name:hljs-title; mso-style-unhide:no;} span.hljs-function {mso-style-name:hljs-function; mso-style-unhide:no;} span.hljs-params {mso-style-name:hljs-params; mso-style-unhide:no;} span.hljs-string {mso-style-name:hljs-string; mso-style-unhide:no;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:Calibri; mso-fareast-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} .MsoPapDefault {mso-style-type:export-only; margin-bottom:8.0pt; line-height:107%;} @page WordSection1 {size:8.5in 11.0in; margin:1.0in 1.0in 1.0in 1.0in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.WordSection1 {page:WordSection1;} -->
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public
class CurrentDateTime {
public static void main(String[] args) {
LocalDateTime current = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
String formatted = current.format(formatter);
System.out.println(
"Current Date is: " + formatted);
}
}
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public
class CurrentDateTime {
public static void main(String[] args) {
LocalDateTime current = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"yyyy-MM-dd HH:mm:ss.SSS");
String formatted = current.format(formatter);
System.out.println(
"Current Date and Time is: " + formatted);
}
}
Click Here to View the Output!
Current Date is: Nov 28, 2020 1:19:01 PM
Click Here to View the Explanation!
- This program is used for getting the local date and time in a localized style by using the
.oflocalizedDateTime()
method. - A formatter variable is initialized through the
DateTimeFormatter
class and holds a.oflocalizedDateTime()
method of theDateTimeFormatter
class. A format style Medium is set inside the method through the FormatStyle class of the java.time package in order to get the current date and time in the specified localized format. - After the localized format specification, the
format()
method of theLocalDateTime
class is used to make the program use the defined formatter. - Finally, the program gets the local date time in the localized medium format according to the
formatter
. - However, there are other format styles (Full, Long and Short) that can used with the
formatstyle
object to set the localized format of date and time.