问题:Java面试知识点:Date类、异常
答案:
1.Date类
代码如下:
(1)创建日期:
package com.xy;import java.util.Date;/*** @ProjectName: day01* @Package: com.xy* @ClassName: test01* @Author: 杨路恒* @Description:* @Date: 2021/8/17 0017 9:55* @Version: 1.0*/
public class test01 {public static void main(String[] args) {Date date=new Date(); //当前电脑中的时间System.out.println(date);Date date1=new Date(0L); //从计算机的时间原点开始,过了指定毫秒的那个时间System.out.println(date1);long l = System.currentTimeMillis();long time = date.getTime();System.out.println(l);System.out.println(time);}
}
(2)日期格式化:
package com.xy;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;/*** @ProjectName: day01* @Package: com.xy* @ClassName: test02* @Author: 杨路恒* @Description:* @Date: 2021/8/17 0017 10:12* @Version: 1.0*/
public class test02 {public static void main(String[] args) throws ParseException {Date date=new Date();SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss ");String format = simpleDateFormat.format(date);System.out.println(format);String s="2021-8-17";SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd");Date parse = simpleDateFormat1.parse(s);System.out.println(parse);}
}package com.xy;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;/*** @ProjectName: day01* @Package: com.xy* @ClassName: test03* @Author: 杨路恒* @Description:* @Date: 2021/8/17 0017 10:19* @Version: 1.0*/
public class test03 {public static void main(String[] args) throws ParseException {String start="2020年11月11日 0:0:0";String end="2020年11月11日 0:10:0";String s="2020年11月11日 0:03:47";String s1="2020年11月11日 0:10:11";SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");Date date = simpleDateFormat.parse(s);Date date1 = simpleDateFormat.parse(s1);Date parse = simpleDateFormat.parse(start);Date parse1 = simpleDateFormat.parse(end);if (date.getTime()>=parse.getTime()&&date.getTime()<=parse1.getTime()){System.out.println("第一位参与");}else {System.out.println("第一位没参与");}if (date1.getTime()>=parse.getTime()&&date1.getTime()<=parse1.getTime()){System.out.println("第二位参与");}else {System.out.println("第二位没参与");}}
}
(3)日期常用操作:
package com.xy;import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;/*** @ProjectName: day01* @Package: com.xy* @ClassName: day04* @Author: 杨路恒* @Description:* @Date: 2021/8/17 0017 10:31* @Version: 1.0*/
public class test04 {public static void main(String[] args) {DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");String s="2021年08月17日 16:21:00";LocalDateTime localDateTime = LocalDateTime.parse(s, dateTimeFormatter);LocalDateTime localDateTime1 = localDateTime.plusDays(1);String s1 = localDateTime.format(dateTimeFormatter);System.out.println(s1);LocalDateTime now = LocalDateTime.now();System.out.println(now);LocalDateTime localDateTime2 = LocalDateTime.of(2021, 8, 17, 16, 26);System.out.println(localDateTime2);System.out.println(now.getYear()); //获取年System.out.println(now.getMonth()); //获取月System.out.println(now.getDayOfMonth()); //获取日System.out.println(now.getDayOfWeek());System.out.println(now.getDayOfYear());System.out.println(now.getHour());System.out.println(now.getMinute());System.out.println(now.minusYears(1));System.out.println(now.withYear(2021));Period between = Period.between(localDateTime2.toLocalDate(),now.toLocalDate());System.out.println(between);System.out.println(between.getYears());System.out.println(between.getMonths());System.out.println(between.getDays());System.out.println(between.toTotalMonths());Duration between1 = Duration.between(now, localDateTime2);System.out.println(between1.getSeconds());System.out.println(between1.getNano());System.out.println(between1.toMillis());}
}
2.异常
代码如下:
(1)Throws
package com.xy;/*** @ProjectName: day01* @Package: com.xy* @ClassName: test05* @Author: 杨路恒* @Description:* @Date: 2021/8/17 0017 17:14* @Version: 1.0*/
public class test05 {public static void main(String[] args) throws Exception {try {System.out.println(1/0);} catch (Exception e) {e.printStackTrace();}throw new Exception("异常");}
}
(2)Throw
package com.xy;import java.util.Scanner;/*** @ProjectName: day01* @Package: com.xy* @ClassName: test06* @Author: 杨路恒* @Description:* @Date: 2021/8/17 0017 20:20* @Version: 1.0*/
public class test06 {public static void main(String[] args) {try {Scanner in=new Scanner(System.in);String s = in.nextLine();int i = Integer.parseInt(s);System.out.println(i);System.out.println("输出");System.out.println(1/0);} catch (NumberFormatException e) {System.out.println("执行");e.getMessage();String s = e.toString();System.out.println(s);}catch (ArithmeticException e){System.out.println("执行");e.printStackTrace();}}
}class AgeOutOfBoundException extends NumberFormatException{public AgeOutOfBoundException() {super();}
}
(3)异常