java_Java工具包(Arrays,Date,SimpleDateFormat,NumberFormat,DecimalFormat,Calendar,Math)

来源:互联网 发布:超盟数据李健豪 编辑:程序博客网 时间:2024/06/05 10:20
Arrays(java.util.Arrays)

void Arrays.sort(T[])

Arrays类的sort方法实现各种数据类型的排序

import java.util.Arrays; //Arrays类的sort方法实现各种数据类型的排序

public class EArrays {

        public static void main(String[] args) {

                int[] intDatas = {23,18,83,29,90,85,67,5,10}; //int型数组intDatas

                Arrays.sort(intDatas);  //对intDatas排序

                //对排序后的数组intDatas进行输出

for(int i : intDatas) //针对每个元素

                {

                        System.out.print(+" ");

                }

                System.out.println();

                char[] charDatas = {'z','E','c','F','t','p','W'}; //char类型数组charDatas

                Arrays.sort(charDatas);  //charDatas排序

                for(char ch : charDatas)  //对排序后的数组charDatas进行输出

                {

                        System.out.print(ch+" ");

                }

        }

}

 

在sort方法中,遇到对象数组的排序时,要给对象数组提供排序的依据,实现Comparator接口,可以在接口的compare方法中指定排序规则,实现Comparator接口的对象称为比较器。

import java.util.Arrays;

import java.util.Comparator;

 

class Student{

        String name;

        int age;

        public Student(String name, int age)

        {

                super();

                this.name = name;

                this.age = age;

        }

        public String toString()

        {

                return (name + "," + age);

        }

}

public class StuCom implements Comparator<Student>  //定义比较器实现Comparator接口否则系统无法对一个对象数组进行搜索规则

{

        public int compare(Student stu1, Student stu2)  //实现接口Comparatorcompare方法制定比较规则

        {

                if(stu1.age > stu2.age)

                {

                        return 1;

                }

                else if(stu1.age == stu2.age)

                {

                        return 0;

                }

                else

                {

                        return -1;

                }

        }

        public static void main(String[] args)

        {

                Student[] stus = new Student[]{new Student("小美",21),

                                new Student("阿聪",22), new Student("武大郎",28),

                                new Student("阮小七",26),new Student("晁盖",30),

                                new Student("鲁智深",29),new Student("孙二娘",26),

                                new Student("扈三娘",23),new Student("武松",24)};

                Arrays.sort(stus,new StuCom()); //排序

                for(Student student:stus)

                {

                        System.out.println(student);

                }

        }

}

 

List Arrays.asList(Object[] objs)把指定的数组转换为List对象

import java.util.Arrays;

import java.util.List;

 

public class EList {

 

        public static void main(String[] args) {

                String[] strs = {"aaa","bbb","ccc","ddd","eee","fff","ggg","hhh","iii","jjj"};

                List<String> list = Arrays.asList(strs); //

                for(int i = 0; i < list.size(); i++)

                {

                        System.out.println(list.get(i));

                }

        }

 

}

 

int Arrays.binarySearch(T[] objs, key)

int Arrays.binarySearch(T[] objs, int fromIndex, int toIndex, key)

在数组objs中查找key的位置返回key的下标查找不到返回负值

在binarySearch方法调用前要保证数组是排好序的,若没有排序,可用Arrays.sort()排序

binarySearch()使用:

String[] strs = {"aaa","bbb","ccc","ddd","eee","fff","ggg","hhh","iii","jjj"};

Arrays.sort(strs);

System.out.println(Arrays.binarySearch(strs, 4, 8,"ccc"));

System.out.println(Arrays.binarySearch(strs,"ccc"));

binarySearch()搜索对象:

import java.util.Arrays;

 

class StuComChild extends StuCom

{

        public int compare(Student stu1, Student stu2)  //方法重写

        {

                if(stu1.age > stu2.age)

                {

                        return 1;

                }

                else if(stu1.age == stu2.age && stu1.name.equals(stu2.name))

                {

                        return 0;

                }

                else

                {

                        return -1;

                }

        }

}

public class EBinarySearch {

 

        public static void main(String[] args) {

                Student[] stus = new Student[]{new Student("小美",21),

                                new Student("阿聪",22), new Student("武大郎",28),

                                new Student("阮小七",26),new Student("晁盖",30),

                                new Student("鲁智深",29),new Student("孙二娘",26),

                                new Student("扈三娘",23),new Student("武松",24)};

                Student s = new Student("晁盖",30);

                System.out.println(Arrays.binarySearch(stus, s, new StuComChild()));

        }

 

}

Arrays还有方法:

T[] copyOf(T[] t, int length) 把一个数组复制到一个长度为length的新数组中

fill(T[] t, N newValue) 用一个固定值填充数组中所有元素

 

⒉Date(java.util.Date)

