Mutable string class

The basic concept

  • Because the contents of the String described by the String class are constant and immutable, when a large number of similar strings need to be described in Java code, they can only be applied for and stored separately, resulting in a waste of memory space.
  • To solve the above problem, the java.lang.StringBuilder class and the java.lang.StringBuffer class can be used to describe strings in which character sequences can be changed.
  • The StringBuffer class has been around since jdk1.0 and is thread-safe and therefore inefficient.
  • The StringBuilder class is a non-thread-safe class that has been around since JDK1.5.

A common constructor of the StringBuilder class

Method statement Function is introduced
StringBuilder() Construct an object with no arguments and capacity of 16
StringBuilder(int capacity) The object is constructed based on the capacity specified by the parameter, which specifies the size
StringBuilder(String str) Constructs an object from the string specified by the argument. The capacity is 16+ string length

Member methods commonly used by the StringBuilder class

Method statement Function is introduced
int capacity() Used to return the capacity of the calling object
int length() Used to return the length of the string, that is, the number of characters
StringBuilder insert(int offset, String str) Inserts a string and returns a reference to the calling object, itself
StringBuilder append(String str) Append string
StringBuilder deleteCharAt(int index) Removes a single character at index position from the current string
StringBuilder delete(int start, int end) Delete string
StringBuilder replace(int start, int end, String str) Substitution string
StringBuilder reverse() String inversion
  • Pay attention to

    When passed as an argument, the internal String of the method does not change its value; StringBuffer and StringBuilder change their value.

Design of the return value

  • Many methods of StringBuilder return values of type StringBuilder. Each of these methods returns the following statement: return this.
  • As you can see, these methods return a reference to the object after making changes to the sequence of characters encapsulated by StringBuilder. The purpose of this design is to be able to call continuously.

Date related classes prior to Java8

An overview of the System class

The basic concept

  • The java.lang. System class provides some useful class fields and methods.

Common methods

Method statement Function is introduced
static long currentTimeMillis() Returns the time difference in milliseconds between the current time and 00:00:00:00 on January 1, 1970

An overview of the Date class

The basic concept

  • The java.util.Date class is mainly used to describe specific moments, that is, the year, month, day, hour, minute, second, accurate to the millisecond.

Common methods

Method statement Function is introduced
Date() The object is constructed with no arguments, which is the current system time
Date(long date) Constructs an object based on the number of milliseconds specified by the argument, which is the number of milliseconds from 00:00 00:00 00:00 January 1, 1970
long getTime() Gets the number of milliseconds from the calling object to 00:00:00:00, January 1, 1970
void setTime(long time) Set the calling object to the point in time milliseconds from the base time

An overview of the SimpleDateFormat class

The basic concept

  • Java. Text. SimpleDateFormat classes is mainly used for implementation date and transformation between the text.

Common methods

Method statement Function is introduced
SimpleDateFormat() Constructs objects with no arguments
SimpleDateFormat(String pattern) The object is constructed according to the mode specified by the parameter. The mode is y- year M- month D – day H- hour M- minute S – second
final String format(Date date) Used to convert a date type to a text type
Date parse(String source) Used to convert text type to date type

An overview of the Calendar class

The basic concept

  • The java.util.Calender class is primarily used to describe specific moments, replacing outdated methods in the Date class for globalization.
  • This class is abstract and therefore cannot be instantiated. Its concrete subclasses are for the calendar systems of different countries, among which the most widely used is GregorianCalendar, which corresponds to the standard calendar system used by most countries/regions in the world.

Common methods

Method statement Function is introduced
static Calendar getInstance() To get a reference to the Calendar type
void set(int year, int month, int date, int hourOfDay, int minute, int second) Set the year, month, day, hour, minute, second information
Date getTime() Use to convert a Calendar type to a Date type
void set(int field, int value) Sets the value of the specified field
void add(int field, int amount) Adds a value to the specified field

The use of polymorphism

  • Polymorphism is formed through the parameter transfer of the method.
public static void draw(Shape s) {
    s.show();
}
draw(new Rect(1.2.3.4));
Copy the code
  • Use polymorphic syntactic formats directly in the method body
Account acc = new FixedAccount();
Copy the code
  • Polymorphism is generated by the return value type of a method
Calender getInstance(a) {
    return new GregorianCalendar(zone, aLocale);
}
Copy the code

Date-related classes in Java8

