工具包【日期类、格式化、Math类】的使用

来源:互联网 发布:mac笔记本电脑价格 编辑:程序博客网 时间:2024/06/06 05:25

日期类【Date、Calendar】

基本上所有类中,日期月份都是0-11 (没有12)    时间:0~23   
Date 是个类需要new来使用  (Date   名=new Date();)
其中主要方法:
after(Date when)    before(Date when)    对比时间的前后,若为相同则返回false 
getTime()    返回当前对象的毫秒值
setTime(long time)    设置时间
a.compareTo(b)   返回为1则  a>b  -1则 b>a    0则相同
     
Calendar 是个抽象的类,不能new,得打点调用(.getInstance())
其中主要方法:
getTime()     返回  Date
add(int field,int amount)      无返回值     field带表区域(时区)  

格式化【SimpleDateFormat、Locale、NumberFormat】

调用的方法   SimpleDateFormat(或Number...)  名=new SimpleDateFormat(或Nu
mber...)  ("【符号】")

Math 类

调用的方法    先导包lang包中的Math   然后在需要的位置Math打点调用方法

Colleations 工具类【util工具包中】

调用的方法    Collections.  使用方法


具体如下
public static void main(String[] args) {List<Tt> list=new ArrayList<Tt>();list.add(new Tt("杜蕾斯", 66));list.add(new Tt("杰士邦", 26));list.add(new Tt("冈本", 10));list.add(new Tt("第六感 ", 40));System.out.println("排序前------------------------------------");Show(list);Collections.sort(list, new TtComparator());System.out.println("排序后------------------------------------");Show(list);Collections.reverse(list);System.out.println("反转后-------------------reverse-----------------");Show(list);//Collections.reverse(list);//Show(list);System.out.println("随机排序后----------------shuffle-----------------");Collections.shuffle(list);Show(list);System.out.println("自然升序排序后------------------------------");Collections.sort(list, new TtComparator());Show(list);System.out.println("自然升序排序后---------------sort---------------");Collections.sort(list, new TtComparator());Show(list);System.out.println("1和3交换后---------------swap---------------------");Collections.swap(list, 1, 3);Show(list);System.out.println("整体右移2个size---------------rotate---------------------");Collections.rotate(list, 2);Show(list);Collections.sort(list,new TtComparator());//System.out.println("检查元素-----------------binarySearch------------------------------");//int x=Collections.binarySearch(list, new Tt("杜蕾斯", 66));//System.out.println(x);//Show(list);System.out.println("最大值------------------max------------------------");Tt t=Collections.max(list);System.out.println(t);Tt t1=Collections.max(list, new TtComparator());System.out.println(t1);System.out.println("使用制定元素替代指定列表中的元素----------fill----------");Collections.fill(list, new Tt("糖葫芦", 199));Show(list);int count=Collections.frequency(list, new Tt("糖葫芦", 199));System.out.println(count);Collections.replaceAll(list, new Tt("糖葫芦", 199), new Tt("杜蕾斯", 66));Show(list);System.out.println("--------------------end---------------------");}



原创粉丝点击