Pages

Friday 27 December 2019

Java 8 Date Time API


Why did Java 8 introduce a new date and time API when it already had classes such as Date and Calendar?
Below are the main reasons
1.Bad API design. For example, The Date class has both date and time components, if you only want time information and not date-related information, you have to set the date-related value to zero. Some aspects of the classes are unintuitive as well. For example, in the Date constructor, the range of date values is 1 to 31 but the range of month values is 0 to 11(not 1 to 12).
2.Java.util.Date,java.util.Calendar,java.time.SimpleDateFormatter classes are not thread-safe.

Java 8 provides very good support for date and time-related functionality in the newly introduced package java.time package.
Most of the classes in this package are immutable and thread-safe. We will learn LocalDate, LocalTime, LocalDateTime,Instant,Period, Duration,TemporalUnit and DateTimeFormatter.

The java.time package consists of four sub-packages
java.time.temporal: Access date/time fields and units.
java.time.format: Formats the input and output of date/time objects.
java.time.zone: Handles time zones.
java.time.chrono: Supports calendar systems such as Japanese and Thai Calendars.

LocalDate
java.time.LocalDate represents a date without time or time zone. LocalDate is represented in the ISO-8601 calendar system in a year-month-day format(YYYY-MM-DD). For example,2019-12-27.

How to get today's date?
LocalDate today = LocalDate.now();

LocalTime
java.time.LocalTime class is similar to LocalDate except that LocalTime represents time without dates or time zones. The time is in the ISO-8601 calendar system format HH:MM:SS: nanosecond.

How to get the current time?
LocalTime  currentTime = LocalTime.now();

LocalDateTime
This class java.time.LocalDateTime represents both data and time without time zones.
You can think of LocalDateTime as a logical combination of the LocalTime and LocalDate classes. The date and time formats use the ISO-8601 calendar system:
YYYY-MM-DD HH:MM: SS.nanosecond. For example,
LocalDateTime dateTime =LocalDateTime.now();

Instant
Represents machine time starting from Unix epoch(1970-01-01T00:00:00Z)
Typically used for timestamps
Instant instant = Instant.now();

What is the difference between LocalDateTime and Instant?
LocalDateTime uses the default time zone, but Instant does not.
For examaple,
LocalDateTime dateTime =LocalDateTime.now();//2019-12-27T15:27:27.139835100
Instant instant = Instant.now();//2019-12-27T09:57:27.139835100Z

Period
Represents the amount of time in terms of years, months and days.
Typically used for difference between two LocalDate objects.
How to find out when medicine will expire?
 LocalDate manufactureDate = LocalDate.of(2018,Month.MARCH,15);
 LocalDate expiryDate = LocalDate.of(2021, Month.JULY, 20);
 Period expire = Period.between(manufactureDate, expiryDate);
 System.out.println(expire);//P3Y4M5D, here P-> Period, Y-> Year, M-> Month, D-> Day
System.out.println(expire.getYears()+"Years "+expire.getMonths()+"Months "+expire.getDays()+"Days");//3Years 4Months 5Days

Duration
Represents the amount of time in terms of hours, minutes, seconds and fractions of seconds.
Typically used for difference between two LocalTime objects.

How to find out how many hours to go, to wish your best friend's birthday?
LocalDateTime comingMidnight = LocalDateTime.of(LocalDate.now().plusDays(1),LocalTime.MIDNIGHT);
LocalDateTime now1 = LocalDateTime.now();
Duration duration =Duration.between(now1, comingMidnight);
System.out.println(duration);//PT8H9M29.7853329S, Here PT-> Period Time, H-> Hour, M-> Minute, S->Second

TemporalUnit
The TemporalUnit interface is the part of java.time.temporal package.
It represents date or time units such as seconds, hours, days, months, years, etc.
The enumeration java.util.temporal.ChronoUnit implements TemporalUnit interface.
Instead of using constant values, it is better to use their equivalent enumeration values. Beacuase using enumeration values in ChronoUnit results in more readable code;further, you are less likely to make mistakes. For example,
System.out.println(Duration.of(1, ChronoUnit.MINUTES).getSeconds());//60
System.out.println(Duration.of(1, ChronoUnit.HOURS).getSeconds());//3600
System.out.println(Duration.of(1, ChronoUnit.DAYS).getSeconds());//86400

Time Zone
There are three important classes related to time zones that you need to know in order to work with dates and times across time zones.
  1. ZoneId
  2. ZoneOffset
  3. ZoneDateTime
