JDK8之前
System.currentTimeMillis(); 返回当前时间和1970年1月1日0时0分0秒之间以毫秒为单位的时间差
Date类 java.util.Date类
创建毫秒数的Date对象
Date date = new Date(long型毫秒数)
创造当前时间的Date对象
Date date = new Date()
System.out.println(date) //Fri Jun 12 12:18:14 CST 2020
toString() 显示年月日 // Fri Jun 12 12:18:14 CST 2020
getTime() 显示毫秒数 //1591935523890
java.sql.Date对应数据库中的Date
将Date—>sql下的Date
Date date = new Date();
java.sql.Date date2 = new java.sql.Date(date.getTime());
SimpleDateFormat:对Date格式化和解析
日期—>字符串
SimpleDateFormat sdf = new SimpleDateFormat();
String format = sdf.format(data对象) ; // 20-6-12 下午10:48
字符串—>日期
String str =”2000-06-12 下午10:48”;
Date date1 = sdf.parse(str) ; // Fri Jun 12 12:18:14 CST 2020
SimpleDateFormat sdf = new SimpleDateFormat(格式化的输出);
y年,M月,d天,h小时,m分钟,s秒 ;
Date date = new Date() ;
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
System.out.println(sdf.format(date)); //输出结果 2020-06-12 11:00:26
//注意parse会有异常,必须要字符串和你构造方法里面的样式一样
Date date1 = sdf.parse(“2020-06-12 11:00:26”);
System.out.println(date1); //Fri Jun 12 11:00:26 CST 2020
//将字符串转换成sql下的date
String strDate = “2020-10-21”;
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date date = sdf.parse(strDate);
java.sql.Date date1 = new java.sql.Date(date.getTime());
System.out.println(date1); //2020-10-21
Calendar日历类(抽象类)
Calendar.getInstance(); 调用静态方法
- get()
- set()
- add()
- getTime()
- setTime();
//calendar转换成Date类
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
//Date转换成Calendar类
Date date1 = new Date();
calendar.setTime(date1);
LocalDate LocalTime LocalDateTime
- .now(); 可以获取现在的时间
- .of(); 设置时间
- getXXX()可以获取具体值
- withXX()设置相关的属性,放回一个原来的类型
- plusXXX() :加上属性
- minusXXX():减少属性
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(date); //2020-06-13
System.out.println(time); //15:43:17.953
System.out.println(dateTime); //2020-06-13T15:43:17.953
LocalDateTime dateTime1 = LocalDateTime.of(2000,5,15,13,23,20); //设置时间年月日时分秒
System.out.println(dateTime1); //2000-05-15T13:23:20
Instant(类似与Date)
Instant instant = Instant.now(); // 返回UTC上面的时间 例如:2020-06-13T08:22:02.815Z
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8)); //东八区上面的时间 例如:2020-06-13T16:21:24.988+08:00
long l = instant.toEpochMilli(); // 获取对应的毫秒数 例如:1592036522815
Instant instant1 = Instant.ofEpochMilli(毫秒数); // 通过给定毫秒数,获取Instant实例
DateTimeFormatter:格式化或者解析日期,时间(类似SimpleDateFormat)
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME ;
日期—>字符串
LocalDateTime localDateTime = LocalDateTime.now() ;
String s = formatter.format(localDateTim // 2020-06-13T19:07:43.064
字符串—>日期
TemporalAccessor parse = formatter.parse(s);
本地化格式:ofLocalizedDateTime()适用
FormatStyle.LONG —2020年6月13日 下午07时28分42秒 / FormatStyle.MEDIUM —2020-6-13 19:27:10 /FormatStyle.SHORT —20-6-13 下午7:25
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
LocalDateTime localDateTime = LocalDateTime.now() ;
String format = formatter.format(localDateTime); //2020年6月13日 下午07时21分43秒
ofLocalizedDate(FormatStyle.LONG);适用LocalDate
FormatStyle.SHORT/FormatStyle.MEDIUM/FormatStyle.LON G/FormatStyle.FULL
自定义格式,如ofPattern(“yyyy-MM-dd hh:mm:ss”);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH-mm-ss”);
LocalDateTime localDateTime = LocalDateTime.now();
formatter.format(localDateTime); //2020-06-13 19-51-17
String.StringBuffer.StringBuilder
int Integer
在数字上面比较两个对象compareTo(); 返回值 0 ; -1;1
JDk1.5之后,自动装箱,如果装的是一个字节,那么该数据会被共享不会重新开辟空间(127);
String
- 不可继承
- Serializable接口,字符串支持序列化的
- 内部定义了final char[] value 储字符串数据
- String str = “abc” ;字面量的定义方式
- 字符串常量值不会存储相同内容的字符串
- 对字符串重新赋值和连接以及replace()都要,重新在字符串常量值创造字符串常量
- 例题4有String的一些操作
- 常量与常量,结果都在常量池,常量池不会存在相同的常量
- 只要有一个变量,结果就在堆中
- 拼接方法调用intern(); 返回值就会放回在常量池中的对象,
String类和stringBuffer类
String s = new String() == 等价于String s =” “ 不等价与String s = null
String s = “123” 在常量池中创建 而String s = new String(“123”)是在堆空间创建
构造方法可以存byte[]数组 ,和char[]数组
当中可以利用String(char [] value ,int offset,int count) 创建一个String
String方法:
- length() :获取字符串的长度 返回值 int
- equals():判断字符串相等 返回值boolean;
- equalsIgnoreCase(String str):忽略大小写的字符串比较 返回值boolean
- charAt(int index):获取索引位置上的字符 返回值 char
- indexOf(int ch): 获取第一次出现的位置;返回值 int
- indexOf(String str),如上一样。其中可以通过返回值-1判断改字符是否存在。
- indexOf(String str,int fromIndex);从指定索引fromIndex的开始查找第一次出现的位置
- isEmpty(): 判断字符串是否长度为0,(value.length == 0) ; 返回值boolean
- trim() 去除首位的空格 返回值String
- lastIndexOf(String ch) 最后出现的一次位置的索引
- lastIndexOf(String str,int fromIndex) 从fromIndex反向查找
- compareTo(); 比较两个字符串大小
- substring(int beginIndex, int endIndex) 获取部分字符串(子串) 从beginIndex开始截取endIndex(不包含)结束, substring(int beginIndex); 返回值String
- solit(String regex) 将字符串变成字符串数组(字符串的切割) 返回值String[]
- toUpperCase():大写 toLowerCase()小写 将字符串转换成大小写:返回字符串
- replace(char oldCahr, char newChar) 将全部指定字符中内容替换 返回值String replace(String oldString, String new String) 返回值String (不存在,则不会修改)
- concat(String newString):将字符串进行连接 相当于+
- contains(String str) 是否包含指定字符串 返回值boolean
- startsWith(String str) 开头 endsWith(String str) 结尾 : 是否以指定字符串开头和结尾的. 返回值ture ; startWith(String prefix, int toffset) : 判断字符串索引开始为prefix
- getBytes(); 将字符串转换成字节数组:返回值byte[]
- toCharArray(); 将字符串转换字符数组:返回值char[]
byte.char数组转换成为String,把byte. char数组传进String的构造器就行
String,StringBuffer,StringBuilder
- String:不可变字符序列,底层使用char[]存储,频繁修改对内存消耗大;
- StringBuffer:可变字符序列,线程安全的(方法都是synchronized同步方法),效率低,底层使用char[]存储
- StringBuilder:可变字符序列,JDK5.0新增,线程不安全,效率低,底层使用char[]存储
- StringBuffer和StringBuilder 对象.length()存储的是他们的实际的字符长度
- 底层使用16长度字符数组,不够默认将16原来的2倍+2,把原有数组复制到新数组去
- 开发中为了避免扩容(长度尽量自己去创造,提高效率!)可以StringBuffer(int capacity)或StringBuilder(int capacity)来创造长度为capacity的数组
- 三者效率:StringBuilder>StringBuffer>String效率高到低;
- StringBuffer,StringBuilder两者可以存储不同类型的数据,但是最终还是要转换字符串使用,
StringBuffer和StringBuilder方法
- append(data) 添加
- insert(index,data)在index下标添加data
- replace(start,end,String str)修改[start,end)为str
- delete(start,end)删除 包头不包尾
- charAt(int index)查找index索引上面的字符串
- indexOf(String) 查找字符的出实现的索引 lastIndexOf(String)没有存在返回值为-1
- setCharAt(index,string)
- substring(int beginIndex, int endIndex) 截取从beginIndex到endIndex(不包含)结束, substring(int beginIndex); 返回值String
- reverse()把当前字符反转
jdk升级:简化书写,提高效率,增加安全性。