java 时间日期转换


  1 import java.text.SimpleDateFormat;
  2 import java.util.ArrayList;
  3 import java.util.Calendar;
  4 import java.util.Date;
  5 import java.util.List;
  6 
  7 /**时间  日期转换类
  8  * @author DELL
  9  *
 10  */
 11 public class UtilsData {
 12     
 13     
 14     /**
 15      * 通用类
 16      * 
 17      * @author root
 18      */
 19     
 20         private static SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
 21         private static SimpleDateFormat datetime = new SimpleDateFormat(
 22                 "yyyy-MM-dd HH:mm:ss");
 23         public static Date getNowDate(){
 24             return new Date();
 25         }
 26         // ****************************当前时间相关****************************
 27         /**
 28          * 获得以 yyyy-MM-dd 为形式的当前时间字符串
 29          * 
 30          * @return String
 31          */
 32         public static String getCurrentTimeByDay() {
 33             String time = date.format(new Date(System.currentTimeMillis()));
 34             return time;
 35         }
 36 
 37         /**
 38          * 获得以 yyyy-MM-dd HH:mm:ss 为形式的当前时间字符串
 39          * 
 40          * @return String
 41          */
 42         public static String getCurrentTimeBySecond() {
 43             String time = datetime.format(new Date(System.currentTimeMillis()));
 44             return time;
 45         }
 46 
 47         /**
 48          * 获得给定格式的当前时间字符串
 49          * 
 50          * @param give
 51          *            String 给定的时间格式
 52          * @return String
 53          */
 54         public static String getCurrentTime(String give) {
 55             SimpleDateFormat temp = new SimpleDateFormat(give);
 56             return temp.format(new Date(System.currentTimeMillis()));
 57         }
 58 
 59         // ****************************String转换为Date****************************
 60         /**
 61          * 将String转化成date
 62          * 
 63          * @throws ParseException
 64          * */
 65         public static Date pStringToDate(String str, String sfgs)
 66                 throws Exception {
 67             SimpleDateFormat sf = new SimpleDateFormat(sfgs);
 68             return sf.parse(str);
 69         }
 70 
 71         /**
 72          * 将String转化成date 格式为yyyy-MM-dd hh:mm:ss
 73          * 
 74          * @throws ParseException
 75          * */
 76         public static Date pStringToDate(String str) throws Exception {
 77             return datetime.parse(str);
 78         }
 79 
 80         // ****************************Date转换为String****************************
 81         /**
 82          * 转换成日期格式的字符串 格式为yyyy-MM-dd
 83          * 
 84          * @param Object
 85          * @return String
 86          */
 87         public static String dateFormat(Date o) {
 88             if (o == null) {
 89                 return "";
 90             }
 91             return date.format(o);
 92         }
 93 
 94         /**
 95          * 转换成时间格式的字符串 格式为yyyy-MM-dd hh:mm:ss
 96          * 
 97          * @param Date
 98          * @return String
 99          */
100         public static String dateTimeFormat(Date o) {
101             if (o == null) {
102                 return "";
103             }
104             return datetime.format(o);
105         }
106 
107         /**
108          * 转换成给定时间格式的字符串
109          * 
110          * @param Date
111          * @param String
112          * @return String
113          */
114         public static String getDateFormat(Date d, String format) {
115             return new SimpleDateFormat(format).format(d);
116         }
117 
118         /**
119          * 日期格式化(yyyy年MM月dd日)
120          * 
121          * @param Date
122          * @return String
123          * */
124         public static String fDateCNYR(Date date) {
125             return getDateFormat(date, "yyyy年MM月dd日");
126         }
127 
128         /**
129          * 日期格式化(yyyy年MM月dd日 HH:mm)
130          * 
131          * @param Date
132          * @return String
133          * */
134         public static String fDateCNYRS(Date date) {
135             return getDateFormat(date, "yyyy年MM月dd日 HH点");
136         }
137 
138         /**
139          * 日期格式化(yyyy年MM月dd日 HH:mm)
140          * 
141          * @param Date
142          * @return String
143          * */
144         public static String fDateCNYRSF(Date date) {
145             return getDateFormat(date, "yyyy年MM月dd日 HH:mm");
146         }
147 
148         /**
149          * 日期格式化(yyyy年MM月dd日 HH:mm:ss)
150          * 
151          * @param Date
152          * @return String
153          * */
154         public static String fDateCNYRSFM(Date date) {
155             return getDateFormat(date, "yyyy年MM月dd日 HH:mm:ss");
156         }
157 
158         // ****************************时间格式的String转换为String****************************
159         /**
160          * 根据给定的时间格式字符串截取给定格式的字符串
161          * 
162          * @param d
163          *            String 给定时间格式为yyyy-MM-dd HH:mm:ss
164          * @param format
165          *            String 给定的格式
166          * @return String
167          */
168         public static String getDateFormat(String d, String format)
169                 throws Exception {
170             Date date = datetime.parse(d);
171             return getDateFormat(date, format);
172         }
173 
174         // ****************************时间格式的String转换为long****************************
175         /**
176          * 通过字符串获得long型时间
177          * 
178          * @param String
179          * @return long
180          */
181         public static long getDateFromStr(String dateStr) {
182             long temp = 0L;
183             Date date = null;
184             try {
185                 date = datetime.parse(dateStr);
186             } catch (Exception e) {
187                 e.printStackTrace();
188                 return temp;
189             }
190             temp = date.getTime();
191             return temp;
192         }
193 
194         // ****************************Date转换为给定格式的Date****************************
195         /**
196          * 日期格式化(2014-03-04)
197          * 
198          * @param Date
199          * @return Date
200          * @throws ParseException
201          * */
202         public static Date fDate(Date dat) throws Exception {
203             String dateStr = date.format(dat);
204             return date.parse(dateStr);
205         }
206 
207         /**
208          * 通过开始时间和间隔获得结束时间。
209          * 
210          * @param String
211          * @param int
212          * @return String
213          */
214         public static String getEndTime(String start, int span) {
215             if (isNullOrNone(start) || span == 0) {
216                 return null;
217             }
218             long temp = getDateFromStr(start);
219             temp += span * 60L * 1000L;
220             return datetime.format(new Date(temp));
221         }
222 
223         /**
224          * 格式化字符串,将2013-10-20 00:00:00.000000简化为2013-10-20 00:00:00
225          * 
226          * @param String
227          *            str
228          * @return String
229          * @throws ParseException
230          * */
231         public static String getFormatStringDay(String str) throws Exception {
232             Date date = datetime.parse(str);
233             return datetime.format(date);
234         }
235 
236         /**
237          * 判断是否为空
238          * 
239          * @param String
240          * @return boolean
241          */
242         public static boolean isNullOrNone(String src) {
243             if (null == src || "".equals(src)) {
244                 return true;
245             }
246             return false;
247         }
248 
249         /**
250          * 如果字符串长度大于25则截取前25个字符串后续改成省略号
251          * 
252          * @param String
253          * @return String
254          */
255         public static String showCount(String str) {
256             if (str != null) {
257                 if (str.length() > 25) {
258                     str = str.substring(0, 25);
259                     str = str + "...";
260                 }
261             } else {
262                 str = "";
263             }
264             return str;
265         }
266 
267         /**
268          * 是否符合日期格式yyyy-MM-dd
269          * 
270          * @param day
271          *            String 日期字符串
272          * @return boolean
273          */
274         public static boolean isFormatDay(String day) {
275             return day
276                     .matches("(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)");
277         }
278 
279         /**
280          * 是否符合时间格式HH:mm:ss
281          * 
282          * @param time
283          *            String 时间字符串
284          * @return boolean
285          */
286         public static boolean isFormatTime(String time) {
287             return time
288                     .matches("(0[1-9]|1[0-9]|2[0-4]):(0[1-9]|[1-5][0-9]):(0[1-9]|[1-5][0-9])(\\.000000)?");
289         }
290 
291         /**
292          * 是否符合时间格式yyyy-MM-dd HH:mm:ss
293          * 
294          * @param time
295          *            String 时间字符串
296          * @return boolean
297          */
298         public static boolean isFormat(String time) {
299             String[] temp = time.split(" ");
300             return isFormatDay(temp[0]) && isFormatTime(temp[1]);
301         }
302 
303         /**
304          * 通过给定的年、月、周获得该周内的每一天日期
305          * 
306          * @param year
307          *            int 年
308          * @param month
309          *            int 月
310          * @param week
311          *            int 周
312          * @return List<Date> 七天的日期
313          */
314         public static List<Date> getDayByWeek(int year, int month, int week) {
315             List<Date> list = new ArrayList<Date>();
316             // 先滚动到该年.
317             Calendar c = Calendar.getInstance();
318             c.set(Calendar.YEAR, year);
319             // 滚动到月:
320             c.set(Calendar.MONTH, month - 1);
321             // 滚动到周:
322             c.set(Calendar.WEEK_OF_MONTH, week);
323             // 得到该周第一天:
324             for (int i = 0; i < 6; i++) {
325                 c.set(Calendar.DAY_OF_WEEK, i + 2);
326                 list.add(c.getTime());
327             }
328             // 最后一天:
329             c.set(Calendar.WEEK_OF_MONTH, week + 1);
330             c.set(Calendar.DAY_OF_WEEK, 1);
331             list.add(c.getTime());
332             return list;
333         }
334 
335         /**
336          * 获得当前日期是本月的第几周
337          * 
338          * @return int
339          */
340         public static int getCurWeekNoOfMonth() {
341             Date date = new Date(System.currentTimeMillis());
342             Calendar calendar = Calendar.getInstance();
343             calendar.setFirstDayOfWeek(Calendar.MONDAY);
344             calendar.setTime(date);
345             return calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);
346         }
347 
348         /**
349          * 获得当前日期是星期几
350          * 
351          * @return int
352          */
353         public static int getCurWeekNo(String dat) {
354             SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
355             Date date = null;
356             try {
357                 date = format.parse(dat);
358             } catch (Exception e) {
359                 e.printStackTrace();
360             }
361             Calendar calendar = Calendar.getInstance();
362             calendar.setFirstDayOfWeek(Calendar.MONDAY);
363             calendar.setTime(date);
364             return calendar.get(Calendar.DAY_OF_WEEK);
365         }
366 
367         /**
368          * 获得当前的年份
369          * 
370          * @return
371          */
372         public static int getCurrentYear() {
373             Calendar calendar = Calendar.getInstance();
374             return calendar.get(Calendar.YEAR);
375         }
376 
377         /**
378          * 获得当前的月份
379          * 
380          * @return
381          */
382         public static int getCurrentMonth() {
383             Calendar calendar = Calendar.getInstance();
384             return calendar.get(Calendar.MONTH) + 1;
385         }
386 
387         /**
388          * 获得当前的日期天
389          * 
390          * @return
391          */
392         public static int getCurrentDay() {
393             Calendar calendar = Calendar.getInstance();
394             return calendar.get(Calendar.DATE);
395         }
396         /**
397          * 获取当月最后一天
398          * 
399          * @param Date date
400          * @param String format
401          * @return String
402          * */
403         public static String lastDayOfMoth(Date date, String format){
404             Calendar cal = Calendar.getInstance();
405             cal.setTime(date);
406             cal.set(Calendar.DAY_OF_MONTH,1);
407             cal.add(Calendar.MONTH,1);
408             cal.add(Calendar.DATE, -1);
409             date =  cal.getTime();;
410             SimpleDateFormat sf = new SimpleDateFormat(format);
411             return sf.format(date);
412         }
413         /**
414          * 获取当月最后一天
415          * 
416          * @param Date date
417          * @param String format
418          * @return String
419          * */
420         public static String firstDayOfMoth(Date date, String format){
421             Calendar cal = Calendar.getInstance();
422             cal.setTime(date);
423             cal.add(Calendar.DATE, 0);
424             date =  cal.getTime();;
425             SimpleDateFormat sf = new SimpleDateFormat(format);
426             return sf.format(date);
427         }
428         //****************************************************************
429         /**
430          * 转换成字符串方法,其中如果是Integer格式的返回0,如果是Double格式的返回0.0
431          * 
432          * @param Object
433          * @return String
434          */
435         public static String toString(Object o) {
436             if (o == null) {
437                 if (o instanceof Integer) {
438                     return "0";
439                 }
440                 if (o instanceof Double) {
441                     return "0.0";
442                 }
443                 return "";
444             } else {
445                 return o.toString();
446             }
447         }
448 
449         /**
450          * 清空字符串,如果为则转换成null
451          * 
452          * @param String
453          * @return String
454          */
455         public static String emptyString2Null(String src) {
456             if (src != null) {
457                 if ("".equals(src)) {
458                     src = null;
459                 }
460             }
461             return src;
462         }
463         /**
464          * 转化成可在hql中使用的字符串
465          * 1,2 转为 '1','2'
466          * */
467         public static String formatIds(String ids){
468             if(ids!=null&&ids!="")
469             {
470                 String[] id = ids.split(",");
471                 StringBuffer idsStr = new StringBuffer();
472                 for(String str : id){
473                     idsStr.append("'"+str+"',");
474                 }
475                 return idsStr.toString().substring(0,idsStr.length()-1);
476             }
477             else
478             {
479                 return "";
480             }
481         }
482         /**
483          * 获取当前日期前一天
484          * 
485          * @param Date date
486          * @return Date
487          * */
488         public static Date getSpecifiedDayBefore(Date date){
489             Calendar c = Calendar.getInstance();
490             c.setTime(date);
491             int day = c.get(Calendar.DATE);
492             c.set(Calendar.DATE, day-1);
493             date = c.getTime();
494             return date;
495         }
496         /**
497          * 比较两个日期的大小
498          * 
499          * @param data1
500          * @param data2
501          * 
502          * @return boolean
503          * 
504          * @author zhangss 2016-5-18 13:47:16
505          * */
506         public boolean bjDate(Date date1, Date date2){
507             if (date1.getTime() > date2.getTime()) {
508                 return true;
509             }
510             return false;
511         }
512 
513 }

优质内容筛选与推荐>>
1、C语言博客作业4
2、网络流学习(最小费用最大流)
3、【SAS NOTES】proc tabulate遇到的问题
4、Sequence operation HDU 3397
5、12、使用PWM调整LCD背光亮度


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号