第十次作业

 

1,简述String类中的equals方法与Object类中的equals方法的不同点。
Object类的equals比较的是两个对象的地址
String类的equals比较的是两个字符串的内容
2,编写程序,(Scanner)当以年-月-日的格式输入一个日期时,输出其该年是否为闰年,该月有几天,该日是星期几;

public static void main(String[] args) throws ParseException {
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入格式:“xxxx年xx月xx日”的日期");
    String str=sc.nextLine();
    SimpleDateFormat sun=new SimpleDateFormat("yyyy年MM月dd日");
    Date s=sun.parse(str);
    Calendar u=Calendar.getInstance();
    u.setTime(s);
    int year=u.get(Calendar.YEAR);
    int month=u.get(Calendar.MONTH)+1;
    int week=u.get(Calendar.DAY_OF_WEEK)-1;
    GregorianCalendar g=new GregorianCalendar();
    if (g.isLeapYear(year)) {
        System.out.println(year+"是闰年");
    }else {
        System.out.println(year+"不是");
    }
    int month_num=u.getActualMaximum(Calendar.DAY_OF_MONTH);
    if (week==0) {
        System.out.println("该月有"+month_num+"天,"+"该日是周日");
    }else {
        System.out.println("该月有"+month_num+"天,"+"该日是周"+week);
    }
}

3,计算某年、某月、某日和某年、某月、某日之间的天数间隔和周数。

public static void main(String[] args) throws ParseException {
    Scanner sc= new Scanner(System.in);
    System.out.println("请输入日期1:“xxxx-xx-xx”");
    String s=sc.nextLine();
    System.out.println("请输入日期2:“xxxx-xx-xx”");
    String u=sc.nextLine();
    SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
    Date n=sd.parse(s);
    Date n2=sd.parse(u);
    long i=0;
    if (n.after(n)) {
        i=n.getTime()-n2.getTime();
    }else {
        i=n2.getTime()-n.getTime();
    }
    long a=1000*60*60*24;
    long day=i/a;
    long week=day/7;
    
    System.out.println("两个日期相隔"+day+"天");
    System.out.println("相隔"+week+"周");
}

4,简述StringBuilder类与string类的区别
String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且浪费大量优先的内存空间
StringBuffer是可变类,和线程安全的字符串操作类,任何对它指向的字符串的操作都不会产生新的对象。每个StringBuffer对象都有一定的缓冲区容量,当字符串大小没有超过容量时,不会分配新的容量,当字符串大小超过容量时,会自动增加容量 
5,计算自己出生了多少天?

public static void main(String[] args) throws ParseException {
//    获取出生日期
    Scanner sc=new Scanner(System.in);
    System.out.println("出生日期,格式:xxxx-xx-xx");
String s=sc.next();
//DateFormat类中方法parse,把字符串的出生日期,解析为Date格式
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
Date birthday=sd.parse(s);
//Dote格式出生日期转换毫秒值
long birthdaydateTime=birthday.getTime();
//当前日期转换毫秒值
long todayTime=new Date().getTime();
//毫秒值转换为天
long time= todayTime-birthdaydateTime;
System.out.println(time/1000/60/60/24);
}

6,求一个for循环 执行时间?

public static void main(String[] args) {
    int a=0;
    int x=10000;
    for (int i = 0; i <x; i++) {
        a=2*x;
    }
    System.out.println(a/10000);
}
原文地址:https://www.cnblogs.com/yinjiaxin/p/13884040.html