日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java面试知识点:Date类、异常

發布時間:2024/7/5 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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类、异常的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。