[go: up one dir, main page]

0% found this document useful (0 votes)
117 views20 pages

Date & Calendar: Java SE

Java provides two main classes for working with dates and times - Date and Calendar. The Date class represents an instant in time and stores the number of milliseconds since January 1, 1970. The Calendar class handles common calculations related to dates and times such as parsing, formatting, adding, and rolling fields. Formatting and parsing dates can be done using date/time format patterns and the DateFormat class.

Uploaded by

sissthwae oo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views20 pages

Date & Calendar: Java SE

Java provides two main classes for working with dates and times - Date and Calendar. The Date class represents an instant in time and stores the number of milliseconds since January 1, 1970. The Calendar class handles common calculations related to dates and times such as parsing, formatting, adding, and rolling fields. Formatting and parsing dates can be done using date/time format patterns and the DateFormat class.

Uploaded by

sissthwae oo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Date & Calendar

Java SE
Contents

• Overview

• Date Objects

• Calendar Object

• Formatting Date
Overview
• အခ#iန&အ'( ကiuယ&စ'(-ပuရန& java.util.Date အ'( အသu2(-ပuခ345က

• အခ#iန&အ'( Millisecond အထieဖ'&-ပအသu2(-ပu9iuင&

• JDK 1.1 ကတည&(က e94၊ လ၊ 9?စ&၊ န'ရ@၊ မiနစ&အစBiသည&တiuအ'( Interpret လuပ&9iuင&eသ'


Method မ#'(ပDဝင&ခ34eသ'&လည&( Internationalization အတFက& အစG&မe-ပeသ'e5က'င&4
သiပ&Hပ@( အသu2(မ-ပuသင&4

• eန'က&ပiuင&(တFင& အဆiuပD Interpreting eဆ'င&JFက&ခ#က&မ#'(အတFက& java.util.Calendar


အ'( အသu2(-ပuခ34

• Date မ? String ဒDမ?မဟuတ& String မ? တဆင&4 Date အ'( e-ပ'င&(လည&(ရန&


java.text.DateFormat Class အ'( အသu2(-ပuခ34

• JDK 1.8 အeရ'က&တFင& Date and Time API အသစ&အ'( အစ'(ထiu(ရန& အသစ&-ဖM&4စFက&ခ34
Date Object

Creation Interpretation Formatting


Creation

Using Constructors

From Calendar Object

Parsing by DateFromat
Using Constructors
• Date()

Date Instance အ'( Initiliaze လuပ&တ34 အခ#iန&ကiu မ@လ@စကNန&4934 ယOHပ@(
တည&eဆ'က&

Date date = new Date()

• Date(long time)

Parameter တFင&ပDဝင&eသ' Standard Base Time (January 1, 1970,
00:00:00 GMT) မ? မ@လ@စကNန&4အခ#iန&9?င&4 Date Object အ'( တည&eဆ'က&

-မန&မ'-ပည&ပတ&ဝင&(က#င&မ?'အသu2(-ပuပDက Thu Jan 01 06:30:00 MMT
1970 အ'( အe-ခခ2၍ eဖ'&-ပမည&-ဖစ&သည&

long time = ……;

Date date = new Date(time);
From Calendar Object
• Calendar Class ၏ Instance မ?တဆင&4လည&( Date Object အ'( ရBi9iuင&ပDသည&။

public static void main(String[] args) {


Calendar cal = Calendar.getInstance();
Date date = cal.getTime();

System.out.println(date.toString());
}

Calendar Class 9?င&4 ပတ&သက&၍ eန'က&ပiuင&(တFင& အeသ(စiတ&eဖ'&-ပပDမည&။


Parsing by DateFormat
• DateFormat Object မ?လည&( Format ခ#ထ'(eသ' String မ#'(မ?တဆင&4 Date Object အ'( ရယO9iuင&ပDသည&

• Date Constructor 9?င&4 Calendar Class တiuသည& လက&Biအခ#iန&အ'( ရယOရ'တFင& အစG&e-ပeသ'&လည&(


သတ&မ?တ&ထ'(eသ' အခ#iန&တစ&ခuမ? Date Object အ'( ရယOရ'တFင& Date Format မ? Parse လuပ&-ခင&(ကiu
eJF(ခ#ယ&သင&4ပDသည&

try {

DateFormat df = new SimpleDateFormat("yyyy/MM/dd");


Date date = df.parse("2000/01/01");
System.out.println(date.toString());

} catch (ParseException e) {
e.printStackTrace();
}
Calendar Interpretation

Creation

Interpretation for
Calendar Fields

Adding and Rolling


Creation of Calendar
• Calendar Class ၏ Static Method getInstance မ? တ
ဆင&4 Calendar Object အ'( တည&eဆ'က&9iuင&

• getInstance()

• getInstance(Locale aLocale)

• getInstance(TimeZone zone)

• getInstance(Locale aLocale, TimeZone zone)


Getting & Setting Calendar
Fields
• Set method မ#'(အ'( အသu2(-ပu၍ Calendar Fields Value မ#'(အ'( သတ&မ?တ&9iuင&

• Get method မ#'(အ'( အသu2(-ပu၍ Calendar Fields Value မ#'(အ'( ရယO9iuင&

• Fields of Date

• YEAR + MONTH + DAY_OF_MONTH

• YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK

• YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK

• YEAR + DAY_OF_YEAR

• YEAR + DAY_OF_WEEK + WEEK_OF_YEAR

• Fields of Time of Day

• HOUR_OF_DAY

• AM_PM + HOUR
Sample
public static void doGet(Calendar c) {
// get
System.out.println(c.get(Calendar.YEAR));
System.out.println(c.get(Calendar.MONTH));
System.out.println(c.get(Calendar.DAY_OF_MONTH));
System.out.println(c.get(Calendar.HOUR_OF_DAY));

System.out.println(c.getTimeInMillis());
System.out.println(c.getTime());
}
Sample
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
// before setting
doGet(c);

// setting
c.set(Calendar.YEAR, 2010);
c.set(Calendar.MONTH, 12);
c.set(Calendar.DAY_OF_MONTH, 15);

// after setting
doGet(c);
}
Leniency
• Calendar Class တFင& Calendar Fields မ#'(အ-ဖစ&e-ပ'င&(လည&(ရ'တFင&
Mode 9?စ&မ#iu(BiပDသည&။

• Lenient Mode

Calendar Field အ'( ဆက&တiuက&တFက&ခ#က&9iuင&သည&

c.set(Calendar.DAY_OF_MONTH, 45)

လက&Biလ၏ရက&အ'(9Sတ&၍ eန'က&လ၏ရက&စF3အ'( eဖ'&-ပမည&

• Non-Lenient Mode

အထက&ပDအတiuင&( တFက&ခ#က&ပDက java.lang.IllegalArgumentException
အ'( -ဖစ&eပTeစမည&။

• Leniency အ'( သတ&မ?တ&ရန& Calendar.setLenient method အ'( အသu2(-ပu


9iuင&
Adding and Rolling Fields
Operation Description

အခ#iန&ရက&9?စ&လတiuအ'( အeပDင&(အ9Sတ& -ပuလuပ&လiuပDက


ADD
အသu2(-ပu9iuင&

လက&Bi Field ထက&Uက@(eသ' Field အ'( မe-ပ'င&(လည&(


ROLL
eစပ3 အeပDင&(အ9Sတ& -ပuလuပ&
Sample
public static void main(String[] args) throws
ParseException {
Date d = new
SimpleDateFormat("yyyyMMdd").parse("20130410");
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();

c1.setTime(d);
c2.setTime(d);

c1.add(Calendar.DAY_OF_MONTH, -15);
c2.roll(Calendar.DAY_OF_MONTH, -15);

System.out.println(c1.getTime());
System.out.println(c2.getTime());
}
Formatting Date
• Date Object အ'( String အ-ဖစ& Format လuပ&လiuသM&4
အခDတFင&၎င&(၊ Format ခ#ထ'(eသ' String မ?တဆင&4 Date
Object အ'( Parse လuပ&လiuသM&4အခDတFင&၎င&( အသu2(-ပu

• DateFormat သည& Abstract Class -ဖစ&Hပ@(၊ Concrete


Class မ?' SimpleDateFormat Class -ဖစ&၏

• Format လuပ&လiuသM&4 အခD format(date date)

• Parse လuပ&လiuသM&4 အခD parse(String source)


Date and Time Pattern
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day name in week Text Tuesday; Tue
Day number of week (1 =
u Number 1
Monday, ..., 7 = Sunday)
Date and Time Pattern
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978

Pacific Standard Time; PST;


z Time zone General time zone
GMT-08:00

Z Time zone RFC 822 time zone -0800


X Time zone ISO 8601 time zone -08; -0800; -08:00
Sample
Date and Time Pattern Result
"yyyy.MM.dd G 'at' HH:mm:ss z" 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy" Wed, Jul 4, '01
"h:mm a" 12:08 PM
"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time
"K:mm a, z" 0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa" 02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z" Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ" 010704120856-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" 2001-07-04T12:08:56.235-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX" 2001-07-04T12:08:56.235-07:00
"YYYY-'W'ww-u" 2001-W27-3

You might also like