Since the object content of the String class cannot be changed, a new String object will be constructed every time it is spliced, which is time-consuming and a waste of memory space
At this time, you need to use java The provided StringBuild class solves this problem
StringBuilder is also called a variable character sequence. It is a string buffer similar to String and can be regarded as a container. Many strings can be held in the container
Variable means that the content in the StringBuilder object is variable
public StringBuilder()
: Create an empty buffer
public StringBuilder(String srt)
: Create a buffer that stores str
//public StringBuilder():创建一个空白可变字符串对象,不含有任何内容 StringBuilder sb = new StringBuilder(); System.out.println("sb:" + sb); System.out.println("sb.length():" + sb.length()); //public StringBuilder(String str):根据字符串的内容,来创建可变字符串对象 StringBuilder sb2 = new StringBuilder("hello"); System.out.println("sb2:" + sb2); System.out.println("sb2.length():" + sb2.length());
public StringBuilder append(Object obj)
: Append any type of data to the container and convert it to a string
// 链式编程, 链式编程返回结果 看最后调用的方法 StringBuilder abc = new StringBuilder(stringBuilder.append(10).append("abc").append(10.1).append(new Object()).toString()); System.out.println("abc = " + abc);
public StringBuilding reverse()
: Reverse the buffer data
String string = new StringBuilder(abc).reverse().toString(); System.out.println(string);
java.util.Date
Represents a specific moment, accurate to milliseconds
public Date()
: The current date object, the millisecond value elapsed from the time when the program is run to the time origin, is converted into a Date object, allocates a Date object and initializes this object to represent the time at which it is allocated (accurate to milliseconds).
public Date(long date)
: Convert the millisecond value date of the specified parameter into a Date object, allocate the Date object and initialize this object
Time origin : January 1, 1970 00:00:00
China is in the East 8th District. Strictly speaking, it is January 1, 1970 00:08:00
1s = 1000ms
public static void main(String[] args) { // 创建日期对象,把当前的时间 System.out.println(new Date()); // Tue Jan 16 14:37:35 CST 2020 // 创建日期对象,把当前的毫秒值转成日期对象 System.out.println(new Date(0)); // Thu Jan 01 08:00:00 CST 1970 }
long getTime()
: Get the millisecond value of the date object
// 获取从 时间原点 到 当前日期 的毫秒值 long time = nowTime.getTime(); System.out.println(time);
void setTime(long time)
: Set millisecond value
// 设置偏移毫秒值为0, 即时间原点 nowTime.setTime(0); System.out.println(nowTime);
java.text.DateFormat
is an abstract class of date/time formatting subclass, we can use this class to help us complete it Conversion between date and text, that is, you can convert back and forth between Date object and String object.
Since DateFormat is an abstract class and cannot be used directly, a commonly used subclass java.text.SimpleDateFormat
is required.
This class requires a pattern (format) to specify the standard for formatting or parsing.
public SimpleDateFormat()
: Constructs SimpleDateFormat using the default mode and locale date format symbols.
The default format is: (year)-(month)-(day) (am/pm)xx:xx
public SimpleDateFormat(String pattern)
: Constructs a SimpleDateFormat with the given pattern and date format symbols of the default locale.
The parameter pattern is a string representing a custom format for date and time.
Commonly used format rules are:
Identifier letters (case sensitive) | Meaning |
---|---|
y | 年 |
M | 月 |
d | 日 |
H | hours |
m | minutes |
s | Seconds |
Note: For more detailed format rules, please refer to the API documentation of the SimpleDateFormat class.
public String format(Date date)
: Pass the date object and return the formatted string.
// 将当前日期 转换为 x年x月x日 xx:xx:xx Date nowTime = new Date(); DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E"); System.out.println(df.format(nowTime));
public Date parse(String source)
Pass the string and return the date object
// 获取sDate所代表的时间的毫秒值 String sDate = "1949-10-01"; DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd"); // parse 若无法解析字符串会抛出异常 ParseException Date date = df2.parse(sDate); long time = date.getTime(); System.out.println(time);
java.util.Calendar
Calendar calendar class, replaces many Date methods
It is an abstract class, but provides static methods to create objects, and also provides Many static attributes
Month 0-11 represents January-December
The first day of the week abroad is Sunday
public static Calendar getInstance()
: Get a calendar using the default time zone and locale.
Calendar c = Calendar.getInstance(); System.out.println(c);
Use class name.property name
to call, representing the given calendar field:
Field value | Meaning |
---|---|
YEAR | 年 |
MONTH | Month (starts from 0, can be used as 1) |
DAY_OF_MONTH | Day of the month (number) |
HOUR | hour (12-hour format) |
HOUR_OF_DAY | hour (24-hour format) |
MINUTE | Minutes |
SECOND | Seconds |
Week Day in (day of the week, Sunday is 1, you can use -1) |