Java面试知识点:Date类、异常
生活随笔
收集整理的這篇文章主要介紹了
Java面试知识点:Date类、异常
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題: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)異常
?
總結
以上是生活随笔為你收集整理的Java面试知识点:Date类、异常的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL 与 ORACLE 的比较
- 下一篇: JavaWeb笔记:Html总结