Java 8 - Date & Time API (Joda-Time API)
Date & Time API (Joda-Time API):
* This is developed by joda.org.
* java.time is the package name. //imprt java.time.*;
v1.7:
Date, Calender, TimeStamp etc., - Lot of methods are not preferred to use.
v1.8:
LocalDate date = LocalDate.now(); // Current System Date YYYY-MM-DD
LocalTime time = LocalTime.now(); // Current System Time HH:mm:ss:nnn -nano second
Eg : 2020-04-13
int dd = date.getDayOfMonth(); // 13
int mm = date.getMonthValue(); // 04
int yyyy = date.getYear(); // 2020
Convert 13-07-2020
System.out.printf("%d-%d-%d",dd,mm,yyyy); //printf
Eg: 10:30:50:
int h = time.getHour(); // 10
int m = time.getMinute(); //30
int s = time.getSecond(); // 50
int n = time.getNano(); //
System.out.printf("%d:%d:%d:%d",h,m,s,n); //printf
Date & Time :
LocalDateTime dt = LocalDateTime.now(); // 2020-04-13T10:30:50:324
To get each seperatly we can use the same method which we use in LocalDate & LocalTime Object.
If you want to check some particular Date Time:
LocalDateTime dt = LocalDateTime.Of(yy,mm,dd,h,m,s,n);
Represent some particulare time
1993,May,23 10:30
LocalDateTime dt = LocalDateTime.Of(1993,05,23,10,30); // 05 or Month.May
How to add or less from current month, year,date,
dt.plusMonths(6);
dt.minusMonths(6);
Period:
To represent the quantity of the day.
Check your age ?
LocalDate birthday = LocalDate.Of(1993,05,13);
LocalDate today = LocalDate.now();
Period p = Period.between(birthday,today);
System.out.printf("Year %d Months %d Days %d",p.getYears(),p.getMonths(),p.getDays()); // printf
Check your balance days to die ?
LocalDate deathDate = LocalDate.Of(1993+60,05,13);
Period p1 = Period.between(today,deathDate);
int d = p1.getYears()*365+p1.getMonths()*30+p.getDays();
System.out.println("Your balance day to die %d",d);
Check the year is Leap year or not ?
Scanner sc = new Scanner();
S.o.p("Enter the Year:");
int n = sc.nextInt();
Year y = Year.Of(n);
if(y.isLeap()){
System.out.printf("%d year is Leap Year",n);
}else{
System.out.println("%d year is not Leap Year",n);
}
Internalization:
* To represent US or UK timing.
* ZoneId is the special class to refer Internalization. It means it will represent which zone you are.
Current System Default Zone:
ZoneId zone = ZoneId.SystemDefault();
Refer my own Zone and get Date and time for that particular Zone:
ZoneId la = ZoneId.Of("America/Los_Angels");
ZonedDateTime dt = ZonedDateTime.now(la);
* This is developed by joda.org.
* java.time is the package name. //imprt java.time.*;
v1.7:
Date, Calender, TimeStamp etc., - Lot of methods are not preferred to use.
v1.8:
LocalDate date = LocalDate.now(); // Current System Date YYYY-MM-DD
LocalTime time = LocalTime.now(); // Current System Time HH:mm:ss:nnn -nano second
Eg : 2020-04-13
int dd = date.getDayOfMonth(); // 13
int mm = date.getMonthValue(); // 04
int yyyy = date.getYear(); // 2020
Convert 13-07-2020
System.out.printf("%d-%d-%d",dd,mm,yyyy); //printf
Eg: 10:30:50:
int h = time.getHour(); // 10
int m = time.getMinute(); //30
int s = time.getSecond(); // 50
int n = time.getNano(); //
System.out.printf("%d:%d:%d:%d",h,m,s,n); //printf
Date & Time :
LocalDateTime dt = LocalDateTime.now(); // 2020-04-13T10:30:50:324
To get each seperatly we can use the same method which we use in LocalDate & LocalTime Object.
If you want to check some particular Date Time:
LocalDateTime dt = LocalDateTime.Of(yy,mm,dd,h,m,s,n);
Represent some particulare time
1993,May,23 10:30
LocalDateTime dt = LocalDateTime.Of(1993,05,23,10,30); // 05 or Month.May
How to add or less from current month, year,date,
dt.plusMonths(6);
dt.minusMonths(6);
Period:
To represent the quantity of the day.
Check your age ?
LocalDate birthday = LocalDate.Of(1993,05,13);
LocalDate today = LocalDate.now();
Period p = Period.between(birthday,today);
System.out.printf("Year %d Months %d Days %d",p.getYears(),p.getMonths(),p.getDays()); // printf
Check your balance days to die ?
LocalDate deathDate = LocalDate.Of(1993+60,05,13);
Period p1 = Period.between(today,deathDate);
int d = p1.getYears()*365+p1.getMonths()*30+p.getDays();
System.out.println("Your balance day to die %d",d);
Check the year is Leap year or not ?
Scanner sc = new Scanner();
S.o.p("Enter the Year:");
int n = sc.nextInt();
Year y = Year.Of(n);
if(y.isLeap()){
System.out.printf("%d year is Leap Year",n);
}else{
System.out.println("%d year is not Leap Year",n);
}
Internalization:
* To represent US or UK timing.
* ZoneId is the special class to refer Internalization. It means it will represent which zone you are.
Current System Default Zone:
ZoneId zone = ZoneId.SystemDefault();
Refer my own Zone and get Date and time for that particular Zone:
ZoneId la = ZoneId.Of("America/Los_Angels");
ZonedDateTime dt = ZonedDateTime.now(la);
Comments
Post a Comment