Java常用类

String类

package homework;

import java.util.Arrays;

import org.junit.Test;

public class JTest {

	/**
	 * equals方法,作用:比较两个字符串是否相等,如果相等则为true
	 */
	@Test
	public void test1() {
		String str1 = "12345";
		String str2 = "67891";
		String str3 = "12345";
		System.out.println(str1.equals(str2));
		System.out.println(str1.equals(str3));
		
	}
	
	/**
	 * subString(first,last):截取字符串,获得想要的内容
	 * 位置是包含first,不包含last
	 */
	@Test
	public void test2() {
		String str = "hellojavaworld";
		System.out.println(str.substring(5, 9));
		System.out.println(str.substring(9));
	}
	
	/**
	 * split:将一个字符串按照指定规则拆分成字符串数组
	 */
	@Test
	public void test3() {
		String str = "沈兰舟#萧策安#戚竹音#陆广白";
		String[] arr = str.split("#");
		System.out.println(Arrays.toString(arr));
		for(String s : arr) {
			System.out.println(s);
		}
	}
	
	/**
	 * indexOf:在一个大字符串中查询某个小字符串,如果查询到,则返回该小字符串的位置(索引),查不到则返回-1
	 * 
	 */
	@Test
	public void test4() {
		String str = "nsaoaoudwbfey";
		System.out.println(str.indexOf("o"));
		System.out.println(str.indexOf("k"));
	}
	

	/**
	 * replace:字符串替换
	 */
	@Test
	public void test5() {
		String str = "彼方尚有荣光在";	
		System.out.println(str.replace("荣光", "希望"));
	}
	
	/**
	 * trim:删除字符串左右两边的空格
	 */
	@Test
	public void test6() {
		String str = "  爱意随风起  ";
		System.out.println(str.trim());
	}
}

StringBuffer类

package homework;

import org.junit.Test;

public class TestStringBuffer {

	/**
	 * append:向原有的字符串末尾添加新的字符/字符串
	 */
	@Test
	public void test1() {
		StringBuffer s = new StringBuffer("七杀贪狼");
		s.append("破军");
		System.out.println(s);
	}
	
	/**
	 * insert:向指定位置插入字符/字符串
	 */
	@Test
	public void test2() {
		StringBuffer s = new StringBuffer("你是我最喜的人");
		s.insert(5, "欢");
		System.out.println(s);
	}
	/**
	 * delete:删除指定位置的字符/字符串
	 */
	@Test
	public void test3() {
		StringBuffer s = new StringBuffer("你是我最喜欢讨厌的人");
		s.delete(6, 8);
		System.out.println(s);
	}
	
	/**
	 * replace:替换指定的字符串
	 */
	@Test
	public void test4() {
		StringBuffer s = new StringBuffer("你是我最喜欢最讨厌的人");
		System.out.println(s.replace(6, 8, "重要"));
	}
	
	/**
	 * substring:截取字符串
	 */
	@Test
	public void test5() {
		StringBuffer str = new StringBuffer("你好旧时光");
		String s = str.substring(2);
		System.out.println(s);
		String st = str.substring(2, 4);
		System.out.println(st);
	}
	
	/**
	 * reverse:反转截取字
	 */
	@Test
	public void test6() {
		StringBuffer str = new StringBuffer("你是年少的欢喜");
		System.out.println(str.reverse());
		
	}
}

Object类

package homework;

import org.junit.Test;

public class TestObject {

	@Test
	public void test() {
		StringBuffer s = new StringBuffer("iloveyouthreethounds");
		StringBuffer s1 = new StringBuffer("何人知我霜雪催");
		System.out.println(s.hashCode());//获得指定对象的hashcode
		System.out.println(s1.hashCode());
		System.out.println(s.getClass()); //获取指定对象运行时的类型,包名+类名
		System.out.println(s1.getClass());
	}
}

Math类

package homework;

import org.junit.Test;

public class TestMath {

	/**
	 * PI:圆周率
	 */
	@Test
	public void test1() {
		System.out.println(Math.PI);
	}
	
	/**
	 * floor:小数取整,向下取整
	 */
	@Test
	public void test2() {
		System.out.println(Math.floor(5.6));
	}
	
	/**
	 * ceil:小数取整,向上取整
	 */
	@Test
	public void test3() {
		System.out.println(Math.ceil(3.1));
	}
	
	/**
	 * round:小数取整,四舍五入
	 */
	@Test
	public void test4() {
		System.out.println(Math.round(5.4));
		System.out.println(Math.round(5.6));
	}
	
	/**
	 * random:生成一个0-1之间的随机小数(包含0,不包含1)
	 */
	@Test
	public void test5() {
		for(int i = 0;i<10;i++) {
		System.out.println(Math.random());
		}
	}
}

Random类

package homework;

import java.util.Random;

import org.junit.Test;

public class TestRandom {

	/**
	 * nextBoolean:随机生成一个布尔类型的值
	 */
	@Test
	public void test1() {
		Random random = new Random();
		for(int i = 0;i<5;i++) {
			System.out.println(random.nextBoolean());
		}
	}
	
	/**
	 * nextDouble:随机生成一个0-1之间的小数(包含0不包含1)
	 */
	@Test
	public void test2() {
		Random random = new Random();
		for(int i = 0;i<5;i++) {
			System.out.println(random.nextDouble());
		}
	}
	
	/**
	 * nextInt:随机生成一个0-n之间的正数(包含0不包含n)
	 */
	@Test
	public void test3() {
		Random random = new Random();
		for(int i = 0;i<5;i++) {
			System.out.println(random.nextInt(20));
		}
	}
}

SimpleDateFormat类

package homework;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;

public class TestSimpleDateFormat {

	/**
	 * format:格式化日期(yyyy-MM-dd hh:mm:ss)year,month,day hour:minute:second
	 */
	@Test
	public void test1() {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
		System.out.println(sdf.format(d));
		System.out.println(sdf1.format(d));
	}
	
	/**
	 * parse:把一个合格的字符串转换成一个日期对象
	 * @throws ParseException 
	 */
	@Test
	public void test2() throws ParseException {
		String str = "2021-08-19 08:14:05";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date d = sdf.parse(str);
		System.out.println(d);
	}
}

Date类

package homework;

import java.util.Date;

import org.junit.Test;

public class TestDate {


	/**
	 * getTime:获取当前时间的毫秒数,从1970年1月1日开始(1秒=1000毫秒,1s=1000ms)
	 */
	@Test
	public void test1() {
		Date d = new Date();
		System.out.println(d.getTime());
	}
	

	/**
	 * toLocaleString:获取当地时间,返回值是一个字符串
	 */
	@Test
	public void test2() {
		Date d = new Date();
		System.out.println(d.toLocaleString());
	}
}

Calendar类 

package homework;

import java.util.Calendar;

import org.junit.Test;

public class TestCalendar {

	/**
	 * Calendar-->get:YEAR、MONTH、DATE、DAY_OF_WEEK、DAY_OF_MONTH、WEEK_OF_MONTH、WEEK_OF_YEAR
	 */
	@Test
	public void test() {
		Calendar c = Calendar.getInstance();//实例化Calendar对象
		System.out.println(c.get(Calendar.DATE));//查询现在时刻在当前月份的哪一天
		System.out.println(c.get(Calendar.YEAR));//查询现在时刻在哪一年
		System.out.println(c.get(Calendar.DAY_OF_WEEK)); //查询星期几,从周日开始
		System.out.println(c.get(Calendar.MONTH)+1);//查询现在时刻在哪一月(从0开始查)
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值