Java 8 — Date and Time api (Joda api) | Code Factory
Index Page : Link
Reference Link : Link
Donate : Link
Date and Time API Introduction (Joda Time API) :
- Date and Time API / Joda Time API developed by joda.org
- Until 1.7v whatever classes and interfaces we have to represent Date values and Time values not upto the mark, with respect to convenience and with respect to performance, to overcome this problem java people introduced new Date and Time API in the 1.8v.
Example 1 :
package com.codeFactory.datetime;import java.time.LocalDate;
import java.time.LocalTime;public class Test {
public static void main(String... args) {
LocalDate date = LocalDate.now();
System.out.println(date);
LocalTime time = LocalTime.now();
System.out.println(time);
}
}
Output :
2020-05-08
13:30:49.580
Example 2 :
package com.codeFactory.datetime;import java.time.LocalDate;public class Test {
public static void main(String... args) {
LocalDate date = LocalDate.now();
System.out.println(date);
int dd = date.getDayOfMonth();
int mm = date.getMonthValue();
int yyyy = date.getYear();
System.out.format("%d-%d-%d", dd, mm, yyyy);
}
}
Output :
2020-05-08
8-5-2020
Example 3 :
package com.codeFactory.datetime;import java.time.LocalTime;public class Test {
public static void main(String... args) {
LocalTime time = LocalTime.now();
System.out.println(time);
int h = time.getHour();
int m = time.getMinute();
int s = time.getSecond();
int n = time.getNano();
System.out.format("%d:%d:%d:%d", h, m, s, n);
}
}
Output :
13:37:35.468
13:37:35:468000000
Example 4 :
package com.codeFactory.datetime;import java.time.LocalDateTime;public class Test {
public static void main(String... args) {
LocalDateTime dt = LocalDateTime.now();
System.out.println(dt);
int dd = dt.getDayOfMonth();
int mm = dt.getMonthValue();
int yyyy = dt.getYear();
System.out.format("%d-%d-%d", dd, mm, yyyy);int h = dt.getHour();
int m = dt.getMinute();
int s = dt.getSecond();
int n = dt.getNano();
System.out.format("\n%d:%d:%d:%d", h, m, s, n);
}
}
Output :
2020-05-08T13:42:12.678
8-5-2020
13:42:12:678000000
Example 5 :
package com.codeFactory.datetime;import java.time.LocalDateTime;public class Test {
public static void main(String... args) {
/*LocalDateTime.of(YYYY, MM, DD, H, M, S, N)*/
LocalDateTime dt = LocalDateTime.of(2020, 5, 8, 13, 50, 40, 150);
System.out.println(dt);
System.out.println("After 5 months : " + dt.plusMonths(5));
System.out.println("Before 5 months : " + dt.minusMonths(5));
}
}
Output :
2020-05-08T13:50:40.000000150
After 5 months : 2020-10-08T13:50:40.000000150
Before 5 months : 2019-12-08T13:50:40.000000150
Date and Time API Introduction Period & Year :
Example 1 :
package com.codeFactory.datetime;import java.time.LocalDate;
import java.time.Period;public class Test {
public static void main(String... args) {
LocalDate bDay = LocalDate.of(1991, 1, 9);
LocalDate today = LocalDate.now();
Period p = Period.between(bDay, today);
System.out.format("Age is %d Years %d Months %d Days.", p.getYears(), p.getMonths(), p.getDays());
}
}
Output :
Age is 29 Years 3 Months 29 Days.
Example 2 :
package com.codeFactory.datetime;import java.time.Year;public class Test {
public static void main(String... args) {
Year y = Year.of(1994);
if(y.isLeap()) {
System.out.println(y + " is leap year");
} else {
System.out.println(y + " is not leap year");
}
}
}
Output :
1994 is not leap year
Date and Time API Introduction ZoneId, ZonedDateTime :
package com.codeFactory.datetime;import java.time.ZoneId;
import java.time.ZonedDateTime;public class Test {
public static void main(String... args) {
ZoneId zone = ZoneId.systemDefault();
System.out.println(zone);
ZoneId la = ZoneId.of("America/Los_Angeles");
ZonedDateTime zdt = ZonedDateTime.now(la);
System.out.println(zdt);
}
}
Output :
Asia/Calcutta
2020-05-08T02:37:03.966-07:00[America/Los_Angeles]