Java 常用类库

来源:互联网 发布:excel去重复数据计数 编辑:程序博客网 时间:2024/05/24 05:05

一、知识点

StringBuffer

国际化程序

Math

Random 

Arrays 

日期操作类

比较器

正则表达式

 

二、具体类容

StringBuffer

 

 

 

实例:

public class MyStringBuffer {

public static void main(String[] args) {

//字符串连接

StringBuffer sb = new StringBuffer();//构造一个空字符串对象

sb.append("Hello Android");//添加字符串

sb.append(你好吗?   ");

sb.append(11111);

sb.append(123).append("  dad");

for (int i = 0; i < 5; i++) {

sb.append(i);

}

System.out.println(sb);

//转换成String

String str = sb.toString();

//替换

sb.replace(0, 5, "caoye");

System.out.println(sb);

//反转

//sb.reverse();

//System.out.println(sb);

//插入内容

sb.insert(5, "abc");

System.out.println(sb);

//字符串截取

String str1 = sb.substring(5);//从参数位置开始截取

System.out.println(str1);

String str2 = sb.substring(5, 8);//范围  包括5  不包括8  半闭半开区间

System.out.println(str2);

//查找

int a = sb.indexOf("a");

System.out.println(a);

int b = sb.indexOf("a",6);//数字表示起始位置

System.out.println(b);

//查找单个字符

char c = sb.charAt(4);

System.out.println(c);

//删除

System.out.println(sb);

sb.delete(0, 5);

System.out.println(sb);

}

}

 

国际化程序

public class MyInternation {

public static void main(String[] args) {

Locale locale = new Locale("zh","CN");//中国的Locale对象

Locale locale1 = new Locale("en","US");//美国的Locale对象

Locale locale2 =Locale.getDefault();

System.out.println(locale2.getDisplayCountry()+"   "+locale2.getDisplayLanguage()+"   "+

locale2.getDisplayName());

ResourceBundle res = ResourceBundle.getBundle("Message");

String str = res.getString("info");//键值对

System.out.println(str);//文件默认格式    IOS 8859-1

//实现国际化

ResourceBundle res1 = ResourceBundle.getBundle("Message",locale);

ResourceBundle res2 = ResourceBundle.getBundle("Message",locale1);

String zh = res1.getString("info");

String en = res2.getString("info");

System.out.println(zh);

System.out.println(en);

//处理动态数据  Messageformat   修改信息格式

System.out.println(MessageFormat.format(zh"哈哈","涅米"));//可变参数   int

System.out.println(MessageFormat.format(en"abc","oiu"));

//div(1, 5,6,7,8788,45);

}

/******************************/

//可变参数

public static void div(int i , int ...j){

}

/******************************/

}

Math类

public class MyMath {

public static void main(String[] args) {

int i = -2;

int temp = Math.abs(i);

System.out.println(temp);

double s = Math.cbrt(27);

System.out.println(s);

System.out.println(Math.ceil(-4.5));

System.out.println(Math.floor(-4.5));

System.out.println(Math.pow(5, 2));

int ran = (int) (Math.random()*50+50);

System.out.println(ran);

System.out.println(Math.round(-8.8));

}

}

 

 

Random类

public class MyRandom {

public static void main(String[] args) {

Random rm = new Random();

int temp = rm.nextInt();//int所有的范围内随机

int temp1 = rm.nextInt(5);//不包括5  

System.out.println(temp1);

}

}

 

 

Arrays

public class MyArrays {

public static void main(String[] args) {

//Arrays binarySearch

int a[] = {2,232,45,5,55,23};

Arrays.sort(a);//排序

System.out.println(Arrays.toString(a));//数组输出

//int index = Arrays.binarySearch(a, 52);//二分法查找

//System.out.println(index);

/*

 * public static int binarySearch(int[] a,

                               int fromIndex,

                               int toIndex,

                               int key)

 */

int index = Arrays.binarySearch(a, 1, 4, 55);//二分法查找

System.out.println(index);

/*

 * public static <T> T[] copyOfRange(T[] original,

                                  int from,

                                  int to)

 */

//复制数组

int b[] = Arrays.copyOf(a, 10);

System.out.println(Arrays.toString(a));

System.out.println(Arrays.toString(b));

/*

 * public static <T> T[] copyOfRange(T[] original,

                                  int from,

                                  int to)

                                  参数:

original - 将要从其复制一个范围的数组

from - 要复制的范围的初始索引(包括)

to - 要复制的范围的最后索引(不包括)。(此索引可以位于数组范围之外)。 

 */

int c[] = Arrays.copyOfRange(a, 1, 4);

System.out.println(Arrays.toString(c));

//初始化数组中的元素到指定数

Arrays.fill(a, 5);

System.out.println(Arrays.toString(a));

}

}

 

日期操作类

Date

public class MyDate {

public static void main(String[] args) {

Date date = new Date();

System.out.println(date);

//Date(long date) 毫秒值

//当前日期的毫秒值

long time = System.currentTimeMillis();//返回:当前时间与协调世界时 1970 年 月 日午夜之间的时间差(以毫秒为单位测量)。

long newtime = 60*60*1000;

System.out.println(time);

Date date1 = new Date(time+newtime);//date1.getTime() 获得毫秒值

System.out.println(date1);

//System.out.println(1900+date1.getYear());

//System.out.println(date1.getMonth()+1);

//System.out.println(date1.getDay());

}

}

 

Calendar类

public class MyCalendar {

public static void main(String[] args) {

//初始化1

Calendar calendar1  = new GregorianCalendar();

//初始化2

Calendar calendar = Calendar.getInstance();

System.out.println(calendar.get(Calendar.YEAR));

System.out.println(calendar.get(Calendar.MONTH)+1);

System.out.println(calendar.get(Calendar.DAY_OF_YEAR));//当前年过了多少天

System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//当前月份的第一天

System.out.println(calendar.get(Calendar.DAY_OF_WEEK));//按照美国周期开始算

System.out.println(calendar.get(Calendar.HOUR_OF_DAY));//24小时制

System.out.println(calendar.get(Calendar.HOUR));//12小时制

System.out.println(calendar.get(Calendar.MINUTE));

System.out.println(calendar.get(Calendar.SECOND));

System.out.println(calendar.get(Calendar.MILLISECOND));

}

}

 

SimpleDateFormat

public class MySimpleDateFormat {

public static void main(String[] args) {

//日期转换成固定格式的时间

Date date = new Date();

System.out.println(date);

//20150507日  151610123毫秒

String str = "yyyyMMdd日  HHmmssSSS毫秒";

SimpleDateFormat sdf = new SimpleDateFormat(str);

String timestr = sdf.format(date);

System.out.println(timestr);

//固定格式的时间转化成日期

try {

Date date1 = sdf.parse(timestr);

System.out.println(date1.toLocaleString());

catch (ParseException e) {

e.printStackTrace();

}

}

}

 

 

 

 

 

0 0
原创粉丝点击