Date类包装了自1970年1月1日00 :00 :00 GMT开始到现在经过的毫秒值,该类的大部分构造器和方法都已经过时,但在数据库操作中,它允许将毫秒值表示为SQL DATE值,是java.sql.Date的父类。

构造方法:

构造方法

说明

Date()

按照当前系统时间构造一个Date对象

Date(long date)

按照给定的时间毫秒值构造个Date对象

方法:

返回类型

方法

说明

boolean

after(Date when)

测试当前对象表示的时间是否在指定时间之后

boolean

before(Date when)

测试当前对象表示的时间是否在指定时间之前

long

getTime()

返回当前对象对应的时间毫秒值

void

setTime(long time)

设置时间

示例代码:

import java.util.Date;

 

public class EDate {

        public static void main(String[] args) {

                Date date = new Date(); //当前系统时间

                System.out.println(date);

                date.setTime(0L); //设置时间

                System.out.println(date); /*java.util.Date类型表示的是GMT时间,

                                本身输出是国际化输出,中国在东八区,

                                所以输出结果是8点,

                                而Date的其它方法不容易实现国际化,所以是过时的

                                更灵活的时间类可参考java.util.Calendar

                                    */

                date.setTime((10L*365+2)*24*60*60*1000); //设置时间为1970年后10年的时间的毫秒值

                System.out.println(date);

                System.out.println(date.after(new Date())); //date是否在当前时间之后

                System.out.println(date.before(new Date())); //date是否在当前时间之前

                System.out.println(date.getTime()); //date对应的毫秒值

        }

}

 

DateFormat (java.text.DateFormat日期时间格式化)

Date格式有四种,这四种格式被定义为DateFormat类的常量:

格式

说明

SHORT

09-8-20

MEDIUM

2009-8-20

LONG

2009年8月20日

FULL

2009年8月20日 星期四

Locale类的对象表示了不同的地区,例如Locale.China表示中国地区,Locale.US表示美国地区

DateFormat是一个抽象类,不能直接实例化,可使用以下静态方法得到DateFormat的对象:

方法

说明

getDateInstance()

返回默认地区默认格式的关于日期的DateFormat对象

getDateInstance(int)

返回指定格式、默认地区的关于日期的DateFormat对象

getDateInstance(int,Locale)

返回指定格式、指定地区的关于日期的DateFormat对象

getTimeInstance()

返回默认地区、默认格式的关于时间的DateFormat对象

getDateInstance(int)

返回指定格式、默认地区的关于时间的DateFormat对象

getDateInstance(int,Locale)

返回指定格式、指定地区的关于时间的DateFormat对象

getDateTimeInstance()

返回默认地区、默认日期格式、默认时间格式的关于日期和时间的DataFormat对象

getDateTimeInstance(int,int)

返回默认地区、指定日期格式、指定时间格式的关于日期和时间的DateFormat对象

getDateTimeInstance(int,int,Locale)

返回指定地区、指定日期格式、指定时间格式的关于日期和时间格式的DateFormat对象

调用DateFormat对象的format方法可以把Date对象转换成指定格式的String类型

import java.text.DateFormat;

import java.util.Date;

import java.util.Locale;

 

public class EDateFormat {

 

        public static void main(String[] args) {

                Date today = new Date(); //获取系统日期时间

                Locale locale1 = Locale.CHINA; //地区CHINA

                DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT,locale1);  //格式1

                DateFormat df2 = DateFormat.getTimeInstance(DateFormat.MEDIUM,Locale.US); //格式2

                DateFormat df3 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,Locale.UK);//格式3

                System.out.println(locale1.getDisplayCountry() + "short日期格式:" + df1.format(today));

                System.out.println((Locale.US).getDisplayCountry() + "medium时间格式:" + df2.format(today));

                System.out.println((Locale.UK).getDisplayCountry() + "medium日期、medium时间格式:" + df3.format(today));

        }

 

}

 

⒋SimpleDateFormat(java.text.SimpleDateFormat)

该类用字符串指定日期和时间的格式,字符串中的字符成为模式字符(区分大小写),常见的模式字符定义如下:

字母

日期或时间元素

字母

日期或时间元素

y

a

Am/Pm标记

M

年中的月份

H

一天中的小时数(0~23)

w

年中的周数

k

一天中的小时数(1~24)

W

月份中的周数

K

am/pm中的小时数(0~11)

D

年中的天数

h

am/pm中的小时数(1~12)

d

月份中的天数

m

小时中的分钟数

F

月份中的星期

s

分钟中的秒数

E

星期中的天数

S

毫秒数

代码示例:

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

 

public class ESimpleDateFormat {

 

