• 第八章 常用用类


    8.4 StringBuffer类

    8.4.1 StringBuffer对象

    String对象的字符序列是不可修改的,但StringBuffer类的对象的实体的内存空间可以自动地改变大小,便于存放一个可变的字符序列。

    StringBuffer s= new StringBuffer("我喜欢");
    //对象s可调用append方法追加一个字符序列
    s.append("蓝色");
    

    StringBuffer类有三个构造方法

    • StringBuffer();//通过capacity方法获取当前实体的实际容量
    • StringBuffer(int size);//初始容量为参数size指定的字符个数,超过时自动增加
    • StringBuffer(String s);

    8.4.2 StringBuffer类的常用方法

    1.append方法
    2.charAt(int n)和setCharAt(int n, char ch)
    • public char charAt(int n):得到StringBuffer对象的字符序列位置n上的字符。
    • public void setCharAt(int n, char ch):将当前StringBuffer对象的字符序列位置n处的字符用参数ch指定的字符替换(n的值必须是非负的,且小于当前对象实体中字符序列的长度,StringBuffer对象的字符序列的第一个位置是0)。
    • StringBuffer insert(int index,String str):将参数str指定的字符序列插入到参数index指定的位置,返回当前对象的引用。
    • public StringBuffer reverse():将该对象实体中的字符序列翻转,返回当前对象的引用。
    • StringBuffer delete(int startIndex,int endIndex):删除startIndex到endIndex-1位置的字符。
    • StringBuffer replace(int startIndex,int endIndex,String str)

    8.5 Date类与Calendar类

    8.5.1 Date类

    1.使用无参数构造方法

    Date nowTime=new Date();
    //获取本机的当前日期和时间
    System.out.println(nowTime);
    

    2.使用带参数的构造方法
    计算机系统将其自身的时间的“公元”设置在1970年1月1日0时(格林威治时间),根据这个使用Date的带参数的构造方法Date(long time)来创建Date对象。

    Date date1=new Date(1000);
    date2=new Date(-1000);
    

    正数表示公元后的时间,负数表示公元前的时间。1000表示1000毫秒。

    8.5.2 Calendar类

    Calendar类在java.util包中。使用calendar类的static方法getInstance()可以初始化一个日历对象。

    Calendar c=Calendar.getInstance();
    

    Calendar对象的方法:

    public final void set(int year,int month,int date);
    public final void set(int year,int month,int date,int hour,int minute);
    public final void set(int year,int month,int date,int hour,int minute,int second);
    

    calendar对象调用方法public int get(int field)可以获取有关年份、月份、小时、星期等信息,参数field的有效值有calendar的静态常量指定。

    c.get(Calendar.MONTH);
    //返回一个整数,0表示当前日历是在1月
    c.get(Calendar.DAY_OF_WEEK);
    //返回整数,1表示星期日
    

    calendar对象调用方法public long getTimeInMillis()可以返回当前对象中时间的毫秒计时,如果本地时区是北京时区,是与1970.1.1 8点的差值。

    import java.util.Calendar;
    import java.util.Date;
    
    public class Main {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Calendar c=Calendar.getInstance();
    		c.setTime(new Date());
    		int year=c.get(Calendar.YEAR);
    		int month=c.get(Calendar.MONTH)+1;
    		int day=c.get(Calendar.DAY_OF_MONTH);
    		int hour=c.get(Calendar.HOUR_OF_DAY);
    		int minute=c.get(Calendar.MINUTE);
    		int second=c.get(Calendar.SECOND);
    		System.out.println("现在的时间是:");
    		System.out.println(""+year+"年"+month+"月"+day+"日");
    		System.out.println(""+hour+"时"+minute+"分"+second+"秒");
    		int y=2012,m=9,d=1;
    		c.set(y, m-1,d);
    		long t1=c.getTimeInMillis();
    		y=2016;
    		m=7;
    		day=1;
    		c.set(y, m-1,d);
    		long t2=c.getTimeInMillis();
    		long subDay=(t2-t1)/(1000*60*60*24);
    		System.out.println(""+new Date(t2));
    		System.out.println("与"+new Date(t1));
    		System.out.println("相隔"+subDay+"天");
    
    
    
    				
    	}
    
    }
    
    

    在这里插入图片描述

    Main.java
    
    public class Main {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		CalendarBean cb=new CalendarBean();
    		cb.setYear(2022);
    		cb.setMonth(6);
    		String []a=cb.getCalendar();
    		char[] str="日一二三四五六".toCharArray();
    		for(char c:str) {
    			System.out.printf("%3c",c);
    		}
    		for(int i=0;i<a.length;i++) {
    			if(i%7==0)
    				System.out.println("");
    			System.out.printf("%4s",a[i]);
    		}
    	}
    
    }
    
    
    import java.util.Calendar;
    
    public class CalendarBean {
    	int year=0,month=0;
    	public void setMonth(int month) {
    		this.month = month;
    	}
    	public void setYear(int year) {
    		this.year=year;
    	}
    	public String[] getCalendar() {
    		String[] a=new String[42];
    		Calendar rili=Calendar.getInstance();
    		rili.set(year, month-1,1);
    		int weekDay=rili.get(Calendar.DAY_OF_WEEK)-1;
    		int day=0;
    		if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
    			day=31;
    		if(month==4||month==6||month==9||month==11)
    			day=30;
    		if(month==2) {
    			if(((year%4==0)&&year%100!=0)||(year%400==0))
    				day=19;
    				else
    					day=28;				
    		}
    		for(int i=0;i<weekDay;i++)
    			a[i]=" ";
    		for(int i=weekDay,n=1;i<weekDay+day;i++) {
    			a[i]=String.valueOf(n);
    			n++;
    		}
    		for(int i=weekDay+day;i<a.length;i++)
    			a[i]=" ";
    		return a;					
    	}
    }
    
    

    请添加图片描述

    8.6 日期的格式变化

    可以使用String类调用format方法对日期进行格式化。

    8.6.1 format方法

    format(格式化模式,日期列表)
    

    按照格式化模式返回日期列表中所列各个日期所含数据(年、月、日、时等数据)的字符串表示。
    1.格式化模式
    format方法中的“格式化模式”是一个用双引号括起的字符序列,该字符序列中的字符由时间格式符和普通字符构成。
    假设当前时间是2016/10/01

    Date now=new Date();
    String s1=String.format("%tY年%tm月%td日",now,now,now);
    String s2=String.format("%tF",now);
    

    请添加图片描述
    2.日期列表

    保证format方法“格式化模式”中的格式符的个数与日期列表中列出的个数相同。

    3.格式化同一日期

    8.6.2 不同区域的星期格式

    用特定地区的星期格式来表示日期中的星期,可以用format的重载方法

    format(Locale locale,格式化模式,日期列表);
    

    Locale 表示地域。
    Locale类的static常量都是locale对象,其中US表示美国的static。

    String s=String.format(Locale.US,"%ta(%,new Date());
    

    8.7 Math类、BigInteger类和Random类

    8.7.1 Math类

    计算平方根、绝对值或者获取一个随机数。
    java.lang包中的Math类包含进行科学计算的static方法。
    两个static常量,E和PI

    • public static long abs(double a):返回a的绝对值
    • public static double max(double a, double b):返回a、b的最大值
    • public static double min(double a, double b):返回a、b的最小值
    • public static double random():产生一个0-1之间的随机数
    • public static double pow(double double b):返回a的b次幂
    • public static double sqrt(double a):a的平方根 square root
    • public static double log(double a):a的对数
    • public static double sin(double a): a的正弦
    • public static double asin(double a):a的反正弦
    • public static double ceil(double a):a的最小整数(ceil 天花板)
    • public static double floor(double a):a的最大整数
    • public static long round(double a): (long) Math.floor(a+0.5)),即所谓a的四舍五入后的值

    8.7.2 BigInteger类

    特别大的整数
    public BigInteger(String val) 构造一个十进制的BigInteger类的对象。
    该构造方法可能发生NumberFormatException异常。

    • public BigInteger add(BigInteger val):加
    • public BigInteger subtract(BigInteger val):
    • public BigInteger multiply(BigInteger val):
    • public BigInteger divide(BigInteger val):
    • public BigInteger remainder(BigInteger val):
    • public BigInteger compareTo(BigInteger val):
    • public BigInteger abs( ):
    • public BigInteger pow(int a):
    • public String toString();
    • public String toString(int p);p进制的字符串表示

    8.7.3 Random类

    更加灵活的用于获得随机数的Random类(java.util)

    public Random();
    public Random(long seed);

    Random r=new Random();
    r.nextInt();
    
    r.nextInt(100);
    //0-99之间的某个整数(闭区间)
    
    r.nextBoolean();
    

    8.8 数字格式化

    8.8.1 format方法

    1.格式化模式
    程序可以使用String类调用format方法对数字进行格式化

    String s=String.format("%.2f",3.141592);
    //3.14
    

    2.值列表

    String s=String.format("%d元%0.3f斤",888,999.777666);
    //888 999.778
    

    3.格式化顺序

    format方法默认从左到右,如果不希望,可以在格式符前面添加索引符号 index$ ,n$表示值列表的第n个。

    8.8.2 格式化整数
    1. %d %o %x % X
    • 十进制
    • 八进制
    • 小写十六进制
    • 大些十六进制

    2.修饰符

    +:强制添加正号
    ,:按千分组

    3.数据的宽度

    即format方法返回的字符串的长度。

    %md 左加空格
    %-md右加空格
    %0md 不加空格(用0代替空格)

    8.8.3 格式化浮点数

    1.float Float double Double

    %f 格式为十进制浮点数,保留6位小数
    %e(%E) 科学计数法的十进制的浮点数(E大写)
    %g(%G)
    %a(%A)

    2.修饰符
    +强制添加正号

    3.限制小数位数与数据的宽度
    %.nf
    %mf

    8.9 Class类与Console

    8.9.1 Class类

    Class类是java.lang包中的类。

  • 相关阅读:
    c++征途 -- 程序流程结构
    【网络】网口工作模式(混杂模式|监听模式|监视模式|管理模式)
    Kafka 技术指南:使用、特性、一致性保证与 Golang 中间件应用(上)
    Java岗大厂面试百日冲刺 - 日积月累,每日三题【Day5】 —— 基础篇2
    1.并发编程基础
    原子物理中的组合常数
    鸿蒙应用开发
    ArrayList详解
    Qt中对象树的机制介绍以及底层实现,各种结果分析:(以及自己写容易犯错的点)
    【SICP】引入与教学大纲
  • 原文地址:https://blog.csdn.net/qq_34266854/article/details/127112427