1 import java.text.ParseException;
2 import java.text.SimpleDateFormat;
3 import java.util.Calendar;
4 import java.util.Date;
5
6
7 public class DateUtil {
8 static Calendar ca = Calendar.getInstance();
9 /**
10 *1 将Date转换为String
11 * @return
12 */
13 public static void getDateToString(){
14 Date a = new Date();
15 SimpleDateFormat sdf = new SimpleDateFormat("y-M-d");
16 String str = sdf.format(a);
17 System.err.println(str);
18 }
19 /**
20 *2 将String日期转换为Date
21 * @param str
22 * @return
23 */
24 public static Date getStringToDate(String str){
25 SimpleDateFormat sdf = new SimpleDateFormat("y-M-d");
26 Date date=null;
27 try {
28 date = sdf.parse(str);
29 } catch (ParseException e) {
30 // TODO Auto-generated catch block
31 e.printStackTrace();
32 } finally{
33 System.err.println(date);
34 return date;
35 }
36 }
37 /**
38 *3 计算该月份最多有多少天
39 * @param year
40 * @param month
41 * @return
42 */
43 public static int getMaximumDay(int year,int month){
44 Date now = new Date();
45
46 ca.clear();
47 ca.set(Calendar.YEAR,year);
48 ca.set(Calendar.MONTH,month-1);
49 //ca.set(Calendar.DATE,day);
50 int a = ca.getActualMaximum(Calendar.DAY_OF_MONTH);
51 System.out.println(a);
52 return a ;
53 }
54 /**
55 *4 指定日期计算这是当年第几天第几星期
56 *
57 * @param str
58 */
59 public static void setDate(String str) {
60 SimpleDateFormat sdf = new SimpleDateFormat("y-M-d");
61 Date date = null;
62 try {
63 date = sdf.parse(str);
64 ca.clear();
65 ca.setTime(date);
66 int a = ca.get(Calendar.DAY_OF_YEAR);
67 int b = ca.get(Calendar.WEEK_OF_YEAR);
68 System.err.println("此日期为当年的第"+a+"天"+"第"+b+"周");
69
70 } catch (ParseException e) {
71 // TODO Auto-generated catch block
72 e.printStackTrace();
73 }
74 }
75 /**
76 *5 计算一年中第几个星期天是几号:
77 * @param args
78 */
79 public static void getDate(int year,int week){
80 ca.clear();
81 ca.set(Calendar.YEAR, year);
82 ca.set(Calendar.WEEK_OF_YEAR, week);
83 ca.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
84 SimpleDateFormat sdf = new SimpleDateFormat();
85 int m = ca.get(Calendar.DAY_OF_MONTH);
86 int n = ca.get(Calendar.MONTH);
87 System.out.println(sdf.format(ca.getTime()));
88 }
89 /**
90 *6 将日历转换为日期
91 */
92 public static void getCalendarToDate(){
93 ca.getTime();
94 System.out.println(ca.getTime());
95 }
96 /**
97 *7 将日期转换为日历
98 */
99 public static void getDateToCalendar(){
100 Date now = new Date();
101 ca.setTime(now);
102 ca.getCalendarType();
103 System.out.println(ca);
104 // System.out.println(ca.getCalendarType());
105 }
106 /**
107 *8 计算两个日期相隔多少天
108 * @param date1
109 * @param date2
110 */
111 public static void getDays(String date1,String date2){
112 ca.setTime(getStringToDate(date1));
113 int a1 = ca.get(Calendar.DAY_OF_YEAR);
114 ca.setTime(getStringToDate(date2));
115 int a2 = ca.get(Calendar.DAY_OF_YEAR);
116 System.err.println("同年两个日期的相隔的天数为:"+Math.abs(a2-a1));
117 }
118 public static void main(String[] args) {
119 // TODO Auto-generated method stub
120 // getDateToString(); //1,将Date的日期转化为String的形式返回
121 // getStringToDate("2018-09-12");//2.将String类型转成
122 // getMaximumDay(2017,2);//3.指定年份和月份,计算该月份最大有多少天Date类型
123 // setDate("2018-09-26");//4.指定日期,计算该天是当年第多少天,多个个星期
124 // getDate(2018,2);//5.计算一年中的第几星期天是几号
125 // getDateToCalendar();//6.Date类型转成Calendar
126 // getCalendarToDate();//7.Calendar类型转成Date
127 // getDays("2018-08-01","2018-09-1");//8.计算两个同一年的时间中间相隔的天数
128 }
129
130 }