Java8 date class origin

  • A java.util.date class was included in JDK 1.0, but most of its methods were deprecated after the introduction of the Calendar class in JDK 1.1. And Calendar is not much better than Date. The problems they face are:

    • The year in the Date class starts at 1900 and the month starts at 0.
    • Formatting is only useful for the Date class, not the Calendar class.
    • Non-thread-safe, etc.

An overview of Java8 date classes

  • Java 8 further enhances Date and Time handling with the release of a new date-time API.
  • Java.time package: The base package for this package’s date/time API.
  • Java.time. chrono package: This package provides access to different calendar systems.
  • Java.time. format package: This package formats and parses date and time objects.
  • Java.time. temporal package: This package contains the underlying framework and extended features.
  • Java.time. zone package: This package supports classes for different time zones and related rules.

Overview of the LocalDate class

The basic concept

  • The java.time.LocalDate class is mainly used to describe date information in year-month-day format. This class does not represent time and time zone information.

Common methods

Method statement Function is introduced
static LocalDate now() Gets the current date from the system clock in the default time zone

Overview of the LocalTime class

The basic concept

  • The java.time.LocalTime class is used to describe time information, including hours, minutes, seconds, and nanoseconds.

Common methods

Method statement Function is introduced
static LocalTime now() Gets the current time from the system time in the default time zone
static LocalTime now(ZoneId zone) Gets the current time of the specified time zone

An overview of the LocalDateTime class

The basic concept

  • The java.time.LocalDateTime class is used to describe dates and times without time zones in the ISO-8601 calendar system, for example, 2007-12-03T10:15:30.

Common methods

Method statement Function is introduced
static LocalDateTime now() Gets the current date and time from the system time of the default time zone
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) Set the date and time according to the specified date, month, day, hour, minute, second
int getYear() Gets the numeric value of the year field
int getMonthValue() Gets the month field between 1 and 12
int getDayOfMonth() Get the date field
int getHour() Hours of acquisition
int getMinute() Get minutes
int getSecond() Get number of seconds
LocalDateTime withYear(int year) Sets to the year specified by the parameter
LocalDateTime withMonth(int month) Sets to the month specified by the parameter
LocalDateTime withDayOfMonth(int dayOfMonth) Sets to the day specified by the parameter
LocalDateTime withHour(int hour) When set to the parameter specified
LocalDateTime withMinute(int minute) Sets the score specified for the parameter
LocalDateTime withSecond(int second) Sets to the second specified by the parameter
LocalDateTime plusYears(long years) Plus the year specified by the argument
LocalDateTime plusMonths(long months) Plus the month specified by the argument
LocalDateTime plusDays(long days) Plus the day specified by the argument
LocalDateTime plusHours(long hours) Plus the parameter specified
LocalDateTime plusMinutes(long minutes) Plus the points specified by the argument
LocalDateTime plusSeconds(long seconds) Plus the seconds specified by the argument
LocalDateTime minusYears(long years) Subtract the year specified by the argument
LocalDateTime minusMonths(long months) Subtract the month specified by the argument
LocalDateTime minusDays(long days) Subtract the day specified by the argument
LocalDateTime minusHours(long hours) When subtracting the parameter specified
LocalDateTime minusMinutes(long minutes) Subtract the score specified by the argument
LocalDateTime minusSeconds(long seconds) Subtract the seconds specified by the argument

An overview of the Instant class

The basic concept

  • The java.time.Instant class is used to describe Instant point in time information.

Common methods

Method statement Function is introduced
static Instant now() Retrieves the current time from the system clock
OffsetDateTime atOffset(ZoneOffset offset) Combine this moment with the offset to create the offset date time
static Instant ofEpochMilli(long epochMilli) Constructs an object based on the number of milliseconds specified by the argument, which is the number of milliseconds from 00:00 00:00 00:00 on January 1, 1970
long toEpochMilli() Gets the number of milliseconds from 00:00:00, January 1, 1970

Overview of the DateTimeFormatter class

The basic concept

  • Java. Time. The format. DateTimeFormatter class is mainly used for formatting and parsing the date.

Common methods

Method statement Function is introduced
static DateTimeFormatter ofPattern(String pattern) Gets the object according to the mode specified by the argument
String format(TemporalAccessor temporal) Converts the specified date and time to a string
TemporalAccessor parse(CharSequence text) Converts the parameter specified string to date time