zoukankan      html  css  js  c++  java
  • java 获取指定日前的前一天

      /**
         * @Author:
         * @Description:更加输入日期,获取输入日期的前一天
         * @Date:
         * @strData:参数格式:yyyy-MM-dd
         * @return:返回格式:yyyy-MM-dd
         */
        public static String getPreDateByDate(String strData) {
            String preDate = "";
             Calendar c = Calendar.getInstance();
             SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
             Date date = null;
             try {
                 date = sdf.parse(strData);
             } catch (java.text.ParseException e) {
                 e.printStackTrace();
             }
             
             c.setTime(date);
             int day1 = c.get(Calendar.DATE);
             c.set(Calendar.DATE, day1 - 1);
             preDate = sdf.format(c.getTime());
            return preDate;
        }
        
     import java.util.Calendar;
    import java.util.Date;
    import java.util.List;
    import java.text.SimpleDateFormat;
    
    //获取从2018-02-05到2018-10-15号之间所有的周一到周日的日期
        public static void getWeekdays(){
            String monitorDate = "2018-02-";
            String startDate = "";
            String endDate = "";
            String dateRange = "";
            int mon = 2;
            for(int i=5;i<=260;) {
                if (endDate.length() == 0) {
                    startDate = monitorDate + i;
                } else{
                    startDate = getSixthDateByDate(endDate,1);
                }
                endDate = getSixthDateByDate(startDate,6);
                System.out.println(startDate+": "+endDate);
                dateRange = startDate + endDate;
                dateRange= dateRange.replace("-","");
                System.out.println(dateRange);
                System.out.println("
    ");
                i+=7;
            }
        }
        /**
         * @Author:
         * @Description:更加输入日期,获取输入日期后的第一天
         * @startData:参数格式:yyyy-MM-dd
         * @day:day=1,获取输入日期的第一天;day=2,获取输入日期的第二天;day=n,获取输入日期的第n天
         * @return:返回格式:yyyy-MM-dd
         */
        public static String getSixthDateByDate(String startData,int day) {
            String preDate = "";
            Calendar c = Calendar.getInstance();
            SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                date = sdf.parse(startData);
            } catch (java.text.ParseException e) {
                e.printStackTrace();
            }
    
            c.setTime(date);
            int day1 = c.get(Calendar.DATE);
            //c.set(Calendar.DATE, day1 +6);获取输入日期后的第一天
            c.set(Calendar.DATE, day1 +day);
            preDate = sdf.format(c.getTime());
            return preDate;
        }

      //获取今天是星期几. 1:周一;2:周二;3:周三....以此类推
        public static int getCurrentDayofweek() {
            //今天是几号 
            Date date=new Date(); 
            Calendar c=Calendar.getInstance(); 
            c.setTime(date); 
            //今天是这个星期的第几天 
            int week=c.get(Calendar.DAY_OF_WEEK)-1; 
            return week;
        }
  • 相关阅读:
    如何使用反射技术获取泛型类的真实类型?
    applicationContext.xml文件如何调用外部properties等配置文件
    applicationContext.xml中的使用${}是代表什么意思?
    net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
    MySql中LongText字段对应Hibernate映射文件的设置(转)
    ckeditor的详细配置(转)
    XML-学习
    WSDL-学习总结
    ONVIF-WSDL
    sourceinsight相关配置
  • 原文地址:https://www.cnblogs.com/shaosks/p/9663274.html
Copyright © 2011-2022 走看看