SlideShare a Scribd company logo
DATE AND TIME
FORMATTER
FORMATTING DATE AND TIME
• One of the more powerful conversion
specifiers is %t. – to format time and date
information.
• The %t specifier works a bit differently than
the others because it requires the use of a
suffix to describe the portion and precise
format of the time or date desired.
• %tM, where M indicates minutes in a two-
character field.
SUFFIX
FORMATTING TIME AND DATE.
import java.util.*;
class TimeDateFormat
{
public static void main(String args[])
{
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
// Display standard 12-hour time format.
fmt.format("%tr", cal);
System.out.println(fmt);
// Display complete time and date
information.
fmt = new Formatter();
fmt.format("%tc", cal);
System.out.println(fmt);
// Display just hour and minute.
fmt = new Formatter();
fmt.format("%tl:%tM", cal, cal);
System.out.println(fmt);
// Display month by name and
number.
fmt = new Formatter();
fmt.format("%tB %tb %tm", cal, cal,
cal);
System.out.println(fmt);
}
}
Sample output is shown here:
// Display standard 12-hour time format.
09:17:15 AM
// Display complete time and date information.
Mon Jan 01 09:17:15 CST 2007
// Display just hour and minute.
9:17
// Display month by name and number
January Jan 01
The %n and %% Specifiers
• The %n and %% format specifiers differ from
the others in that they do not match an
argument.
• The %n inserts a newline.
• The %% inserts a percent sign.
// Demonstrate the %n and %% format specifiers.
import java.util.*;
class FormatDemo3
{
public static void main(String args[])
{
Formatter fmt = new Formatter();
fmt.format("Copying file%nTransfer is %d%% complete", 88);
System.out.println(fmt);
}
}
Output:
Copying file
Transfer is 88% complete
Specifying a Minimum Field Width
• An integer placed between the % sign and the
format conversion code acts as a minimum
field-width specifier.
• This pads the output with spaces to ensure
that it reaches a certain minimum length.
// Demonstrate a field-width specifier
import java.util.*;
class FormatDemo4
{
public static void main(String args[])
{
Formatter fmt = new Formatter();
fmt.format("|%f|%n|%12f|%n|%012f|",
10.12345, 10.12345, 10.12345);
System.out.println(fmt);
}
}
Specifying Precision
• Aprecision specifier can be applied to the %f,
%e, %g, and %s format specifiers.
• It follows the minimum field-width specifier (if
there is one) and consists of a period followed
by an integer.
• Its exact meaning depends upon the type of
data to which it is applied.
Using the Format Flags
• Formatter recognizes a set of format flags
that lets you control various aspects of a
Conversion.
• All format flags are single characters, and a
format flag follows the % in a format
specification.
Date and time

More Related Content

Viewers also liked

Köşe Takımları
Köşe TakımlarıKöşe Takımları
Köşe Takımları
köşe takımları
 
жумаш айжан+услуги+идея
жумаш айжан+услуги+идеяжумаш айжан+услуги+идея
жумаш айжан+услуги+идея
Айжан Жумаш
 
Funciones de los comites
Funciones de los comitesFunciones de los comites
Funciones de los comites
Elva Figueroa
 
Alice in wonderland adapted
Alice in wonderland   adaptedAlice in wonderland   adapted
Alice in wonderland adapted
Chantelle (Chelly) Ney
 
The Grammar of User Experience
The Grammar of User ExperienceThe Grammar of User Experience
The Grammar of User Experience
Stefano Bussolon
 
The X factor - defining the concept of Experience
The X factor - defining the concept of ExperienceThe X factor - defining the concept of Experience
The X factor - defining the concept of Experience
Stefano Bussolon
 

Viewers also liked (6)

Köşe Takımları
Köşe TakımlarıKöşe Takımları
Köşe Takımları
 
жумаш айжан+услуги+идея
жумаш айжан+услуги+идеяжумаш айжан+услуги+идея
жумаш айжан+услуги+идея
 
Funciones de los comites
Funciones de los comitesFunciones de los comites
Funciones de los comites
 
Alice in wonderland adapted
Alice in wonderland   adaptedAlice in wonderland   adapted
Alice in wonderland adapted
 
The Grammar of User Experience
The Grammar of User ExperienceThe Grammar of User Experience
The Grammar of User Experience
 
The X factor - defining the concept of Experience
The X factor - defining the concept of ExperienceThe X factor - defining the concept of Experience
The X factor - defining the concept of Experience
 

Similar to Date and time

Pi data link-2017-sp1-user-guide-es-05
Pi data link-2017-sp1-user-guide-es-05Pi data link-2017-sp1-user-guide-es-05
Pi data link-2017-sp1-user-guide-es-05
Yonel Sánchez
 
The following steps illustrate how to write a simple Level-2 MATLAB .pdf
The following steps illustrate how to write a simple Level-2 MATLAB .pdfThe following steps illustrate how to write a simple Level-2 MATLAB .pdf
The following steps illustrate how to write a simple Level-2 MATLAB .pdf
anonakitchen
 
CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
Aditya Vaishampayan
 
Making Custom Oscilloscope Measurements
Making Custom Oscilloscope MeasurementsMaking Custom Oscilloscope Measurements
Making Custom Oscilloscope Measurements
teledynelecroy
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C Language
Adnan Khan
 
F# Console class
F# Console classF# Console class
F# Console class
DrRajeshreeKhande
 
headerfilesinc-181121134545 (1).pdf
headerfilesinc-181121134545 (1).pdfheaderfilesinc-181121134545 (1).pdf
headerfilesinc-181121134545 (1).pdf
jazzcashlimit
 
