Java中获取指定日为星期几及其他日期操作

摘自:http://blog.csdn.net/cselmu9/article/details/8625530#t6

在开发中经常会使用到一些日期方面的操作,下面例子展示几个常用的操作。

1、取得指定日期是星期几

取得指定日期是星期几可以采用下面两种方式取得日期是星期几:

a、使用Calendar类

 1 //根据日期取得星期几
 2     public static String getWeek(Date date){
 3         String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
 4         Calendar cal = Calendar.getInstance();
 5         cal.setTime(date);
 6         int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
 7         if(week_index<0){
 8             week_index = 0;
 9         } 
10         return weeks[week_index];
11     }
b、使用SimpleDateFormat类
1 //根据日期取得星期几
2     public static String getWeek(Date date){ 
3         SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
4         String week = sdf.format(date);
5         return week;
6     }

注:格式化字符串存在区分大小写
对于创建SimpleDateFormat传入的参数:EEEE代表星期,如“星期四”;MMMM代表中文月份,如“十一月”;MM代表月份,如“11”;
yyyy代表年份,如“2010”;dd代表天,如“25”


        

2、取得日期是某年的第几周

根据日期入得日期是某年的第几周。

1 //取得日期是某年的第几周
2     public static int getWeekOfYear(Date date){
3         Calendar cal = Calendar.getInstance();
4         cal.setTime(date);
5         int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
6         return week_of_year;
7     }

3、得到某年的某个月有多少天

已知年份和月份,取得该月有多少天。

//取得某个月有多少天
    public static int getDaysOfMonth(int year,int month){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month-1);
        int days_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        return days_of_month;
    }

4、取得两个日期之间的相差多少天

已知两个日期,计算它们之间相差多少天。

1 // 取得两个日期之间的相差多少天
2     public static long getDaysBetween(Date date0, Date date1) {
3         long daysBetween = (date0.getTime() - date1.getTime() + 1000000) / 86400000;// 86400000=3600*24*1000  用立即数,减少乘法计算的开销
4         return daysBetween;
5     }

5、完整的测试代码

 1 package org.ml.test;
 2 
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Calendar;
 6 import java.util.Date;
 7 
 8 public class CalendarDemo {
 9     public static void main(String[] args) {
10         String strDate = "2013-03-08";// 定义日期字符串
11         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");// 定义日期格式
12         Date date = null;
13         try {
14             date = format.parse(strDate);// 将字符串转换为日期
15         } catch (ParseException e) {
16             System.out.println("输入的日期格式不合理!");
17         }
18         System.out.println(strDate + "是:" + getWeek(date));
19         System.out.println(strDate + "是一年的第:" + getWeekOfYear(date) + "周");
20         System.out.println(strDate + "是一年的" + (date.getMonth() + 1) + "月有:"
21                 + getDaysOfMonth(date.getYear(), date.getMonth() + 1) + "天");
22         System.out.println(strDate + "距离" + (format.format(new Date())) + "还有"
23                 + getDaysBetween(date, new Date()) + "天");
24 
25     }
26 
27     // 根据日期取得星期几
28     public static String getWeek(Date date) {
29         // String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
30         // Calendar cal = Calendar.getInstance();
31         // cal.setTime(date);
32         // int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
33         // if(week_index<0){
34         // week_index = 0;
35         // }
36         // return weeks[week_index];
37         SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
38         String week = sdf.format(date);
39         return week;
40     }
41 
42     // 取得日期是某年的第几周
43     public static int getWeekOfYear(Date date) {
44         Calendar cal = Calendar.getInstance();
45         cal.setTime(date);
46         int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
47         return week_of_year;
48     }
49 
50     // 取得某个月有多少天
51     public static int getDaysOfMonth(int year, int month) {
52         Calendar cal = Calendar.getInstance();
53         cal.set(Calendar.YEAR, year);
54         cal.set(Calendar.MONTH, month-1);
55         int days_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
56         return days_of_month;
57     }
58 
59     // 取得两个日期之间的相差多少天
60     public static long getDaysBetween(Date date0, Date date1) {
61         long daysBetween = (date0.getTime() - date1.getTime() + 1000000) / 86400000;// 86400000=3600*24*1000  用立即数,减少乘法计算的开销
62         return daysBetween;
63     }
64 
65 }

6、测试结果

7、引出的问题

看下面的代码:

 1 public static void main(String[] args) throws Exception{
 2     String strDate = "999-999-999";// 定义日期字符串
 3     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");// 定义日期格式
 4     Date date = null;
 5 try {
 6     date = format.parse(strDate);// 将字符串转换为日期
 7 } catch (ParseException e) {
 8     System.out.println("日期格式有误,请给出正确的日期格式");
 9     return;
10 }
11     System.out.println(format.format(date)); 
12 }

Java中使用yyyy-MM-dd日期格式进行转换,转换字符串为999-999-999时,没有出现异常,反倒是执行通过了。

运行结果:1084-11-23

解决办法如下:

在date = format.parse(strDate)前面加上format.setLenient(false)就行了。意思是【指定日期/时间解析是否不严格。进行不严格解析时,解析程序可以使用启发式的方法来解释与此对象的格式不精确匹配的输入。进行严格解析时,输入必须匹配此对象的格式。 】

原文地址:https://www.cnblogs.com/kingxiaozi/p/3877455.html