java代码------常用工具类(尚马day14作业)Week4(5)
2022/3/1 20:23:02
本文主要是介绍java代码------常用工具类(尚马day14作业)Week4(5),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
(1)
/** * @author: sunshine * @description: * @data: 2022/2/28 20:56 * @version: 0.1 * @since: jdk11 */ //1.将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss public class Exercise1 { public static void main(String[] args) { demo1(); //(jdk1.8之前) demo2(); //(jdk1.8之后) } private static void demo2() { LocalDateTime localDateTime = LocalDateTime.of(2022,Calendar.FEBRUARY,1,12,0,0); DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN); String format = formatter.format(localDateTime); System.out.println(format); //String format1 = localDateTime.format(formatter); } private static final String PATTERN = "yyyy-MM-dd HH:mm:ss"; private static void demo1() { //Date转String //1.创建Date对象 Date date = new Date(); //获得系统当前时间 long time = date.getTime(); //获得毫秒数(不推荐) long millis = System.currentTimeMillis(); //推荐 //创建Calender对象 Calendar calendar = Calendar.getInstance(); calendar.set(2022,Calendar.FEBRUARY,1,12,0,0); //2022-2-1 12:00:00 //Calender转Date Date date1 = calendar.getTime(); //2.创建格式类DateFormat DateFormat dateFormat = new SimpleDateFormat(PATTERN); String dateStr = dateFormat.format(date); System.out.println(dateStr); } }
(2)
/** * @author: sunshine * @description: * @data: 2022/2/28 21:22 * @version: 0.1 * @since: jdk11 */ //2.使用Calendar类的相关属性以及方法!打印出某年某个月的日历信息!(要求年月日由命令行输入) public class Exercise2 { public static void main(String[] args) { //demo3(); demo4(); } private static void demo4() { //动态月份的情况 int year = 2022; Scanner input = new Scanner(System.in); System.out.println("请录入月份:"); int month = input.nextInt(); Calendar calendar = Calendar.getInstance(); calendar.set(year,month-1,1); System.out.println("日\t一\t二\t三\t四\t五\t六"); int totalDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); //判断1号有几个空格 int day = calendar.get(Calendar.DAY_OF_WEEK); for (int i = 1; i < day; i++) { System.out.print("\t"); } for (int i = 1; i <= totalDay; i++) { System.out.print(i+"\t"); //判断当前天是否为一周最后一天,是则换行 calendar.set(Calendar.DAY_OF_MONTH,i); if (calendar.get(Calendar.DAY_OF_WEEK)==7){ System.out.println(); } } } private static void demo3() { //固定月份的情况 int year = 2022; int month = 2; Calendar calendar = Calendar.getInstance(); calendar.set(year,month-1,1); //2月份的序号是1 System.out.println("日\t一\t二\t三\t四\t五\t六"); int totalDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); System.out.print("\t\t"); for (int i = 1; i <= totalDay; i++) { System.out.print(i+"\t"); //判断当前天是否为一周最后一天,是则换行 calendar.set(Calendar.DAY_OF_MONTH,i); if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){ System.out.println(); } } } }
(3)
//4.计算两个时间相差几个小时 public class Exercise4 { public static void main(String[] args) { //demo1(); demo2(); } private static void demo2() { //LocalDateTime 与 LocalDateTime LocalDateTime dateTime1 = LocalDateTime.of(2022,Calendar.FEBRUARY,1,12,0,0); LocalDateTime dateTime2 = LocalDateTime.now(); long hours = dateTime1.until(dateTime2, ChronoUnit.HOURS); //括号里的减括号外面的 System.out.println(hours); } private static void demo1() { //Date 与 Date Calendar calendar = Calendar.getInstance(); calendar.set(2022,Calendar.FEBRUARY,1,12,0,0); //2022-2-1 12:00:00 Date date1 = calendar.getTime(); Date date2 = new Date(); long result = Math.abs((date1.getTime()-date2.getTime())/1000/3600); System.out.println(result); } }
(4)
/** * @author: sunshine * @description: * @data: 2022/2/28 21:24 * @version: 0.1 * @since: jdk11 */ //5.一个方法,要求传入时间和间隔天数,返回新的日期 public class Exercise5 { public static void main(String[] args) { demo1(new Date(),10); //Date之间的操作 demo2(LocalDate.now(),10); //LocalDate之间的操作 } private static LocalDate demo2(LocalDate localDate,int days) { return localDate.plusDays(days); } private static Date demo1(Date date,int days) { //传入时间加时间间隔 //用毫秒数来写 long time = date.getTime() + Duration.ofDays(days).toMillis(); //加号后面的是天转毫秒 return new Date(time); } }
(5)
/** * @author: sunshine * @description: * @data: 2022/2/28 21:24 * @version: 0.1 * @since: jdk11 */ //6.要求写一个工具类,可以自定义获取N个随机数字或字母(0~9,a~z,A~Z混合一起) public class Exercise6 { public static void main(String[] args) { demo1(); //demo2(); } private static void demo2() { StringBuilder builder = new StringBuilder(); ThreadLocalRandom random = ThreadLocalRandom.current(); int choice = random.nextInt(0,3); for (int i = 1; i <= 5; i++) { char ch = ' '; switch (choice) { case 0: ch = (char)random.nextInt(48,57); break; case 1: ch = (char)random.nextInt(68,90); break; case 2: ch = (char)random.nextInt(97,122); break; } builder.append(ch); } System.out.println(builder.toString()); } private static void demo1() { //48-57 0-9 A-Z 65-90 a-z 97-122 ThreadLocalRandom random = ThreadLocalRandom.current(); StringBuilder builder = new StringBuilder(); int count = 5; for(int i=1;i<=count;i++){ int num = random.nextInt(48,122); if(num>=48 && num<=57){ num = num -48; //直接变成对应数字 builder.append(num); }else if( (num>=65 && num<=90) || (num>=97 && num<=122) ){ char ch = (char)num; //数字转字符 builder.append(ch); }else { i--; } } System.out.println(builder.toString()); } }
这篇关于java代码------常用工具类(尚马day14作业)Week4(5)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南