实用类

枚举是指由一组固定的常量组成的类型,简单来说,枚举就是使用一组常量值表示特定的数据集合。在这个数据集合定义时,所有可能的值都是已知的。

定义枚举使用的关键字是enum,例如:

public enum Week{
<span style="white-space:pre">	</span>MON,TUE,WED,THU,FRI,SAT,SUN
}
枚举中除了定义枚举常量外还可以定义普通的类成员包括构造方法,构造方法必须是私有访问级别的。

注意:在定义任何其他类成员,枚举类必须首先定义枚举常量,如果枚举中除了枚举常量还需要定义其他成员,那么枚举常量的列表必须以;结尾。

枚举默认父类是Enum类,他是java.lang包下的。

public class EmunDemo {
	public static void main(String[] args) {
		doWhat(Week.MON);
	}
	public static void doWhat(Week week){
		switch (week) {
		case MON:
		case TUE:
		case WEN:
		case THU:
		case FRI:
			System.out.println("工作日!");
			break;
		case SAT:
			System.out.println("今天周六!");
			break;
		case SUN:
			System.out.println("今天周天!");
			break;
		}
	}
}

Math类,他提供了常用的数学运算方法和两个静态常量,E(自然对数的底数)和PI(圆周率),Math类中定义的方法都是公有的静态方法,不需要创建Math对象可以直接调用。

常用方法如下:

1、max方法,比较两个数大小,int num = Math.max(20,25);

2、求幂运算方法,pow方法。返回a的b次幂的值。例如,求2的3次幂,double powNum = Math.pow(2,3);

3、得到0到1之间的double类型的随机数的方法random方法。返回值大于等于0.0,小于等于1.0。

产生1到10之间随机数的方法 int random = (int)(Math.random()*10+1);

求绝对值的方法,abs()方法、求两个数较小数的方法min()方法、求正平方根方法sqrt()方法……

常用的字符串操作类:String、StringBulider、StringBuffer

String类的特点:是一个String对象所包含的字符串内容永远不会改变。

String类常用的方法:

1、equals方法(比较时区分大小写)。

2、trim方法,他返回一个去掉前后空格的字符串副本。

3、equalsIgnoreCase方法,比较时不区分大小写。

4、substring方法,截取字符串某一部分的方法。

5、indexOf()方法,返回指定子字符串在此字符串中第一次出现的位置。

6、length()方法,获取字符串长度。

7、toLowerCase()和toUpCase()方法,将字符串转换为小写和大写的方法。

例如:

public class StringDemo {
	public static void main(String[] args) {
		String str1 = "   tom";
		String str2 = "tom";
		System.out.println(str1);
		System.out.println(str1.trim());
		System.out.println(str1.equals(str2));
		System.out.println(str1.trim().equals(str2));
		System.out.println("TOM".equalsIgnoreCase(str2));
		String s = "我是中国人!";
		System.out.println(s.substring(2,4));
		System.out.println(s.indexOf("国"));
	}
}
运行结果

   tom
tom
false
true
true
中国
3
StringBuffer对象的特点是可变长的,StringBuffer对象可以将一些字符和子字符串插在原字符串中间或者末尾。他经常预分配比实际需要多一些的字符空间。

创建StringBuffer对象方法由两种,一种是不带参数的,他是创建一个不带字符,但具有指定初始容量的StringBuffer对象,例如StringBuffer sb = new StringBuffer;另一种是带String类型参数的,它是将其内容初始化为指定的字符串内容,该字符串的容量为16加上字符串的长度,例如StringBuffer sbf = new StringBuffer("我会变长");

能对StringBuffer对象进行修改的是append方法,他能把任何数据的字符串表示形式添加到StringBuffer对象的末尾。

reverse方法,是将字符串用反转形式取代,比如有一个人叫”万千一“,反转后输出”一千万“。

public class StringDemo {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("万千一");
		StringBuffer sb1 = sb.reverse();
		System.out.println(sb1);
		System.out.println(sb.append(sb1));
	}
}
一千万
一千万一千万

Date类

Date类对象记录格林尼治标准事件1970年1月1日0时0分0秒开始至今的毫秒数。

注意在Calender类中,月份是从0-11计算的,所有要在月份上+1。

DateFormat类提供了多种格式化和解析时间的方法。格式化是将日期转换为文本,解析是将文本转换成日期格式,我们常用是DateFormat的子类SimpleDateFormat类。

SimpleDateFormat可以通过用户定义的时间日期格式的模式完成格式化和解析操作。所谓的日期和时间格式其实就是由日期和时间模式字符串指定。而日期和时间模式字符串是由具体特定含义的字母组成,我们称这些字母是模式字母。

format方法,他是将一个Date对象格式化成日期/时间字符串。

parse方法,他是从给定字符串的开始解析文本,来生成一个日期对象。

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateDemo {
	public static void main(String[] args) {
		//使用默认时区和语言环境获得一个日历。
		Calendar calendar = Calendar.getInstance();
		//获取当前日历的年月日、时分秒
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		int second = calendar.get(Calendar.SECOND);
		System.out.println("今天是"+year+"年"+month+"月"+day+"日");
		System.out.println("现在是:"+hour+":"+minute+":"+second);
		/*判断今年是否是闰年*/
		GregorianCalendar gregorianCalendar=new GregorianCalendar();
		boolean flag=gregorianCalendar.isLeapYear(gregorianCalendar.get(gregorianCalendar.YEAR));
		System.out.println("今年是否为闰年?"+flag);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G  HH:mm:ss z");
		String s = sdf.format(new Date());
		System.out.println(s);
		SimpleDateFormat dateformat2=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");   
	    String s2=dateformat2.format(new Date());
	    System.out.println(s2);
	}
}
运行结果:今天是2014年7月24日
现在是:21:8:41
今年是否为闰年?false
2014.07.24 公元  21:08:41 CST
2014年07月24日 21时08分41秒 星期四 

 Random类 

他是一个伪随机数生成器,他又两个构造方法,第一个构造方法是创建一个新的伪随机数生成器,它使用当前时间为初始值,也就是种子数。第二种构造方法是允许定义一个long类型的种子来创建一个新的伪随机数生成器。

public class RandomDemo {
	public static void main(String[] args) {
		Random random = new Random();
		//产生10个int类型随机数
		for(int i = 0;i<10;i++){
			System.out.print(random.nextInt()+" ");
		}
		System.out.println();
		//产生10个0-10之间的随机数
		for(int i = 0;i<10;i++){
			System.out.print(random.nextInt(10)+" ");
		}
		System.out.println();
		//产生10个boolean类型随机数
		for(int i = 0;i<10;i++){
			System.out.print(random.nextBoolean()+" ");
		}
	}
}
803076482 -934803243 924070634 1335636195 -1225520841 812869566 795495620 468179664 1860663960 1922764468 
7 3 3 6 3 9 7 8 3 1 
true false false true false false false true false true 














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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值