第八章 常用用类

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(%<tF)",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包中的类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值