ZoneId
The java.time.ZoneId class represents time zones. Time zones are typically identified using an offset from Greenwich Mean Time(GMT, also known as UTC/Greenwich).
For example, the time zone of India is Asia/Calcutta
System.out.println(ZoneId.systemDefault());//Asia/Calcutta

ZoneOffset
It represents the time-zone offset from UTC/Greenwich. For example, zone ID "Asia/Calcutta" has a zone offset of +5:30(plus 5 hours and 30 minutes) from UTC/Greenwich.
The ZoneOffset class is the child class of ZoneId class.

ZonedDateTime
If you want all three- date, time, and time zone together. For that, you can use the ZoneDateTime class.
LocalDate currentDate = LocalDate.now();
 LocalTime currentTime = LocalTime.now();
 ZoneId myZone = ZoneId.systemDefault();
 ZonedDateTime zoneDateTime = ZonedDateTime.of(currentDate,currentTime,myZone);
System.out.println(zoneDateTime);//2019-12-27T16:14:59.424905800+05:30[Asia/Calcutta]

Formatting Dates and Times
Using java.time.format.DateTimeFormatter class you can format the date and time according to your requirement.
DateTimeFormatter class provides many predefined constants for formatting date and time values.
Here is a list of a few such predefined formatters.
ISO_DATE(2019-12-27)
ISO_TIME(11:30:25.624)
ISO_ZONED_DATE_TIME(2019-12-27T16:14:59.424905800+05:30[Asia/Calcutta])
RFC_1123_DATE_TIME(Thu,26 Dec 2019 11:27:30+0530)
For example,
  LocalTime wakeupTime = LocalTime.of(6,0,0);
  System.out.println(DateTimeFormatter.ISO_TIME.format(wakeupTime));// 06:00:00

If you want to use a custom format instead of any of the predefined format. To do so, you can use the
ofPattern() method in the DateTimeFormatter class.
DateTimeFormatter customFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy");
System.out.println(customFormat.format(LocalDate.of(2019,Month.MARCH,01)));//01/03/2019

Important letters and their meanings for creating patterns for dates
G(era: BC,AD)
y(year of the era: 2020,20)
Y(week based year: 2020,20)
M(month:3,Mar,November)
w(week in year: 12)
W(week in month: 3)
E(day name in week: Sun, Sunday)
D(day of year: )
d(day of month)

Example:
class DateFormatter{
public static void main(String[] args) {
String[] dateFormats = { "d-M-y",
                                                 "dd-MM-yy",
                                                  "dd-MMM-yyy",
                                                  "dd-MM-yyyy", 
                                                   "D-MM-yyyy",
                                                   "DD-MM-yyyy",
                    "DDD-MM-yyyy",
                                                    "d - MMM- Y",
                                                    "w'th week of' YY",
                                                    "w'th week of' YYY",
                                                   "E, dd'th' MMMM,YYYY",
                    "EE, dd'th' MMMM,YYYY",
                                                    "EEE, dd'th' MMMM,YYYY",
                                                    "EEEE, dd'th' MMMM,YYYY" };
Arrays.stream(dateFormats)
.forEach(i -> System.out.println(DateTimeFormatter.ofPattern(i).format(LocalDate.now())));

}
}

Important letters and their meanings for creating patterns for time
a(marker for the text a.m/p.m marker)
H(hour:value range 0-23)
k(hour value range 1-24)
K(hour in an a.m/p.m :value range 0-11)
h(hour in a.m/p.m : value range 1-12)
m(minute)
s(second)
S(fraction of a second)
z(time zone: general time-zone format)

class TimeFormatter{
public static void main(String[] args) {
String[] timeFormats = {    "h:mm",
                                            "hh 'o''clock'",
                                            "H:mm a",
                                            "hh:mm:ss:SS",
                                            "K:mm:ss a"
                                                          };
Arrays.stream(timeFormats)
.forEach(i -> System.out.println(DateTimeFormatter.ofPattern(i).format(LocalTime.now())));

}
}

Saturday 14 December 2019

Collection

The collection framework is very important for fresher and experienced developers. You must have a good hold on it if you want to crack java interviews. You are supposed to prepare theoretical, coding and scenario-based questions related to this topic. I will try to explain each concept and interview questions of the Collection framework in a simple way. Almost, in all the interview it is asked that what is the difference between List, Set, Map, etc. How will you choose which Collection is suitable for a particular situation? With the help of the below diagram, you can answer the above questions with confidence. So please spend some time in the below diagram.

Collection Framework