文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>java 日期操作辅助类

java 日期操作辅助类

时间:2010-10-04  来源:幽哥

FormatDate.java

 

 

view sourceprint?
01 package com.ucit.ca.webApp.tool;
02  
03 import java.util.*;
04 import java.text.*;
05  
06 public class FormatDate {
07  
08     public FormatDate() {
09     }
10  
11     // 格式化日期为字符串 "yyyy-MM-dd   hh:mm"
12     public String formatDateTime(Date basicDate, String strFormat) {
13         SimpleDateFormat df = new SimpleDateFormat(strFormat);
14         return df.format(basicDate);
15     }
16  
17     // 格式化日期为字符串 "yyyy-MM-dd   hh:mm"
18     public String formatDateTime(String basicDate, String strFormat) {
19         SimpleDateFormat df = new SimpleDateFormat(strFormat);
20         Date tmpDate = null;
21         try {
22             tmpDate = df.parse(basicDate);
23         } catch (Exception e) {
24             // 日期型字符串格式错误
25         }
26         return df.format(tmpDate);
27     }
28  
29     // 当前日期加减n天后的日期,返回String (yyyy-mm-dd)
30     public String nDaysAftertoday(int n) {
31         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
32         Calendar rightNow = Calendar.getInstance();
33         // rightNow.add(Calendar.DAY_OF_MONTH,-1);
34         rightNow.add(Calendar.DAY_OF_MONTH, +n);
35         return df.format(rightNow.getTime());
36     }
37  
38     // 当前日期加减n天后的日期,返回String (yyyy-mm-dd)
39     public Date nDaysAfterNowDate(int n) {
40         Calendar rightNow = Calendar.getInstance();
41         // rightNow.add(Calendar.DAY_OF_MONTH,-1);
42         rightNow.add(Calendar.DAY_OF_MONTH, +n);
43         return rightNow.getTime();
44     }
45  
46     // 给定一个日期型字符串,返回加减n天后的日期型字符串
47     public String nDaysAfterOneDateString(String basicDate, int n) {
48         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
49         Date tmpDate = null;
50         try {
51             tmpDate = df.parse(basicDate);
52         } catch (Exception e) {
53             // 日期型字符串格式错误
54         }
55         long nDay = (tmpDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n)
56                 * (24 * 60 * 60 * 1000);
57         tmpDate.setTime(nDay);
58  
59         return df.format(tmpDate);
60     }
61  
62     // 给定一个日期,返回加减n天后的日期
63     public Date nDaysAfterOneDate(Date basicDate, int n) {
64         long nDay = (basicDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n)
65                 * (24 * 60 * 60 * 1000);
66         basicDate.setTime(nDay);
67  
68         return basicDate;
69     }
70  
71     // 计算两个日期相隔的天数
72     public int nDaysBetweenTwoDate(Date firstDate, Date secondDate) {
73         int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
74         return nDay;
75     }
76  
77     // 计算两个日期相隔的天数
78     public int nDaysBetweenTwoDate(String firstString, String secondString) {
79         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
80         Date firstDate = null;
81         Date secondDate = null;
82         try {
83             firstDate = df.parse(firstString);
84             secondDate = df.parse(secondString);
85         } catch (Exception e) {
86             // 日期型字符串格式错误
87         }
88  
89         int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
90         return nDay;
91     }
92  
93 }
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载