一:calendar类的常用成员方法 日历
1.calendar类的常用成员方法
public int get(int field) // 返回给定日历字段的值
//如下
private static void demo01() {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
System.out.println(year);
int march = c.get(Calendar.MARCH);
System.out.println(march);
int date = c.get(Calendar.DATE);
System.out.println(date);
}*
public void set(int field,int field);//将给定的日历字段设置为给定值
//如下:
private static void demo02() {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,2000);
c.set(Calendar.MARCH,9);
c.set(Calendar.DATE,15);
int year = c.get(Calendar.YEAR);
System.out.println(year);
int march = c.get(Calendar.MARCH);
System.out.println(march);
int date = c.get(Calendar.DATE);
System.out.println(date);
}
public abstract void add(int field , int field);/根据日例字段的规则,为指定的日期添加或者减去指定的时间按量 正数为加 负数为减
//如:
private static void demo03() {
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR,2);
c.add(Calendar.MARCH,1);
c.add(Calendar.DATE,1);
int year = c.get(Calendar.YEAR);
System.out.println(year);
int march = c.get(Calendar.MARCH);
System.out.println(march);
int date = c.get(Calendar.DATE);
System.out.println(date);
}
public Date getTime();//返回有个表示此Calendar时间值
//如:
private static void demo03() {
Date date1 = c.getTime();
System.out.println(date1);
}
二:System类
常用的方法有:public static long currentTimeMillis():返回以毫秒为单位的当前时间。(测试程序效率)
如:
//打印1-9999所需要的时间
private static void demo01() {
//程序执行前获取一次毫秒值
long s = System.currentTimeMillis();
//执行for
for (int i = 1; i <= 9999; i++) {
System.out.println(i);
}
long e = System.currentTimeMillis();
long a = e - s;
System.out.println(a);
}
public static void arraycopy(Objict src,int srcPos, Object dest,int destPos,int length):将数组中指定的数据拷贝到另一个数组中
src - 原数组
srcPos - 原数组中的起始位置
dest - 目标数据中的起始位置
length - 要复制的数组元素的数量
/*复制src数组元素【1,2,3,4,5】,dest数组元素【6,7,8,9,10】
复制后:src数组元素【1,2,3,4,5】,dest数组元素【1,2,3,9,10】*/
private static void demo02() {
//定义数组
int[] src = {1,2,3,4,5};
int[] dest = {6,7,8,9,10};
System.out.println("复制前" + Arrays.toString(dest));
//使用System类中的arraycopy把原数组的前三个复制到目标数组的前三个;
System.arraycopy(src,0,dest,0,3);
System.out.println("复制后" + Arrays.toString(dest));
}
三:StringBuilber类(字符串缓冲区,可以提高字符串的操作效率 )
public static void main(String[] args) {
StringBuilder bu1 = new StringBuilder();
System.out.println("bu1" + bu1);
StringBuilder bu2 = new StringBuilder("abc");
System.out.println("bu2" + bu2);
}
1.常用方法有两个:
①,public StringBuilder append(…):添加任意类型的字符串形式,并返回当前对象自身,
②,public String tostring():将当前StringBulider对象转为string对象
public class Demo03StringBuilder {
public static void main(String[] args) {
//创建StringBuilder对象
StringBuilder bu = new StringBuilder();
//使用append方法网StringBuilder中添加数据
/*StringBuilder bu2 = bu.append("abc");//把bu的地址赋值给了bu2
System.out.println(bu);
System.out.println(bu2);
System.out.println(bu == bu2);//比较地址为true*/
//使用append方法无需接收返回值
/* bu.append("abc");
bu.append(1);
bu.append(true);
bu.append(8.8);
bu.append("中");
System.out.println(bu);*/
//Stringbuilder 和String可以相互转换
String str = "Hello";
System.out.println("str" + str);
StringBuilder bu3 = new StringBuilder(str);
//添加数据
bu3.append("world");
System.out.println("bu3"+bu3);
//StringBuilder --> String
String s = bu3.toString();
System.out.println("s:" + s);
}
}
四:包装类
1.基本数据类型,使用起来非常方便,但是没有对应的方法来操作这些基本类型的数据可以使用一个类,把基本类型的数据装起来,在类中定义一些方法,这个类叫做包装类,。我们可以使用类中的方法来操作这些基本数据类型的数据。
基本类型 对应的包装类
byte —> Byte
short —> Short
int —> Integer
long —> Long
float —> Float
double —> Double
char —> Character
boolean —> Boolean
2.自动拆箱与自动装箱:基本类型的数据和包装类相互转换
//自动装箱:直接把int类型的证书赋值包装
//Integer in = 1;相当于 Integer in = new Integer(1);
Integer in = 1;
//自动拆箱:int + 2 相当于in.intVale() + 2 = 3
in = in+2
3.字符串与基本类型之间的相互转换
基本类型 - 》字符串(String)
①:基本类型的值+" " 简单方法
②:包装类的静态方法toString(参数),不是Object类的toString()重载
static String toString(int i)返回一个表示指定整数的Stringd对象
③:String类的静态方法valueOf(参数)
static String valueOf(int i)返回 int参数的字符串表示形式
字符串(String) ->基本类型
①:使用包装类的静态方法parseXXX(“字符串”);
Integer类:static int parseInt(String s)
Double类:static int parseDouble(String s)
public class Demo05Integer {
public static void main(String[] args) {
//基本类型 - 》字符串(String)
int i1 = 100;
String s1 = i1 + "";
System.out.println(s1+200);
String s2 = Integer.toString(100);
System.out.println(s2+200);
String s3 = String.valueOf(100);
System.out.println(s3 + 200);
//字符串(String) ->基本类型
int i = Integer.parseInt(s1);
System.out.println(i);
}
}