String Concatenation
(3)
(String) Immutability
(1)
Once a String object is created, it is not allowed to change. It cannot be made larger or smaller, and you cannot change characters inside it.
The String Pool
(2)
String Methods
(12)
Creating a StringBuilder
(4)
StringBuilder Methods
(10)
StringBuilder vs. StringBuffer
When writing new code that concatenates a lot of String objects together, you should
use StringBuilder. StringBuilder was added to Java in Java 5. If you come across older code, you will see StringBuffer used for this purpose. StringBuffer does the same thing but more slowly because it is thread safe.
Creating Java Arrays
(8)
Array Methods
Creating an ArrayList
(6)
ArrayList Methods
(11)
Wrapper Classes
(8 + 2)
int primitive = Integer.parseInt(“123”);
Integer wrapper = Integer.valueOf(“123”);
Autoboxing
ince Java 5, you can just type the primitive value and Java will convert it to the relevant wrapper class for you.
Converting Between array and List
(2)
LocalDate
(4)
LocalDate Contains just a date—no time and no time zone. A good example of LocalDate is your birthday this year. It is your birthday for a full day regardless of what time it is.
LocalDate.now();
LocalDate date1 = LocalDate.of(2015, Month.JANUARY, 20);
LocalDate date2 = LocalDate.of(2015, 1, 20);
LocalTime
(5)
LocalTime time1 = LocalTime.of(6, 15);
LocalTime time2 = LocalTime.of(6, 15, 30);
LocalTime time3 = LocalTime.of(6, 15, 30, 200);
LocalDateTime
(9)
LocalDateTime Contains both a date and time but no time zone. A good example of LocalDateTime is “the stroke of midnight on New Year’s.” Midnight on January 2 isn’t nearly as special, and clearly an hour after midnight isn’t as special either.
java.time.LocalDateTime.now();
public static LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute)
public static LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second)
public static LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanos)
public static LocalDateTime.of(int year, Month month, int dayOfMonth, int hour, int minute)
public static LocalDateTime.of(int year, Month month, int dayOfMonth, int hour, int minute, int second)
public static LocalDateTime.of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanos)
public static LocalDateTime.of(LocalDate date, LocalTime)
Date Methods
Method LocalDate LocalTime LocalDateTime plus/minusYears yes no yes plus/minusMonths yes no yes plus/minusWeeks yes no yes plus/minusDays yes no yes plus/minusHours no yes yes plus/minusMinutes no yes yes plus/minusSeconds no yes yes plus/minusNanos no yes yes toEpochDay() yes no no toEpochTime() no no yes
Periods
Period annually = Period.ofYears(1); // every 1 year
Period quarterly = Period.ofMonths(3); // every 3 months
Period everyThreeWeeks = Period.ofWeeks(3); // every 3 weeks
Period everyOtherDay = Period.ofDays(2); // every 2 days
Period everyYearAndAWeek = Period.of(1, 0, 7);// every year and 7 days
Date Formatting
LocalDateTime dateTime = LocalDateTime.of(date, time);
System.out.println(date.format(DateTimeFormatter.ISO_LOCAL_DATE));
System.out.println(time.format(DateTimeFormatter.ISO_LOCAL_TIME)); System.out.println(dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
DateTimeFormatter shortDateTime = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
System.out.println(shortDateTime.format(dateTime)); // 1/20/20 System.out.println(shortDateTime.format(date)); // 1/20/20
System.out.println( shortDateTime.format(time)); // UnsupportedTemporalTypeException
MMMM M represents the month. The more Ms you have, the more verbose the Java output. For example, M outputs 1, MM outputs 01, MMM outputs Jan, and MMMM outputs January.
dd d represents the date in the month. As with month, the more ds you have, the more verbose the Java output. dd means to include the leading zero for a single-digit month.
hh h represents the hour. Use hh to include the leading zero if you’re outputting a single- digit hour.
: Use : if you want to output a colon. mm m represents the minute.
Parsing Dates and Time
DateTimeFormatter f = DateTimeFormatter.ofPattern(“MM dd yyyy”);
LocalDate date = LocalDate.parse(“01 02 2015”, f);
LocalTime time = LocalTime.parse(“11:22”);