1 日期模板
模板定义如下:
y 年号,如1996
M 月份,如July 或者07
d 月中第几天,如12
H 小时(24 制),如0、17
m 分钟,如32
s 钞钟,如55
S 微钞,如978
E 星期几,如Tuesday
D 一年中的第几天,如189
w week in year (Number) 27
W week in month (Number) 2
a am/pm marker (Text) PM
k hour in day (1~24) (Number) 24
K hour in am/pm (0~11) (Number) 0
z time zone (Text) Pacific Standard Time
' escape for text (Delimiter)
'' single quote (Literal) '
2 日期格式
使用SimpleDateFormat 类格式化日期,如果日期格式不对,会产生异常。
import java.text.*;
import java.util.*;
public class TestDate {
public static void main(String[] args) {
String dStr = "2001.12.12-08.23.21";
Date d = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd- HH.mm.ss");
try {
d = sdf.parse(dStr);
} catch (ParseException pe) {
System.out.println(pe.getMessage());
}
System.out.println(d);
System.out.println(d.getTime());
}
}
在java.text 包中找到那个SimpleDateFormat 类,给它一个模板,可以parse 成一个Date,然后使用Date
类的getItme() 方法,可以得到一个Long 数据。
3 取得当前日期
public String GetDateTime()
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy- MM-dd HH:mm:ss");
String mDateTime=formatter.format(cal.getTime());
return (mDateTime);
}
public java.sql.Date GetDate()
{
java.sql.Date mDate;
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy- MM-dd HH:mm:ss");
String mDateTime=formatter.format(cal.getTime());
return (java.sql.Date.valueOf(mDateTime));
}
4 当前时间
Calendar cal = Calendar.getInstance();
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM- dd HH:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss G E D F w W a E F");
String mDateTime=formatter.format(cal.getTime());
out.println(mDateTime);
out.println("<br>");
5 一年前日期
java.util.Date myDate=new java.util.Date();
long myTime=(myDate.getTime()/1000)-60*60*24*365;
myDate.setTime(myTime*1000);
String mDate=formatter.format(myDate);
out.println(mDate);
out.println("<br>");
6 明天日期
myDate=new java.util.Date();
myTime=(myDate.getTime()/1000)+60*60*24;
myDate.setTime(myTime*1000);
mDate=formatter.format(myDate);
out.println(mDate);
out.println("<br>");
7 两个时间之间的天数
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM- dd");
java.util.Date date= myFormatter.parse("2003-05-1");
java.util.Date mydate= myFormatter.parse("1899-12-30");
long day=(date.getTime()-mydate.getTime())/(24*60*60*1000);
out.println(day);
out.println("<br>");
8 加半小时
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
java.util.Date date1 = format.parse("2002-02-28 23:16:00");
long Time=(date1.getTime()/1000)+60*30;
date1.setTime(Time*1000);
String mydate1=formatter.format(date1);
out.println(mydate1);
out.println("<br>");
9 年月周求日期
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM F E");
java.util.Date date2= formatter2.parse("2003-05 5 星期五");
SimpleDateFormat formatter3 = new SimpleDateFormat("yyyy-MM-dd");
String mydate2=formatter3.format(date2);
out.println(mydate2);
out.println("<br>");
10 求给定日期是星期几
mydate= myFormatter.parse("2001-1-1");
SimpleDateFormat formatter4 = new SimpleDateFormat("E");
String mydate3=formatter4.format(mydate);
out.println(mydate3);
out.println("<br>");
/////////////////////////////////////////////////////////////////////////
import java.text.SimpleDateFormat;
2import java.util.Calendar;
3import java.util.Date;
4import java.util.GregorianCalendar;
5
6/** *//**
7 *
8 * @author zzk
9 *
10 */
11public class DateUtil {
12
13 /** *//**
14 * 取得某天相加(减)後的那一天
15 *
16 * @param date
17 * @param num(可正可负)
18 * @return
19 */
20 public static Date getAnotherDate(Date date, int num) {
21 Calendar c = Calendar.getInstance();
22 c.setTime(date);
23 c.add(Calendar.DAY_OF_YEAR, num);
24 return c.getTime();
25 }
26
27 /** *//**
28 * 取得某月的的最后一天
29 *
30 * @param year
31 * @param month
32 * @return
33 */
34 public static Date getLastDayOfMonth(int year, int month) {
35 Calendar cal = Calendar.getInstance();
36 cal.set(Calendar.YEAR, year);// 年
37 cal.set(Calendar.MONTH, month - 1);// 月,因为Calendar里的月是从0开始,所以要减1
38 cal.set(Calendar.DATE, 1);// 日,设为一号
39 cal.add(Calendar.MONTH, 1);// 月份加一,得到下个月的一号
40 cal.add(Calendar.DATE, -1);// 下一个月减一为本月最后一天
41 return cal.getTime();// 获得月末是几号
42 }
43
44 /** *//**
45 * 取得某天是一年中的多少周
46 *
47 * @param date
48 * @return
49 */
50 public static int getWeekOfYear(Date date) {
51 Calendar c = new GregorianCalendar();
52 c.setFirstDayOfWeek(Calendar.MONDAY);
53 c.setMinimalDaysInFirstWeek(7);
54 c.setTime(date);
55 return c.get(Calendar.WEEK_OF_YEAR);
56 }
57
58 /** *//**
59 * 取得某天所在周的第一天
60 *
61 * @param date
62 * @return
63 */
64 public static Date getFirstDayOfWeek(Date date) {
65 Calendar c = new GregorianCalendar();
66 c.setFirstDayOfWeek(Calendar.MONDAY);
67 c.setTime(date);
68 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
69 return c.getTime();
70 }
71
72 /** *//**
73 * 取得某天所在周的最后一天
74 *
75 * @param date
76 * @return
77 */
78 public static Date getLastDayOfWeek(Date date) {
79 Calendar c = new GregorianCalendar();
80 c.setFirstDayOfWeek(Calendar.MONDAY);
81 c.setTime(date);
82 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
83 return c.getTime();
84 }
85
86 /** *//**
87 * 取得某一年共有多少周
88 *
89 * @param year
90 * @return
91 */
92 public static int getMaxWeekNumOfYear(int year) {
93 Calendar c = new GregorianCalendar();
94 c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
95 return getWeekOfYear(c.getTime());
96 }
97
98 /** *//**
99 * 取得某年某周的第一天
100 * 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-05为2009年第一周的第一天
101 * @param year
102 * @param week
103 * @return
104 */
105 public static Date getFirstDayOfWeek(int year, int week) {
106 Calendar calFirst = Calendar.getInstance();
107 calFirst.set(year, 0, 7);
108 Date firstDate = getFirstDayOfWeek(calFirst.getTime());
109
110 Calendar firstDateCal = Calendar.getInstance();
111 firstDateCal.setTime(firstDate);
112
113 Calendar c = new GregorianCalendar();
114 c.set(Calendar.YEAR, year);
115 c.set(Calendar.MONTH, Calendar.JANUARY);
116 c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));
117
118 Calendar cal = (GregorianCalendar) c.clone();
119 cal.add(Calendar.DATE, (week - 1) * 7);
120 firstDate = getFirstDayOfWeek(cal.getTime());
121
122 return firstDate;
123 }
124
125 /** *//**
126 * 取得某年某周的最后一天
127 * 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-04为2008年最后一周的最后一天
128 * @param year
129 * @param week
130 * @return
131 */
132 public static Date getLastDayOfWeek(int year, int week) {
133 Calendar calLast = Calendar.getInstance();
134 calLast.set(year, 0, 7);
135 Date firstDate = getLastDayOfWeek(calLast.getTime());
136
137 Calendar firstDateCal = Calendar.getInstance();
138 firstDateCal.setTime(firstDate);
139
140 Calendar c = new GregorianCalendar();
141 c.set(Calendar.YEAR, year);
142 c.set(Calendar.MONTH, Calendar.JANUARY);
143 c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));
144
145 Calendar cal = (GregorianCalendar) c.clone();
146 cal.add(Calendar.DATE, (week - 1) * 7);
147 Date lastDate = getLastDayOfWeek(cal.getTime());
148
149 return lastDate;
150 }
151}
152
posted on 2008-06-13 09:50
南山隐士 阅读(634)
评论(0) 编辑 收藏