        public static void main(String[] args) {

                Date today = new Date(); //获取系统的日期时间

                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:sss"); //格式1

                SimpleDateFormat sdf2 = new SimpleDateFormat("yy/MM/dd HH:mm:ss"); //格式2

                System.out.println(sdf1.format(today)); //按格式1输出today

                System.out.println(sdf2.format(today)); //按格式2输出today

                //解析日期

                String birthday = "1992-04-08";

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                try

                {

                        Date bir = sdf.parse(birthday);  //将字符串birthday解析成日期赋给bir

                        System.out.println(bir);         //输出bir

                        System.out.println(sdf.format(bir));    //按sdf格式输出bir

                }

                catch(ParseException e) //处理异常

                {

                        e.printStackTrace();

                }

        }

 

}

 

⒌NumberFormat、DecimalFormat(java.text..NumberFormat)数字格式化

NumberFormat是所有数值格式的抽象基类,此类提供格式化和解析数值的接口。若要格式化当前Locale的数值,可使用方法myString = NumberFormat.getInstacnce(Locale.FRENCH) ;

方法

说明

getInstance()

获得常规数值格式,可以指定Locale参数

getNumberInstance()

获得常规数值格式,可以指定Locale参数

getIntegerInstance()

获取整数数值格式,可以指定Locale参数

getCurrencyInstance()

获取货比数值格式,可以指定Locale参数,格式化后的数据前面会有一个货币符号“¥”

getPercentInstance()

获取显示百分比的格式,可以指定Locale参数

示例代码:

import java.text.NumberFormat;

import java.util.Locale;

 

public class ENumberFormat {

 

        public static void main(String[] args) {

                double num1 = 230456789;

                double num2 = 0.234;

                NumberFormat nf1 = NumberFormat.getInstance(Locale.CHINA);

                NumberFormat nf2 = NumberFormat.getCurrencyInstance(Locale.CHINA);

                NumberFormat nf3 = NumberFormat.getCurrencyInstance(Locale.US);

                NumberFormat nf4 = NumberFormat.getPercentInstance();

                System.out.println(nf1.format(num1));

                System.out.println(nf2.format(num1));

                System.out.println(nf3.format(num1));

                System.out.println(nf4.format(num1));

                System.out.println(nf1.format(num2));

                System.out.println(nf2.format(num2));

                System.out.println(nf3.format(num2));

                System.out.println(nf4.format(num2));

        }

 

}

 

DecimalFormat(通过模式字符串对数字格式化)

import java.text.DecimalFormat;

 

public class EDecimalFormat {

 

        public static void main(String[] args) {

                long num1 = 1234567000000L;

                double num2 = 0.1653456;

                DecimalFormat df1 = new DecimalFormat("#,###"); //#:一个位置数字,不存在则不显示,0不显示小数多余部分四舍五入

                                                          //,:分隔符

                DecimalFormat df2 = new DecimalFormat("#.00");  //0:一个位置数字不存在用0填充小数多余部分四舍五入

                DecimalFormat df3 = new DecimalFormat("00.#");  //.:小数点

                DecimalFormat df4 = new DecimalFormat("0.##E0");//E:科学计数法

                DecimalFormat df5 = new DecimalFormat("0.##%"); //%:用百分数表示数字

                System.out.println(df1.format(num1));

                System.out.println(df2.format(num2));

                System.out.println(df3.format(num2));

                System.out.println(df4.format(num1));

                System.out.println(df5.format(num2));

        }

 

}

 

⒍Calendar(java.util.Calendar)

Calendar抽象类,为特定的值如YEAR、MONTH、DAY_OF_MONTH、HOUR等日历字段之间的转换和操作日历字段(例如获得下星期的日期)提供了丰富的方法,并且可以方便地与Date类型进行相互转换。

使用静态方法getInstance()和getInstance(Locale locale)获取Calendar对象。Calendar定义的常量字段如下表:

返回值

字段

说明

static int

AM

指示从午夜到中午之前这段时间的AM_PM值

static int

DATE

get和set的字段,指示一个月中的某天

static int

DAY_OF_MONTH

get和set的字段,指示一个月中的某天

static int

DAY_OF_WEEK

get和set的字段,指示一个星期中的某天

static int

DAY_OF_YEAR

get和set的字段,指示一个当前年中的天数

static int

HOUR

get和set的字段,指示上午或下午的小时

static int

HOUR_OF_DAY

get和set的字段,指示一天中的小时

static int

MINUTE

get和set的字段,指示一小时中的分钟

static int

MONTH

指示月份中的get和set的字段

static int

PM

指示从中午到午夜这段时间的AM_PM字段值

static int

SECOND

get和set的字段,指示一分钟中的秒

static int

WEEK_OF_MONTH

get和set的字段,指示当前月中的星期数

static int

WEEK_OF_YEAR

get和set的字段,指示当前年中的星期数

static int

YEAR

表示年的get和set字段

Calendar类中常用的方法:

返回

方法

说明

void

add(int field, int amount)

根据日历的规则,为给定的日历字段添加或减去指定的时间量

