xinyb
10 天以前 658898d28cded745ca15ee0a89e3025358356259
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.yc.utils;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
 * @author 沈奕洲
 * 2011-11-16
 */
public class DateChange {
    
    private static String strDateSeparator = "-"; //日期分隔符
    
    /**
     * 格式化日期
     * @param strDate 字符串的日期格式
     * @return 如果格式正确,则返回正确的日期;否则,返回""。
     */
    public static String formatDate(String strDate)
    {
        String result = "";
        Date date = null;
        try
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy"+strDateSeparator+"MM"+strDateSeparator+"dd");
            date = sdf.parse(strDate);
            result =  formatDate(date);
        }
        catch(Exception e)
        {
            //System.out.println("date:" + e.getMessage());
        }
        return result;
    }
 
    /**
     * 格式化日期
     *
     * @param date Date
     * @return String
     */
    public static String formatDate(Date date)
    {
        return formatDateByFormat(date,"yyyy"+strDateSeparator+"MM"+strDateSeparator+"dd");
    }
 
    /**
     * 格式化今天日期
     * @return
     */
    public static String formatDate()
    {
        Date dToday = new Date();
        return formatDate(dToday);
    }
 
    public static String formatDateTime(Date date)
    {
        return formatDateByFormat(date,"yyyy"+strDateSeparator+"MM"+strDateSeparator+"dd HH:mm:ss");
    }
    
    public static String formatShortDateTime(Date date)
    {
        return formatDateByFormat(date,"yyyy"+strDateSeparator+"MM"+strDateSeparator+"dd");
    }
 
    /**
     * 以指定的格式来格式化日期
     *
     * @param date Date
     * @param format String
     * @return String
     */
    public static String formatDateByFormat(Date date,String format)
    {
        String result = "";
        if(date != null)
        {
            try
            {
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                result = sdf.format(date);
            }
            catch(Exception ex)
            {
                //System.out.println("date:" + date);
                ex.printStackTrace();
            }
        }
        return result;
    }
 
 
    /**
     * 紧缩格式化日期
     *
     * @param date Date
     * @return String
     */
    public static String formatDateSN(Date date)
    {
        return formatDateByFormat(date,"yyyyMMdd");
    }
 
    /**
     * 取今天的紧缩格式化日期
     * @return 八位yyyyMMdd格式的字符
     */
    public static String formatDateSN()
    {
        Date dToday = new Date();
        return formatDateSN(dToday);
    }
 
    /**
     * 根据字符串解析日期
     *
     * @param strDate String
     * @param division String
     * @return Date
     */
    public static Date parseFormatDateTime(String strDate)
    {
        Date date = null;
        try
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy"+strDateSeparator+"MM"+strDateSeparator+"dd HH:mm:ss");
            date = sdf.parse(strDate);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return date;
    }
    
    /**
     * 根据字符串解析日期
     *
     * @param strDate String
     * @param division String
     * @return Date
     */
    public static String parseFormatDateTime_str(String strDate)
    {
        Date date = null;
        String sDate="";
        try
        {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy"+strDateSeparator+"MM"+strDateSeparator+"dd HH:mm:ss");
            date = sdf.parse(strDate);
            sDate=sdf1.format(date);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return sDate;
    }
 
    /**
     * 日期变换函数
     * @param dateformat 日期格式字符串:2005-09-13 14:17:06
     * @param iDay 日期天数 3
     * @return 2005-09-16 14:17:06
     */
    public static String GetDate(String dateformat,int iDay)
    {
        String strOut="";
        Date date = parseFormatDateTime(dateformat);
        if(date!=null){
            GregorianCalendar worldTour = new GregorianCalendar(date.getYear()+1900,date.getMonth(),date.getDate());
            worldTour.add(Calendar.DATE,iDay);
            Date d = worldTour.getTime();
            strOut = formatDateByFormat(d,"yyyy"+strDateSeparator+"MM"+strDateSeparator+"dd")+" "+"00:00:00";
                    //formatDateByFormat(date,"HH:mm:ss");
        }
        return strOut;
    }
    
    
}