Java题目笔记(十四)Date +综合练习
一、时间计算+时间比较
import java.util.Date;
import java.util.Random;public class Main {public static void main(String[] args) {//需求1Date d1=new Date(0L); //从时间原点开始经过了0毫秒long time=d1.getTime();time=time+1000L*60*60*24*365; //一年的时间d1.setTime(time);//修改时间System.out.println(d1);//需求2Random r=new Random();Date d2=new Date(Math.abs(r.nextInt())); //Math.abs 获取绝对值Date d3=new Date(Math.abs(r.nextInt()));System.out.println(d2);System.out.println(d3);long time1=d2.getTime();long time2=d3.getTime();if(time1>=time2){ //long 是基本数据类型,可以直接比较System.out.println("d1 is later");}elseSystem.out.println("d2 is earlier");}
}
二、按照指定格式展示
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.logging.SimpleFormatter;public class Main {public static void main(String[] args) throws ParseException {//解析字符串为Date对象时 指定格式必须与字符串中的时间格式一致SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");String str="2000-11-11";Date str1=sdf1.parse(str);System.out.println(str1); //解析结果 Sat Nov 11 00:00:00 CST 2000}
}
三、秒杀活动
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.logging.SimpleFormatter;public class Main {public static void main(String[] args) throws ParseException {//下单时间:String str1 = "2023年11月11日 0:01:00";String str2 = "2023年11月11日 0:11:0";//秒杀结束时间:String str3 = "2023年11月11日 0:10:0";SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");Date str = sdf1.parse(str3);long last_time = str.getTime();str=sdf1.parse(str1);long time1=str.getTime();str=sdf1.parse(str2);long time2=str.getTime();if(time1>last_time){System.out.println("小贾同学来不急");}else System.out.println("小贾同学来得急");if(time2>last_time){System.out.println("小皮同学来不急");}else System.out.println("小皮同学来得急");}
}
四、集合与包装类综合练习
用集合记录键盘录入的整数,要求当集合内元素之和大于200时停止输入。
import java.util.ArrayList;
import java.util.Scanner;public class Main {public static void main(String[] args) {//用集合记录键盘录入的整数,要求当集合内元素之和大于200时停止输入。ArrayList<Integer> list=new ArrayList<>();Scanner sc=new Scanner(System.in);int sum=0;while(sum<=200){System.out.println("输入整数");String number=sc.nextLine();Integer integer=new Integer(number);int num=Integer.parseInt(number);if(num>100||num<0){System.out.println("请输入0~100的整数");continue;}list.add(integer);sum+=num;}System.out.println("超过200了,总值为"+sum);}
}
五、正则表达式与包装类综合练习
import java.util.Scanner;
public class Main {public static void main(String[] args) {//自己实现ParseInt方法的效果,将字符串形式的数据转换成整数//要求:字符串中只能是数字不能有其他字符//最少一位,最多10位//0不能开头 0:48Scanner sc=new Scanner(System.in);System.out.println("输入整数字符串:");String str=sc.nextLine();String regex="[1-9]\\d{0,9}";int num=0;if(str.matches(regex)){int[] array=new int[str.length()];for(int i=0;i<str.length();i++){char number=str.charAt(i);array[i]=number-48;// num=num*10+array[i];}int index=0;for(int i=array.length-1;i>=0;i--){num+=array[index]*get(i);index++;}System.out.println("输入数字: "+num);}else System.out.println("输入字符串有误");}public static int get(int time){int x=1;while(time>0){x*=10;time--;}return x;}
}
六、ToBinary方法
自己实现ToBinary方法的效果,将一个十进制整数的数据转换成字符串的二进制数据
import java.util.Scanner;
public class Main {public static void main(String[] args) {//自己实现ToBinary方法的效果,将一个十进制整数的数据转换成字符串的二进制数据Scanner sc=new Scanner(System.in);System.out.println("输入十进制整数: ");int number=sc.nextInt();StringBuilder stringBuilder=new StringBuilder();int temp=number;while(temp!=0) {System.out.println("商: "+getShangYu(temp)[0]+" "+"余数:"+getShangYu(temp)[1]);stringBuilder.append(getShangYu(temp)[1]);temp = getShangYu(temp)[0];}System.out.print("逆序: ");StringBuilder result=new StringBuilder();for(int i=stringBuilder.length()-1;i>=0;i--){result.append(stringBuilder.charAt(i));System.out.print(stringBuilder.charAt(i)+" ");}System.out.println();System.out.print("正序: ");for(int i=0;i<stringBuilder.length();i++){System.out.print(stringBuilder.charAt(i)+" ");}System.out.println();String res=result.toString();System.out.println(res);System.out.println(stringBuilder.reverse().toString());}public static int[] getShangYu(int number1){int temp=number1;int count=0;while(temp>=2){count++;temp=temp-2;}int[] arr=new int[2];arr[0]=count; //商arr[1]=temp; //余数return arr;}
}
七、ChronoUnit类使用
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;public class Main {public static void main(String[] args) { //JDK8//算自己活了多少天LocalDate today = LocalDate.now();LocalDate birthday=LocalDate.of(2000,12,12);System.out.println("Today is: " + today);System.out.println(ChronoUnit.DAYS.between(birthday, today));}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class Main {public static void main(String[] args) throws ParseException { //JDK7//算自己活了多少天SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy,MM,dd");String bir="2000,12,12";Date birthday=simpleDateFormat.parse(bir);Date today=new Date();long time1=birthday.getTime();//long time2=today.getTime(); //返回的是微秒long time2=System.currentTimeMillis();long result=(time2-time1)/1000/60/60/24;System.out.println(result);}
}
七、判断闰年
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;public class Main {public static void main(String[] args) throws ParseException { //JDK7//判断一个年份是闰年还是平年//二月有29天是闰年//一年有366天是闰年Scanner sc=new Scanner(System.in);System.out.println("输入年份: ");String year=sc.nextLine();SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");String str1="-02-01";String str2="-03-01";String str3="-01-01";String str4="-12-31";str1=year+str1;str2=year+str2;str3=year+str3;str4=year+str4;Date date1=simpleDateFormat.parse(str1);Date date2=simpleDateFormat.parse(str2);Date date3=simpleDateFormat.parse(str3);Date date4=simpleDateFormat.parse(str4);long time=(date2.getTime()-date1.getTime())/1000/60/60/24;if(time!=29)System.out.println(time+"天,"+year+"不是闰年");elseSystem.out.println(time+"天,"+year+"是闰年");time=((date4.getTime()-date3.getTime())/1000/60/60/24+1);System.out.println(time);if(time!=366){System.out.println(time+"天,"+year+"不是闰年");}else System.out.println(time+"天,"+year+"是闰年");标准方法//通过对三月一日进行减去一天,得到的日期是28 还是 29进行判断Calendar calendar =Calendar.getInstance();Scanner sc=new Scanner(System.in);System.out.println("输入年份: ");int year=sc.nextInt();calendar.set(year,2,1); // Calendar 中 月份对应的索引为 0~11calendar.add(Calendar.DAY_OF_MONTH,-1);int day=calendar.get(Calendar.DAY_OF_MONTH);if(day==29)System.out.println("是闰年");else System.out.println("不是闰年");}
}
import java.text.ParseException;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;public class Main {public static void main(String[] args) throws ParseException { //JDK8//判断一个年份是闰年还是平年//二月有29天是闰年//一年有366天是闰年Scanner sc=new Scanner(System.in);System.out.println("输入年份: ");int year=sc.nextInt();LocalDate localDate=LocalDate.of(year,2,1);LocalDate localDate2=LocalDate.of(year,3,1);LocalDate localDate3=LocalDate.of(year,1,1);LocalDate localDate4=LocalDate.of(year+1,1,1);long day=ChronoUnit.DAYS.between(localDate,localDate2);if(day!=29)System.out.println(day+"天,"+year+"不是闰年");else System.out.println(day+"天,"+year+"是闰年");day=ChronoUnit.DAYS.between(localDate3,localDate4);if(day!=366)System.out.println(day+"天,"+year+"不是闰年");else System.out.println(day+"天,"+year+"是闰年");标准方法同JDK7LocalDate localDate5=LocalDate.of(year,3,1);LocalDate localDate6=localDate5.minusDays(1);day=localDate6.getDayOfMonth();System.out.println(day); //输入2024 day=29}
}