第十七天
输出字符串的每一个字符,可以使用String类的获取功能charAt(int index)------>char
String和StringBuffer之间相互转换
public class StringBufferDemo3 {
public static void main(String[] args) {
//String---->StringBuffer
String s = "hello" ;
//错误的写法
// StringBuffer sb = s ;//两个类型不一致
//方式1:StringBuffer有参构造方法
StringBuffer sb = new StringBuffer(s) ;
System.out.println(sb);
System.out.println("---------------------------");
//方式2:StringBuffer的无参构造方法 结合append(String str)
StringBuffer sb2 = new StringBuffer() ;
sb2.append(s) ;//追加功能
System.out.println(sb2);
System.out.println("-------------------------------------------");
//StringBuffer---->String
//创建字符串缓冲区对象
StringBuffer buffer = new StringBuffer("helloJavaee") ;
//方式1:String类型的构造方法
//public String(StringBuffer buffer)
String str = new String(buffer) ;
System.out.println(str);
System.out.println("-------------------------------------------");
//方式2:StringBuffer的public String toString()方法
String str2 = buffer.toString();
System.out.println(str2);
}
}
StringBuffer的功能:
- StringBuffer append(任何类型) :将内容追加到字符串缓冲区中 (在字符串缓冲区的最后一个字符序列的末尾追加)返回值是字符串缓冲区本身…
- public StringBuffer insert(int offset,String str):插入:在指定位置处插入指定的内容
- public StringBuffer deleteCharAt(int index):删除指定索引处的缓冲区的字符序列,返回字符串缓冲区本身
- public StringBuffer delete(int start,int end):删除从指定位置到指定位置结束的字符序列(包含end-1处的字符),返回字符串缓冲区本身
- public String substring(int start):从指定位置开始,默认截取到末尾,返回值是新的字符串
- public String substring(int start,int end):从指定位置开始到指定end-1结束进行截取,返回的新的字符串
包装类型
int类的包装类型是Integer(引用类型),包含了int类型的原始数据值,几大基本类型都有各自对应的引用类型,当需要基本类型和String类型之间转换时,就需要一个中间桥梁,而包装类型就是呢个中间桥梁.
以下是基本类型和引用类型之间的对照表.
基本类型 | 引用类型 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Integer在进制转换时可以调用它自身的静态功能
通过Integer得到int类型的取值范围
public static String toBinaryString(int i):将整数---->二进制 的字符串
public static String toOctalString(int i):将整数---->八进制的字符串
public static String toHexString(int i):将整数---->十六进制数据
public static final int MAX_VALUE:int的最大值
public static final int MIN_VALUE:int的最小值
JDK5以后的新特性之一就是自动拆装箱,自动拆装箱就是将基本类型自动装箱成相应的包装类类型,或者将自动的包装类型转换为基本类型.
public class IntegerDemo3 {
public static void main(String[] args) {
//创建Integer类对象
int i = 100 ;
Integer ii = new Integer(i) ;
ii += 200 ;
System.out.println("ii:"+ii);
/**
*通过反编译工具查看
int i = 100;
Integer ii = new Integer(i); //创建Integer类对象
ii = Integer.valueOf(ii.intValue() + 200); ii = Integer.valueIf(先拆箱--Integer--->int + 200)
//将300赋值Integer变量ii,底层使用valueOf(int)--->int---->Integer 装箱
System.out.println((new StringBuilder()).append("ii:").append(ii).toString());//以字符串形式输出出来
*/
}
}
Charcater类
构造方法:
public Character(char value)
主要功能:
public static boolean isUpperCase(char ch):判断当前字符是否大写字母字
public static boolean isLowerCAse(char ch):是否为小写字母字符
public static boolean isDigit(char ch):是否为数字字符
public static char toLowerCase(char ch):将字符转换成小写
public static char toUpperCase(char ch):将字符转换成大写
Calendear
Calendar是日历类,它能提供一些年月日的日期等等字段值,它是一个抽象类,不可以实例化,所以要通过子类间接实例化.
静态功能返回值就是它本身.
成员方法:
public int get(int field):根据给定日历字段----获取日历字段的值(系统的日历)
public abstract void add(int field,int amount):给指定的日历字段,添加或者减去时间偏移量
参数1:日历字段
参数2:偏移量
Date
Date类用来表示特定的瞬间,精确到毫秒级别,它还允许格式化和解析日期字符串.
构造方法:
public Date():当前系统时间格式
public Date(long date):参数为 时间毫秒值---->Date对象 (1970年1月1日…)
Date的格式化
DateFormat抽象类提供具体的日期格式化的子类SimpleDateFormat,然后format一个Date对象将其转换为String
String的解析
通过public Date parse(String source)throws ParseException去解析一个字符串,在此要注意的是要是参数模式String source不匹配会出现解析异常.
public class DateDemo2 {
public static void main(String[] args) throws ParseException {
//Date----->String:格式化
//1)创建Date对象:表示当前系统时间对象
Date date = new Date() ;
System.out.println(date);
//2)创建SimpleDateForamt对象:(中间桥梁)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
//3)调用format:格式化
String strDate = sdf.format(date);
System.out.println(strDate);
System.out.println("--------------------------------");
//String---->Date(重点)
//1)有一个日期文本格式
//2)创建SimpleDateFormat对象指定一个模式
//3)调用解析parse方法
//注意:SimpleDateFormat解析模式必须和String日期文本格式匹配一致
String source = "2008-5-12" ; //日期文本
//SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日") ;
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd") ;
//public Date parse(String source)throws ParseException
Date dateResult = sdf2.parse(source);
System.out.println(dateResult);
}
}