java初学小练习1——日期类

    技术2022-07-10  105

    import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class Date_test { public static void main(String[] args) throws ParseException { /** * 1.获取当前的日期,并把这个日期转换为指定格式的字符串 * 如2088-08-08 08:08:08 */ System.out.println(new Date()); SimpleDateFormat f1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(f1.format(new Date())); System.out.println("----------------------------------------------------"); /** * 2.把2020-03-04 * 转换为2020年03月04日 */ String s2 = "2020-03-04"; //给定一个字符串日期 //创建SimpleDateFormat对象sdf1,指定日期模式为yyyy-MM-dd SimpleDateFormat f21 = new SimpleDateFormat("yyyy-MM-dd"); Date date2; try { date2 = f21.parse(s2); //字符串转成date对象类型 SimpleDateFormat f22 = new SimpleDateFormat("yyyy年MM月dd日"); String s22 = f22.format(date2); //date类型转换成字符串 System.out.println(s22); } catch (ParseException e) { e.printStackTrace(); } System.out.println("----------------------------------------------------"); /** * 3.判断2018年2月14日是星期几 */ String s3 = "2018年2月14日"; SimpleDateFormat f31=new SimpleDateFormat("yyyy年MM月dd日"); Date date3 = f31.parse(s3); //字符串转成date对象类型 Calendar c=Calendar.getInstance(); //获得日历 c.setTime(date3); //获得指定时间的日历 int date32 = c.get(Calendar.DAY_OF_WEEK); day(date32); System.out.println("-----------------------------------------------------"); Scanner sc = new Scanner(System.in); System.out.println("请输入年:"); int year = sc.nextInt(); System.out.println("请输入月: "); int month = sc.nextInt(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); Date startData = df.parse("2020-02-01"); //设置起始时间 cal.set(Calendar.YEAR, year); //将年,月设置为用户输入的时间 cal.set(Calendar.MONTH, month-1); cal.set(Calendar.DAY_OF_MONTH, 1); //设置当前月数的第一天 int weekStart = cal.get(Calendar.DAY_OF_WEEK); //获得每周开始的时间 System.out.println("日\t一\t二\t三\t四\t五\t六"); for (int i=1;i<weekStart;i++){ System.out.printf("%-8s",""); //在变量右边缩进8个字符 } int countRelax = 0; int countWeekend = 0; int maxDate = cal.getActualMaximum(Calendar.DATE);//让maxDate获取本月的最大天数 for (int i=1;i<= maxDate;i++){ cal.set(Calendar.DAY_OF_MONTH, i); //设置每月当天的天数, Date temp = cal.getTime(); //获取用户输入的年,月 int gap = getDifferentDay(startData, temp); //获得本月的总天数。 if (gap%4==1){ //上三天休息一天,每循环一次休息天数+1 System.out.printf("%-8s","["+cal.get(Calendar.DAY_OF_MONTH)+"]"); countRelax++; if (cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){ countWeekend++;//判定休息天数是否在周末内,如果是周末天数+1 } }else { System.out.printf("%-8s", cal.get(Calendar.DAY_OF_MONTH)); } if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){ System.out.println(); } cal.add(Calendar.DAY_OF_MONTH,1); } System.out.println("本月休息的天数有:"+countRelax); System.out.println("本月轮到周末休息的天数有:"+countWeekend); } private static int getDifferentDay(Date from, Date to){//该方法用于获得本月的总天数 long fromTime = from.getTime(); long toTime = to.getTime(); int days = (int) ((toTime - fromTime) / (1000 * 60 * 60 * 24)); return days; } public static void day(int i) { switch (i) { case 1: System.out.println("星期天"); break; case 2: System.out.println("星期一"); break; case 3: System.out.println("星期二"); break; case 4: System.out.println("星期三"); break; case 5: System.out.println("星期四"); break; case 6: System.out.println("星期五"); break; case 7: System.out.println("星期六"); break; } } }
    Processed: 0.011, SQL: 9