import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class StringTest1 {
@Test
public void test1(){
String str1 = "123";//数据存储在常量池中
// int num = (int)str1;//错误的
int num = Integer.parseInt(str1);
String str2 = String.valueOf(num); //"123
String str3 = num + "";//有变量名参与,那么 String str3数据存储在堆里
System.out.println(str2);//123
System.out.println(str1 == str3); //false
}
@Test
public void test2(){
String str1 = "123";
// int num = (int)str1;//错误的
int num = Integer.parseInt(str1);
String str2 = String.valueOf(num); //"123
String str3 = num + "";
System.out.println(str2);//123
System.out.println(str1 == str3); //false
}
@Test
public void test3(){
String str1 = "abc123"; //题目: a21cb3
char[] charArray = str1.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.println(charArray[i]);
}
char[] arr = new char[]{'h','e','l','l','o'};
String str2 = new String(arr);
System.out.println(str2);
}
@Test
public void test4() throws UnsupportedEncodingException {
String str1 = "abc123重工";
byte[] bytes = str1.getBytes();//使用默认的字符编码集,进行转换
System.out.println(Arrays.toString(bytes));
byte[] gbks = str1.getBytes("gbk");//使用gbk字符集进行编码。
System.out.println(Arrays.toString(gbks));
System.out.println("*****************************");
String str2 = new String(bytes);//使用默认的字符集,进行解码。
System.out.println(str2);
String str3 = new String(gbks);
System.out.println(str3);//出现乱码。原因:编码集和解码集不一致!
String str4 = new String(gbks,"gbk");
System.out.println(str4);//没有出现乱码。原因:编码集和解码集一致!
}
@Test
public void sss(){
final String s1 = "awdq";
String s2 = s1 + "qfqi";
String s3 = "awdqqfqi";
System.out.println(s3 == s2);
//s1此时是个常量,而不是变量,因为加了fianl
}
}
package doy2;
import org.junit.Test;
public class StringExer {
// 第1题
public String myTrim(String str) {
if (str != null) {
int start = 0;// 用于记录从前往后首次索引位置不是空格的位置的索引
int end = str.length() - 1;// 用于记录从后往前首次索引位置不是空格的位置的索引
while (start < end && str.charAt(start) == ' ') {
start++;
}
while (start < end && str.charAt(end) == ' ') {
end--;
}
if (str.charAt(start) == ' ') {
return "";
}
return str.substring(start, end + 1);
}
return null;
}
@Test
public void testMyTrim() {
String str = " a ";
// str = " ";
String newStr = myTrim(str);
System.out.println("---" + newStr + "---");
}
}
StringBuffer的源码分析
package doy3;
import org.junit.Test;
public class StringBufferBuilderTest {
@Test
public void test1(){
StringBuffer sb1 = new StringBuffer("abc");
sb1.setCharAt(0,'m');
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer();
System.out.println(sb2.length()); //0
}
}
package doy3;
import org.junit.Test;
public class StringBufferBuilderTest1 {
@Test
public void test2(){
StringBuffer s1 = new StringBuffer("abc");
s1.append(1);
s1.append('1');
System.out.println(s1);
// s1.delete(2,4);
// s1.replace(2,4,"hello");
// s1.insert(2,false);
// s1.reverse();
String s2 = s1.substring(1,3);
System.out.println(s1);
System.out.println(s1.length());
System.out.println(s2);
}
}
package doy3;
import org.junit.Test;
public class StringBufferBuilderTest2 {
@Test
public void test3(){
//初始设置
long startTime = 0L;
long endTime = 0L;
String text = "";
StringBuffer buffer = new StringBuffer("");
StringBuilder builder = new StringBuilder("");
//开始对比
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
buffer.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuffer的执行时间:" + (endTime - startTime));
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
builder.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuilder的执行时间:" + (endTime - startTime));
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
text = text + i;
}
endTime = System.currentTimeMillis();
System.out.println("String的执行时间:" + (endTime - startTime));
}
}
JDK 8之前的日期时间API
package doy4;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTimeTest {
//1.System类中的currentTimeMillis()
@Test
public void test1(){
long time = System.currentTimeMillis();
//返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。
//称为时间戳
System.out.println(time);
}
@Test
public void testSimpleDateFormat() throws ParseException, ParseException {
//实例化SimpleDateFormat
// SimpleDateFormat() :默认的模式和语言环境创建对象
SimpleDateFormat sdf = new SimpleDateFormat();
//格式化:日期---》字符串
Date date = new Date();
System.out.println(date); //Sun May 10 16:34:30 CST 2020
String format = sdf.format(date);
// public String format(Date date):方法格式化时间对象date
System.out.println(format); //20-5-10 下午4:34
//解析:格式化的逆过程,字符串---》日期
String str = "19-12-18 上午11:43";
Date date1 = sdf.parse(str);
//public Date parse(String source):从给定字符串的开始解析文本,以生成 一个日期。
System.out.println(date1); //Wed Dec 18 11:43:00 CST 2019
/
@Test
public void test2(){
//构造器一:Date():创建一个对应当前时间的Date对象
Date date1 = new Date();
System.out.println(date1.toString()); //Tue Oct 09 20:09:11 CST 2020
//ublic String toString():返回该对象的字符串表示
System.out.println(date1.getTime()); //1589026216998
//getTime表示返回的毫秒数
//构造器二:创建指定毫秒数的Date对象
Date date2 = new Date(1589026216998L);
//Date(long date) 里面需要传输long型数据,且后面带L
System.out.println(date2.toString());
//创建java.sql.Date对象
java.sql.Date date3 = new java.sql.Date(35235325345L);
System.out.println(date3); //1971-02-13
//如何将java.util.Date对象转换为java.sql.Date对象
//情况一:
Date date4 = new java.sql.Date(2343243242323L);
java.sql.Date date5 = (java.sql.Date) date4;
//情况二:
// Date date6 = new Date();
// java.sql.Date date7 = new java.sql.Date(date6.getTime());
}
@Test
public void testExer() throws ParseException {
String birth = "2020-09-08";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
//yyyy代表的是4个位数的年数,MM表示精确到两位数的月份,dd表示精确到两位数的日数
Date date = sdf1.parse(birth);
// System.out.println(date);
java.sql.Date birthDate = new java.sql.Date(date.getTime());
System.out.println(birthDate);
}
}
package doy4;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
public class DateTime1 {
//DAY_OF_MONTH--->日期 DAY_OF_YEAR---->这一年的第几天
@Test
public void testCalendar(){
//1.实例化
//方式一:创建其子类(GregorianCalendar)的对象
//方式二:调用其静态方法getInstance()
Calendar calendar = Calendar.getInstance();
//这个对象并不是Calendar 自身实例,而是其子类实例,这是在getInstance方法内部其实是实例化了GregorianCalendar 对象并返回了。
// System.out.println(calendar.getClass()); //class java.util.GregorianCalendar
//2.常用方法
//get()获取
int days = calendar.get(Calendar.DAY_OF_MONTH);//获取日期
System.out.println(days); //10
System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); //获取这一年的第几天//131,今天是这一年的131天
//set()设置
//calendar可变性
calendar.set(Calendar.DAY_OF_MONTH,22);//修改日期
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days); //22
//add()修改
calendar.add(Calendar.DAY_OF_MONTH,-3);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days); //22-3 --》19
//getTime():日历类---> Date
Date date = calendar.getTime();//返回当前时间到 自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
System.out.println(date); //返回了一个Dare类型的 //Tue May 19 17:12:06 CST 2020
//setTime():Date ---> 日历类
Date date1 = new Date();
calendar.setTime(date1);
//public void setTime(long time):设置此Date对象,以表示 1970 年1月1日 00:00:00 GMT以后 time 毫秒的时间点
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days); //10
}
}
package doy4;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTime {
@Test
public void testExer() throws ParseException {
String birth = "2020-09-08";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf1.parse(birth);
// System.out.println(date);
java.sql.Date birthDate = new java.sql.Date(date.getTime());
//new java.sql.Date(date.getTime())是什么意思
//----->表示将java.util.Date下的date转化成毫秒数,传到java.sql.Date
System.out.println(birthDate);
}
@Test
public void testSimpleDateFormat() throws ParseException {
//实例化SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat();
// SimpleDateFormat() :默认的模式和语言环境创建对象-->采用默认的时间格式
//格式化:日期---》字符串
Date date = new Date();//显示当前时间
System.out.println(date); //Sun May 10 16:34:30 CST 2020
String format = sdf.format(date);
// public String format(Date date):方法格式化时间对象date 有返回值,Stirng类型的
//格式化之后是'年份'-'月份'-'日期' '上午或者下午''时间'
System.out.println(format); //20-5-10 下午4:34
//解析:格式化的逆过程,字符串---》日期
String str = "19-12-18 上午11:43";
Date date1 = sdf.parse(str);
System.out.println(date1); //Wed Dec 18 11:43:00 CST 2019
/
public class JDK8DateTimeTest {
@Test
public void testDate(){
//偏移量
Date date1 = new Date(2020,9,8);
System.out.println(date1); //Fri Oct 08 00:00:00 CST 3920
Date date2 = new Date(2020 - 1900,9 - 1,8);
System.out.println(date2); //Tue Sep 08 00:00:00 CST 2020
}
}
import org.junit.Test;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class JDK8DateTimeTest {
@Test
public void test1(){
//now():获取当前的日期、时间、日期+时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
//of():设置指定的年、月、日、时、分、秒。没有偏移量
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43);
System.out.println(localDateTime1);
//getXxx():获取相关的属性
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getMonth());
System.out.println(localDateTime.getMonthValue());
System.out.println(localDateTime.getMinute());
//体现不可变性
//withXxx():设置相关的属性
LocalDate localDate1 = localDate.withDayOfMonth(22);
System.out.println(localDate);
System.out.println(localDate1);
LocalDateTime localDateTime2 = localDateTime.withHour(4);
System.out.println(localDateTime);
System.out.println(localDateTime2);
//不可变性
LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
System.out.println(localDateTime);
System.out.println(localDateTime3);
LocalDateTime localDateTime4 = localDateTime.minusDays(6);
System.out.println(localDateTime);
System.out.println(localDateTime4);
}
}
import org.junit.Test;
import java.time.*;
public class JDK8DateTimeTest {
@Test
public void test2(){
//now():获取本初子午线对应的标准时间
Instant instant = Instant.now();
System.out.println(instant); //2020-05-10T09:55:55.561Z
//添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));//东八区
System.out.println(offsetDateTime); //2020-05-10T18:00:00.641+08:00
//toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 ---> Date类的getTime()
long milli = instant.toEpochMilli();
System.out.println(milli); //1589104867591
//ofEpochMilli():通过给定的毫秒数,获取Instant实例 -->Date(long millis)
Instant instant1 = Instant.ofEpochMilli(1550475314878L);
System.out.println(instant1); //2019-02-18T07:35:14.878Z
}
}
import org.junit.Test;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
public class JDK8DateTimeTest {
@Test
public void test3(){
//方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//格式化:日期-->字符串
LocalDateTime localDateTime = LocalDateTime.now();
String str1 = formatter.format(localDateTime);
System.out.println(localDateTime);
System.out.println(str1);//2020-05-10T18:26:40.234
//解析:字符串 -->日期
TemporalAccessor parse = formatter.parse("2020-05-10T18:26:40.234");
System.out.println(parse);
//方式二:
//本地化相关的格式。如:ofLocalizedDateTime()
//FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTime
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
//格式化
String str2 = formatter1.format(localDateTime);
System.out.println(str2);//2020年5月10日 下午06时26分40秒
//本地化相关的格式。如:ofLocalizedDate()
//FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT : 适用于LocalDate
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
//格式化
String str3 = formatter2.format(LocalDate.now());
System.out.println(str3);//2020-5-10
//重点: 方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//格式化
String str4 = formatter3.format(LocalDateTime.now());
System.out.println(str4);//2020-05-10 06:26:40
//解析
TemporalAccessor accessor = formatter3.parse("2020-05-10 06:26:40");
System.out.println(accessor);
}
}
import org.junit.Test;
import java.time.*;
import java.util.Set;
public class JDK8DateTimeTest {
@Test
public void test1(){
//ZoneId:类中包含了所有的时区信息
// ZoneId的getAvailableZoneIds():获取所有的ZoneId
Set zoneIds= ZoneId.getAvailableZoneIds();
for(String s: zoneIds) {
System.out.println(s);
}
// ZoneId的of():获取指定时区的时间
LocalDateTime localDateTime= LocalDateTime.now(ZoneId.of("Asia/Tokyo"));
System.out.println(localDateTime);
//ZonedDateTime:带时区的日期时间
// ZonedDateTime的now():获取本时区的ZonedDateTime对象
ZonedDateTime zonedDateTime= ZonedDateTime.now();
System.out.println(zonedDateTime);
// ZonedDateTime的now(ZoneId id):获取指定时区的ZonedDateTime对象
ZonedDateTime zonedDateTime1= ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));
System.out.println(zonedDateTime1);
}
}
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import org.junit.Test;
public class JDK8APITest {
@Test
public void test2(){
//Duration:用于计算两个“时间”间隔,以秒和纳秒为基准
LocalTime localTime= LocalTime.now();
LocalTime localTime1= LocalTime.of(15, 23, 32);
//between():静态方法,返回Duration对象,表示两个时间的间隔
Duration duration= Duration.between(localTime1, localTime);
System.out.println(duration);
System.out.println(duration.getSeconds());
System.out.println(duration.getNano());
LocalDateTime localDateTime= LocalDateTime.of(2016, 6, 12, 15, 23, 32);
LocalDateTime localDateTime1= LocalDateTime.of(2017, 6, 12, 15, 23, 32);
Duration duration1= Duration.between(localDateTime1, localDateTime);
System.out.println(duration1.toDays());
}
}
import java.time.Period;
import org.junit.Test;
public class JDK8APITest {
@Test
public void test3(){
//Period:用于计算两个“日期”间隔,以年、月、日衡量
LocalDate localDate= LocalDate.now();
LocalDate localDate1= LocalDate.of(2028, 3, 18);
Period period= Period.between(localDate, localDate1);
System.out.println(period);
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
Period period1= period.withYears(2);
System.out.println(period1);
}
}