/*
* StringBuffer构造方法演示
* StringBuilder同理
*/
public class Demo1 {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer();
StringBuffer stringBuffer2 = new StringBuffer("有参构造");
System.out.println(stringBuffer);
System.out.println(stringBuffer2);
}
}
/*
* StringBuffer和StringBuilder
* 中的append和insert方法演示
*/
public class Demo2 {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer(
"武汉加油,中国加油,世界加油");
stringBuffer.append(",必须的");
stringBuffer.append(2020);
stringBuffer.append(new Demo1());
System.out.println(stringBuffer.toString());
stringBuffer.insert(3, "磊哥最帅");
System.out.println(stringBuffer.toString());
}
}
修改方法:
replace(int start , int end , String str) :从指定下标位置start开始,到end - 1 结束,使用指定字符串str替换整个范围。
setCharAt(int index,char ch) :使用指定的字符替换指定下标位置的字符。
/*
* StringBuffer和StringBuilder中的修改方法
*/
public class Demo4 {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("1000Phone教育");
StringBuffer replace = stringBuffer.replace(1, 5, "999");
System.out.println(replace);//会返回一个新的字符串,旧字符串也会产生修改
System.out.println(stringBuffer);
stringBuffer.setCharAt(0, '6');
System.out.println(stringBuffer);
}
}
删除和反序:
delete(int start,int end) :删除指定范围内的字符,[start,end - 1)。
deleteCharAt(int inex) :删除指定下标位置的字符。
reverse( ) :字符串逆序。
/*
* StringBuffer和StringBuilder的删除,逆序方法
*/
public class Demo5 {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("牛肉包,正香");
System.out.println(stringBuffer);
StringBuffer delete = stringBuffer.delete(4, 5);
//返回的新的字符串
System.out.println(delete);
//修改操作之后的原字符串
System.out.println(stringBuffer);
StringBuffer deleteCharAt = stringBuffer.deleteCharAt(3);
System.out.println(deleteCharAt);
System.out.println(stringBuffer);
stringBuffer.reverse();
System.out.println(stringBuffer);
}
}
/*
* Math工具类当中的常用方法
*/
public class Demo001 {
public static void main(String[] args) {
//1.取绝对值
System.out.println(Math.abs(2.3));
System.out.println(Math.abs(-2.3));
//2.向上取整
System.out.println(Math.ceil(4.6));
System.out.println(Math.ceil(-4.6));
//3.向下取整
System.out.println(Math.floor(3.4));
System.out.println(Math.floor(-3.4));
//4.四舍五入
System.out.println(Math.round(2.4));
System.out.println(Math.round(2.5));
System.out.println(Math.round(-2.4));
System.out.println(Math.round(-2.5));
System.out.println(Math.round(-2.6));
}
}
/*
* Data类演示
*/
public class Demo1 {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
File file = new File("E:/aaa/1.txt");
long lastModified = file.lastModified();
Date date2 = new Date(lastModified);
System.out.println(date2);
System.out.println(System.currentTimeMillis());
System.out.println(date.getTime());
System.out.println(date2.getTime());
}
}
/*
* DateFormat抽象类
* SimpleDateFormat 演示
*/
public class Demo2 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy年MM月dd日 HH:mm:ss");
String format = simpleDateFormat.format(new Date());
System.out.println(format);
Date parse = simpleDateFormat.parse("2020年3月11日 23:33:33");
System.out.println(parse);
String format2 = simpleDateFormat.format(parse);
System.out.println(format2);
}
}
/*
* 日历类Canlender
* 演示
*/
public class Demo3 {
public static void main(String[] args) {
//1.得到一个Canlender类对象
Calendar canlendar = Calendar.getInstance();
int year = canlendar.get(Calendar.YEAR);
int month = canlendar.get(Calendar.MONTH) + 1;
int day = canlendar.get(Calendar.DAY_OF_MONTH);
int hour = canlendar.get(Calendar.HOUR_OF_DAY);
int minute = canlendar.get(Calendar.MINUTE);
int second = canlendar.get(Calendar.SECOND);
int dayweek = canlendar.get(Calendar.DAY_OF_WEEK) - 1;
System.out.println(year + "-" + month + "-" +
day + " " + hour + ":" + minute + ":" + second + " " + dayweek);
canlendar.set(Calendar.YEAR, 1890);
Date time = canlendar.getTime();
System.out.println(time);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy年MM月dd日 HH:mm:ss E");
String format = simpleDateFormat.format(time);
System.out.println(format);
}
}
public class Demo1 {
public static void main(String[] args) {
Properties properties = System.getProperties();
properties.list(System.out);
String property = System.getProperty("os.name");
System.out.println(property);
int[] arr = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
int[] temp = new int[10];
System.arraycopy(arr, 0, temp, 0, arr.length);
System.out.println(Arrays.toString(temp));
for (int i = 0; i < 100; i++) {
System.out.println(System.currentTimeMillis());
}
}
}
public class Demo1 {
public static void main(String[] args) throws IOException,
InterruptedException {
Runtime runtime = Runtime.getRuntime();
System.out.println(runtime.totalMemory());
System.out.println(runtime.freeMemory());
System.out.println(runtime.maxMemory());
Process exec = runtime.exec(
"\"D:\\Program Files\\Notepad++\\notepad++.exe\"");
Thread.sleep(5000);
exec.destroy();
}
}
public class Demo2 {
public static void main(String[] args) {
byte parseByte = Byte.parseByte("1");
short parseShort = Short.parseShort("123");
int parseInt = Integer.parseInt("100");
long parseLong = Long.parseLong("1000");
float parseFloat = Float.parseFloat("3.14");
double parseDouble = Double.parseDouble("0.618");
boolean parseBoolean = Boolean.parseBoolean("true");
System.out.println(parseBoolean);
// 传入的字符串无法解析对应的数据,NumberFormatException
// System.out.println(Integer.parseInt("123abc"));
}
}