Java常用类

1、随机输出班上5名学生的学号。

import java.util.*;
public class num3 {

    public static void main(String[] args){
        int a[];
        a = new int[5];
        
        for(int i=0;i<5;i++){
            a[i]=(int)(Math.random()*55+1);
            System.out.println(a[i]);
        }
    }
}

2、获取当前系统时间,运用subString()方法,显示年份。
3,使用Calendar类显示当前日期。


import java.text.SimpleDateFormat;
import java.util.*;
public class fate { 
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println("时间:"+date);
        String strDate = date.toString();
        String year = strDate.substring(24,28);
        String month = strDate.substring(4,7);
        String day = strDate.substring(9,10);
        String hour = strDate.substring(11,13);
        String min = strDate.substring(14,16);
        String sec = strDate.substring(17,19);      
        System.out.print(year+"年");
        System.out.print(month+"月");
        System.out.print(day+"日   ");
        System.out.print(hour+"时");
        System.out.print(min+"分");
        System.out.println(sec+"秒");
      //2,获取当前系统时间,运用subString()方法,显示年份。
        SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy年");
        String a1 = dateformat1.format(new Date());
        System.out.println("时间:"+a1);
      //3,使用Calendar类显示当前日期。
        Calendar a2 = Calendar.getInstance();      
        System.out.print(a2.get(Calendar.YEAR)+"年");
        System.out.print((a2.get(Calendar.MARCH)+1)+"月");
        System.out.print(a2.get(Calendar.DATE)+"日");
        System.out.print(a2.get(Calendar.HOUR)+":");
        System.out.print(a2.get(Calendar.MINUTE)+":");
        System.out.print(a2.get(Calendar.SECOND));      
    }
}

4、分别利用ArrayList类、LinkedList类、Vector类创建集合,并实现相关用法。

import java.util.*;
public class fate { 
    public static void main(String[] args) {
        ArrayList a1 = new ArrayList();
        a1.add("月");
        a1.add("火");
        a1.add("水");
        a1.add("木");
        a1.add("金");
        a1.add("土");
        a1.add("日");
        int i;
        Scanner c = new Scanner(System.in);     
        System.out.println("输入星期数");
        i = c.nextInt();
        System.out.println(a1.get(i-1)+"曜日");
    }
}

5、总结

      没有及时的做好课前预习和课后复习工作,导致上实训课的时候比较吃力,原来上课听懂的地方也会变得模糊,需要翻书。

实验内容和原理:

1、 将布尔型、整型、长整型、双精度型作为参数,实例化相应的包装类对象,并输出对象的数值。

2、 按照“XXXX年XX月XX日 XX时XX分XX秒”的格式,输出当前时间。
以下来自https://www.cnblogs.com/sucker/p/10980183.html

package work;

public class Work1 {
    Boolean b;
    Integer i;
    Long l;
    Double d;
    public Work1(boolean b, int i, long l, double d)
    {
        this.b = new Boolean(b);
        this.i = new Integer(i);
        this.l = new Long(l);
        this.d = new Double(d);
        System.out.println(this.b);
        System.out.println(this.i);
        System.out.println(this.l);
        System.out.println(this.d);
    }
    public static void main(String[] args) {
        new Work1(true, 99, 1437631334, 5.4451356);
    }
}
package work;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class work2 {
    public static void main(String[] args) {
        Date d = new Date();
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日    HH时mm分ss秒", Locale.CHINA);
        System.out.println(df.format(d));        

    }

}

转载于:https://www.cnblogs.com/BKKITO/p/10978808.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值