boolean

after(Object when)

判断Calendar表示的时间是否在指定Object表示的时间之后

boolean

before(Object when)

判断Calendar表示的时间是否在指定Object表示的时间之前

int

get(int field)

返回给定日历字段的值

int

getActualMaximum(int field)

给定此Calendar的时间值,返回指定字段可能拥有的最大值

int

getActualMinimum(int field)

给定此Calendar的时间值,返回指定字段可能拥有的最小值

Date

getTime()

返回此Calendar的时间值(从历元至现在的毫秒偏移量)的Date对象

long

getTimeInMillis()

返回此Calendar的时间值,以毫秒为单位

void

set(int field, int value)

设置给定日历字段为给定值

void

set(int year, int month, int dayOfMonth)

设置日历相应字段的值

void

set(int year, int month, int dayOfMonth, int hourOfDay, int minute)

设置日历相应字段的值

void

set(itn year, int month, int dayOfMonth, int hourOfDay, int minute, int second)

设置日历相应字段的值

void

setTime(Date date)

用给定的Date值设置此Calendar的时间

void

setTimeInMillis(long millos)

用给定的long值设置此Calendar的时间

示例代码如下:

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

 

public class ECalendar {

 

        public static void main(String[] args) {

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                Calendar cale = Calendar.getInstance();

                cale.set(2015, 5, 3);  //设置年月日 Calendar月份从0开始星期天被认为是第一天

                cale.set(Calendar.DAY_OF_WEEK, 4); //把天定位到星期三

                Date date1 = cale.getTime();  //Calendar类型转换为Date类型

                System.out.println(sdf.format(date1));

                cale.set(Calendar.MONTH, 4);  //设置月

                cale.set(Calendar.DAY_OF_MONTH, 3); //设置日

                cale.set(Calendar.YEAR, 2014); //设置年

                Date date2 = cale.getTime();

                System.out.println(sdf.format(date2));

        }

 

}

 

⒎Math(java.lang.Math)

Math类中定义的常量:Math.E自然对数的底数       Math.PI圆周率

Math类中主要的方法:

返回

方法

说明

static T

abs(T a)

返回a的绝对值

static double

acos(double a)

返回一个值的反余弦(0.0~pi)

static double

atan(double a)

返回一个值的反正切 (-pi/2~pi/2)

static double

ceil(double a)

返回最小的(最接近-)double值,该值≥参数,并等于某个整数

static double

cos(double a)

返回角的三角余弦

static double

exp(double a)

返回e的a次幂的值

static double

floor(double a)

返回最大的(最接近+)double值,该值≤参数,并等于某个整数

static double

log(double a)

返回double值的自然对数(底数是e)

static double

log10(double a)

返回double值的底数为10的对数

static T

max(T a, T b)

返回两个值中的较大的一个

static T

min(T a, T b)

返回两个值中的较小的一个

static T

pow(T a, T b)

返回第一个参数的第二个参数次幂的值

static double

random()

返回带正号的double值,该值≥0.01.0

static int

round(float a)

返回最接近参数的int

static double

sin(double a)

返回角的三角正弦

static double

sqrt(double a)

返回正确舍入的double值的平方根

static double

tan(double a)

返回角的三角正切 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果id被锁了怎么办 新浪微博支付宝解绑失败怎么办 阿里妈妈升级看不到引流人数怎么办 阿里妈妈账号被冻结怎么办 微博昵称到次数怎么办 五星好评之后忘记截图了怎么办 评价后忘了截图怎么办 好评率太低不能买东西了怎么办 淘宝评价被删了怎么办 淘宝店铺有流量没有成交怎么办 淘宝好评被删了怎么办 淘宝评论被系统删除怎么办 淘宝被商家删除评价怎么办 淘宝评价管理商家删除了怎么办 淘宝商家删除评价我该怎么办 我的评价隐藏了怎么办 淘宝把评论删了怎么办 淘宝虚假交易被删除评价怎么办 淘宝好评评错了怎么办 被淘宝骗了好评怎么办 美团好评被删了怎么办 卖家收到好评内容是差评怎么办 淘宝收货电话写错了怎么办 淘宝评价写错了怎么办 饿了么商家差评怎么办 淘宝不给补差价怎么办 淘宝顾客给差评怎么办 淘宝买家账号体检违规怎么办 买家淘宝账户体检中心违规怎么办 淘宝卖家电话骚扰该怎么办 手机欠费销户了怎么办 想下载好多个淘宝怎么办 送快递不记得路怎么办 淘宝物流弄丢了怎么办 邮政快递碰上难缠客户怎么办 举证工伤对方不签收怎么办 快递员收件的钱怎么办 锐捷网卡是空的怎么办 mac系统excel太慢怎么办 二手车交易发票丢了怎么办 转转上买二手电脑被骗了怎么办