cy
2022-06-27 150ced737ad5d16d476df143c7e4238fc6b8b998
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package cn.ksource.web.util;
 
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;
 
import org.apache.commons.lang.StringUtils;
 
import cn.ksource.core.util.ConvertUtil;
import cn.ksource.core.util.DateUtil;
import cn.ksource.core.util.StringUtil;
 
public class DateParepareUtil {
    /**
     * 得到起始时间的当天的开始是时间  如:1014-01-01 00:00:00
     * @param dateStr
     * @return
     */
    public static String getBeginDate(String dateStr){
        return dateStr.trim()+" 00:00:00";
    }
    /**
     * 得到起始时间的当天的结束是时间  如:1014-01-01 23:29:59
     * @param dateStr
     * @return
     */
    public static String getEndDate(String dateStr){
        return dateStr.trim()+" 23:59:59";
    }
    
    
    
    /**
     * 返回当前时间字符串
     * @param format,日期格式化字符串 如“yyyy-MM-dd”
     * @return 当前日期字符串
     */
    public static String getToday(String str) {
        SimpleDateFormat format = new SimpleDateFormat(str);//设置日期格式
        String nowTime = format.format(new Date());
        return nowTime;
    }
    
    
    
    /**
     * 功能描述:返回给定日期加上多少天之后的时间<BR>
     * @param from   yyyyMMdd
     * @param day
     * @param length
     * @return
     * @author:杨凯<BR>
     * 时间:Apr 18, 2009 11:56:16 AM<BR>
     */
    public static String getDateAdd(String from,int day){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(from));
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.add(Calendar.DAY_OF_MONTH, day);
        System.out.println(calendar.getTimeInMillis());
        String date = sdf.format(calendar.getTime());
        return date;
    }
    
    
    /**
     * 通过年,月获得该月的第一天和最后一天  如果year和month参数都为空  默认为当前年月
     * @param year 格式 yyyy 
     * @param month  格式 MM
     * @return  数组第一个当前月第一天, 数组第二个:当前月最后一天
     */
    public static String[] queryFirstLastDate(String year,String month) {
        String[] str = new String[2];
        if(StringUtil.isEmpty(year) || !StringUtils.isNumeric(year)||year.length()!=4||Integer.valueOf(year)<0) {
            year = DateUtil.format(new Date(), "yyyy");
        } 
        if(StringUtil.isEmpty(month)|| !StringUtils.isNumeric(month)||Integer.valueOf(month)<1||Integer.valueOf(month)>12) {
            month = DateUtil.format(new Date(), "MM");
        }
        if(month.length()!=2) {
            month = "0"+month;
        }
        String firstDate = year+"-"+month+"-01";
        str[0] = firstDate;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = dateFormat.parse(firstDate);
            String lastDate = dateFormat.format(lastDayOfMonth(date));
            str[1] = lastDate;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return str;
    }
    
    
    /*
     * 获得指定日期所在月最后一天
     */
     public static Date lastDayOfMonth(Date date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.roll(Calendar.DAY_OF_MONTH, -1);
            return cal.getTime();
     }
    
     
     /**
      * 获取当前时间的以前的日期或者以后的日期
      * @param format  返回日期格式
      * @param num  如 -1: 前一个月    1:后一个月
      * @return
      */
     public static String getPrevNextMonth(String format,int num){
            String str = "";
            SimpleDateFormat sdf=new SimpleDateFormat(format);
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MONTH, num);
            cal.set(Calendar.DATE, 1);//把日期设置为当月第一天
            return sdf.format(cal.getTime());
     }
     
         /**
         * 功能描述:字母判断
         * 
         * @param str
         * @return
         */
        public static boolean isZimu(String str) {
            if (str == null || "".equals(str))
                return false;
            Pattern pattern = Pattern.compile("[a-zA-Z]*");
            return pattern.matcher(str).matches();
        }
     
     
     
     public static void main(String[] args) {
            System.out.println(isZimu("dddd2"));
        }
     
     /**
      * 获得指定日期所有月的最有一天
      */
     public static String lastDayMonth(String date,String inputFormat,String outputFormat) {
         SimpleDateFormat format = new SimpleDateFormat(inputFormat);
         try {
            Date date2 = format.parse(date);
            SimpleDateFormat dateFormat = new SimpleDateFormat(outputFormat);
            String lastDate = dateFormat.format(lastDayOfMonth(date2));
            System.out.println(lastDate);
            return lastDate;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
         
     }
}