zoukankan      html  css  js  c++  java
  • 计算天数差-常用日期函数

    long days = mss / ( 60 * 60 * 24);

    long hours = (3601 % ( 60 * 60 * 24)) / (60 * 60);

    long minutes = (3601 % ( 60 * 60)) /60;
    long seconds = 3601 % 60;
    System.out.println(hours+" "+minutes+" "+seconds);1 0 1

    SimpleDateFormat

    dateUtil

    dateformatUtil

    localdatetime

    calendar

    LocalDate          

    System.out.println(LocalDateTime.of(LocalDate.now(), LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    System.out.println(LocalDateTime.of(LocalDate.now(), LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    System.out.println(LocalDateTime.of(LocalDate.now(), LocalTime.NOON).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

    2021-12-08 00:00:00
    2021-12-08 23:59:59
    2021-12-08 12:00:00

    LocalDate dateSimple = LocalDate.parse("20180729", DateTimeFormatter.BASIC_ISO_DATE);
    LocalDate localDate = LocalDate.parse("19570323", DateTimeFormatter.ofPattern("yyyyMMdd"));
    System.out.println(localDate);

    public static double dateDiffDays(String startTime, String endTime,
    String format, String str) {
    // 按照传入的格式生成一个simpledateformate对象
    SimpleDateFormat sd = new SimpleDateFormat(format);
    long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
    long nh = 1000 * 60 * 60;// 一小时的毫秒数
    long nm = 1000 * 60;// 一分钟的毫秒数
    long ns = 1000;// 一秒钟的毫秒数
    long diff;
    long day = 0;
    long hour = 0;
    long min = 0;
    long sec = 0;
    // 获得两个时间的毫秒时间差异
    try {
    diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
    day = diff / nd;// 计算差多少天
    hour = diff % nd / nh + day * 24;// 计算差多少小时
    min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟
    sec = diff % nd % nh % nm / ns;// 计算差多少秒
    // 输出结果
    System.out.println("时间相差:" + day + "天" + (hour - day * 24) + "小时"
    + (min - day * 24 * 60) + "分钟" + sec + "秒。");
    System.out.println("hour=" + hour + ",min=" + min);
    if((hour - day * 24)<5){
    // 半天
    return day+0.5;
    }else{
    // 整天
    return day+1;
    }
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return 0;
    }

     //获取时间差把判断换成这部分

    double h= Double.valueOf((hour - day * 24)+"."+(min - day * 24 * 60));
    return new DecimalFormat("0.00").format(h);

     /**/计算一天的时间差 只能是一天的不然错

    public static String getHours(String end1,String begin1)throws ParseException {
    SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String end="2019-06-21 8:30:00";//sim.format(dd);
    //2.定义一个比较的时间
    String begin="2019-06-21 8:36:00";
    //把string类型转换为long类型的
    long st=sim.parse(end1).getTime();
    long en=sim.parse(begin1).getTime();
    //1秒:1000 1分钟:60000;1小时:3600000;1天为86400000
    //计算天数
    int day=(int) ((en-st)/86400000);
    //计算小时
    int h=(int) (((en-st)%86400000)/3600000);
    //计算分钟
    int m=(int)(((en-st)%86400000)%3600000)/60000;
    //计算秒
    int s=(int)((((en-st)%86400000)%3600000)%60000)/1000;
    String hour = String.valueOf(h);
    boolean hou = hour.contains("-");
    if(hou==true){
    hour=hour.replace("-", "");
    }

    String min = String.valueOf(m);
    boolean mi = min.contains("-");
    if(mi==true){
    min=min.replace("-", "");
    }
    String result=new DecimalFormat("0.00").format(Double.valueOf(hour+"."+min));
    return result;
    }

    ——------------------------------------js

    function getRemainderTime (startdate,enddate){
    //console.log("startdate is "+ startdate+";enddate is "+ enddate);
    var s1 = new Date(startdate);
    var s2 = new Date(enddate);
    runTime = parseInt((s2.getTime() - s1.getTime()) / 1000);
    var year = Math.floor(runTime / 86400 / 365);
    runTime = runTime % (86400 * 365);
    var month = Math.floor(runTime / 86400 / 30);
    runTime = runTime % (86400 * 30);
    var day = Math.floor(runTime / 86400);
    runTime = runTime % 86400;
    var hour = Math.floor(runTime / 3600);
    runTime = runTime % 3600;
    var minute = Math.floor(runTime / 60);
    runTime = runTime % 60;
    var second = runTime;
    console.log(year,month,day,hour,minute,second);
      // return year+','+month+','+day+','+hour+','+minute+','+second;
    if(hour<5){
    // 半天
    return day+0.5;
    }else{
    // 整天
    return day+1;
    }
    }

    public static void main(String[] args) {
    try {
    String strDateStart = "2019-08-01";
    String strDateEnd = "2019-08-31";

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date_start = sdf.parse(strDateStart);
    Date date_end = sdf.parse(strDateEnd);
    WorkDayUtils app = new WorkDayUtils();
    Calendar cal_start = Calendar.getInstance();
    Calendar cal_end = Calendar.getInstance();
    cal_start.setTime(date_start);
    cal_end.setTime(date_end);
    System.out.println("开始日:" + cal_start.get(Calendar.YEAR) + "-" + (cal_start.get(Calendar.MONTH) + 1)
    + "-" + cal_start.get(Calendar.DAY_OF_MONTH) + " " + app.getChineseWeek(cal_start));
    System.out.println("结束日:" + cal_end.get(Calendar.YEAR) + "-" + (cal_end.get(Calendar.MONTH) + 1)
    + "-" + cal_end.get(Calendar.DAY_OF_MONTH) + " " + app.getChineseWeek(cal_end));
    System.out.println("工作日:" + app.getWorkingDay(cal_start, cal_end));
    System.out.println("休息日:" + app.getHolidays(cal_start, cal_end));
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    /**
    * 获取日期之间的天数
    * @param d1
    * @param d2
    * @return
    */
    public int getDaysBetween(Calendar d1, Calendar d2) {
    if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
    Calendar swap = d1;
    d1 = d2;
    d2 = swap;
    }
    int days = d2.get(Calendar.DAY_OF_YEAR)
    - d1.get(Calendar.DAY_OF_YEAR);
    int y2 = d2.get(Calendar.YEAR);
    if (d1.get(Calendar.YEAR) != y2) {
    d1 = (Calendar) d1.clone();
    do {
    days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
    d1.add(Calendar.YEAR, 1);
    } while (d1.get(Calendar.YEAR) != y2);
    }
    return days;
    }

    /**
    * 获取工作日
    * @param d1
    * @param d2
    * @return
    */
    public int getWorkingDay(Calendar d1, Calendar d2) {
    int result = -1;
    if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
    Calendar swap = d1;
    d1 = d2;
    d2 = swap;
    }
    // int betweendays = getDaysBetween(d1, d2);
    // int charge_date = 0;
    int charge_start_date = 0;// 开始日期的日期偏移量
    int charge_end_date = 0;// 结束日期的日期偏移量
    // 日期不在同一个日期内
    int stmp;
    int etmp;
    stmp = 7 - d1.get(Calendar.DAY_OF_WEEK);
    etmp = 7 - d2.get(Calendar.DAY_OF_WEEK);
    if (stmp != 0 && stmp != 6) {// 开始日期为星期六和星期日时偏移量为0
    charge_start_date = stmp - 1;
    }
    if (etmp != 0 && etmp != 6) {// 结束日期为星期六和星期日时偏移量为0
    charge_end_date = etmp - 1;
    }
    // }
    result = (getDaysBetween(this.getNextMonday(d1), this.getNextMonday(d2)) / 7)
    * 5 + charge_start_date - charge_end_date;
    // System.out.println("charge_start_date>" + charge_start_date);
    // System.out.println("charge_end_date>" + charge_end_date);
    // System.out.println("between day is-->" + betweendays);
    return result;
    }

    /**
    * 获取中文日期
    * @param date
    * @return
    */
    public String getChineseWeek(Calendar date) {
    final String[] dayNames = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);
    // System.out.println(dayNames[dayOfWeek - 1]);
    return dayNames[dayOfWeek - 1];
    }

    /**
    * 获得日期的下一个星期一的日期
    * @param date
    * @return
    */
    public Calendar getNextMonday(Calendar date) {
    Calendar result = null;
    result = date;
    do {
    result = (Calendar) result.clone();
    result.add(Calendar.DATE, 1);
    } while (result.get(Calendar.DAY_OF_WEEK) != 2);
    return result;
    }

    /**
    * 获取休息日
    * @param d1
    * @param d2
    * @return
    */
    public int getHolidays(Calendar d1, Calendar d2) {
    return this.getDaysBetween(d1, d2) - this.getWorkingDay(d1, d2);
    }

  • 相关阅读:
    LeetCode 109 Convert Sorted List to Binary Search Tree
    LeetCode 108 Convert Sorted Array to Binary Search Tree
    LeetCode 107. Binary Tree Level Order Traversal II
    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode 103 Binary Tree Zigzag Level Order Traversal
    LeetCode 102. Binary Tree Level Order Traversal
    LeetCode 104. Maximum Depth of Binary Tree
    接口和多态性
    C# 编码规范
  • 原文地址:https://www.cnblogs.com/java-llp/p/10980487.html
Copyright © 2011-2022 走看看