Introduction to Input/Output Functions in C
Introduction to Input/Output Functions in CIntroduction to Input/Output Functions in C
Introduction to Input/Output Functions in C
Thesis Scientist Private Limited
 
prog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docx
prog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docxprog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docx
prog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docx
stilliegeorgiana
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
CHAPTER 4
CHAPTER 4CHAPTER 4
CHAPTER 4
mohd_mizan
 
Header files in c
Header files in cHeader files in c
Header files in c
HoneyChintal
 
Chap 2 input output dti2143
Chap 2  input output dti2143Chap 2  input output dti2143
Chap 2 input output dti2143
alish sha
 
Lecture10
Lecture10Lecture10
Input And Output
 Input And Output Input And Output
Input And Output
Ghaffar Khan
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
KarthikSivagnanam2
 
Log4j
Log4jLog4j
Log4j
vasu12
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
UMA PARAMESWARI
 
Functions
FunctionsFunctions
Functions
Swarup Boro
 
Data annotation validation (ASP.net)
Data annotation validation (ASP.net)Data annotation validation (ASP.net)
Data annotation validation (ASP.net)
Jyotasana Bharti
 

Similar to Date and time (20)

Pi data link-2017-sp1-user-guide-es-05
Pi data link-2017-sp1-user-guide-es-05Pi data link-2017-sp1-user-guide-es-05
Pi data link-2017-sp1-user-guide-es-05
 
The following steps illustrate how to write a simple Level-2 MATLAB .pdf
The following steps illustrate how to write a simple Level-2 MATLAB .pdfThe following steps illustrate how to write a simple Level-2 MATLAB .pdf
The following steps illustrate how to write a simple Level-2 MATLAB .pdf
 
CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
 
Making Custom Oscilloscope Measurements
Making Custom Oscilloscope MeasurementsMaking Custom Oscilloscope Measurements
Making Custom Oscilloscope Measurements
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C Language
 
F# Console class
F# Console classF# Console class
F# Console class
 
headerfilesinc-181121134545 (1).pdf
headerfilesinc-181121134545 (1).pdfheaderfilesinc-181121134545 (1).pdf
headerfilesinc-181121134545 (1).pdf
 
Introduction to Input/Output Functions in C
Introduction to Input/Output Functions in CIntroduction to Input/Output Functions in C
Introduction to Input/Output Functions in C
 
prog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docx
prog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docxprog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docx
prog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docx
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
 
CHAPTER 4
CHAPTER 4CHAPTER 4
CHAPTER 4
 
Header files in c
Header files in cHeader files in c
Header files in c
 
Chap 2 input output dti2143
Chap 2  input output dti2143Chap 2  input output dti2143
Chap 2 input output dti2143
 
Lecture10
Lecture10Lecture10
Lecture10
 
Input And Output
 Input And Output Input And Output
Input And Output
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Log4j
Log4jLog4j
Log4j
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Functions
FunctionsFunctions
Functions
 
Data annotation validation (ASP.net)
Data annotation validation (ASP.net)Data annotation validation (ASP.net)
Data annotation validation (ASP.net)
 

Recently uploaded

South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 

Recently uploaded (20)

South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 

Date and time

  • 2. FORMATTING DATE AND TIME • One of the more powerful conversion specifiers is %t. – to format time and date information. • The %t specifier works a bit differently than the others because it requires the use of a suffix to describe the portion and precise format of the time or date desired. • %tM, where M indicates minutes in a two- character field.
  • 4. FORMATTING TIME AND DATE. import java.util.*; class TimeDateFormat { public static void main(String args[]) { Formatter fmt = new Formatter(); Calendar cal = Calendar.getInstance(); // Display standard 12-hour time format. fmt.format("%tr", cal); System.out.println(fmt); // Display complete time and date information. fmt = new Formatter(); fmt.format("%tc", cal); System.out.println(fmt); // Display just hour and minute. fmt = new Formatter(); fmt.format("%tl:%tM", cal, cal); System.out.println(fmt); // Display month by name and number. fmt = new Formatter(); fmt.format("%tB %tb %tm", cal, cal, cal); System.out.println(fmt); } }
  • 5. Sample output is shown here: // Display standard 12-hour time format. 09:17:15 AM // Display complete time and date information. Mon Jan 01 09:17:15 CST 2007 // Display just hour and minute. 9:17 // Display month by name and number January Jan 01
  • 6. The %n and %% Specifiers • The %n and %% format specifiers differ from the others in that they do not match an argument. • The %n inserts a newline. • The %% inserts a percent sign.
  • 7. // Demonstrate the %n and %% format specifiers. import java.util.*; class FormatDemo3 { public static void main(String args[]) { Formatter fmt = new Formatter(); fmt.format("Copying file%nTransfer is %d%% complete", 88); System.out.println(fmt); } } Output: Copying file Transfer is 88% complete
  • 8. Specifying a Minimum Field Width • An integer placed between the % sign and the format conversion code acts as a minimum field-width specifier. • This pads the output with spaces to ensure that it reaches a certain minimum length.
  • 9. // Demonstrate a field-width specifier import java.util.*; class FormatDemo4 { public static void main(String args[]) { Formatter fmt = new Formatter(); fmt.format("|%f|%n|%12f|%n|%012f|", 10.12345, 10.12345, 10.12345); System.out.println(fmt); } }
  • 10. Specifying Precision • Aprecision specifier can be applied to the %f, %e, %g, and %s format specifiers. • It follows the minimum field-width specifier (if there is one) and consists of a period followed by an integer. • Its exact meaning depends upon the type of data to which it is applied.
  • 11. Using the Format Flags • Formatter recognizes a set of format flags that lets you control various aspects of a Conversion. • All format flags are single characters, and a format flag follows the % in a format specification.