JAVA获取各种日期时间

 

31 public void printFormatDate(Date d) {
32 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
33 System.out.println(df.format(d));
34 }
35
36 public void test1() {// 下个月第一天 37 Date d = new Date();
38 d = DateUtils.ceiling(d, Calendar.MONTH);// 进位月份 39 printFormatDate(d);// 2016-02-01 00:00:00 40 }
41
42 public void test2() {// 下个月第一天 43 Date d = new Date();
44 d = DateUtils.addMonths(d, 1);// 月+1 45 d = DateUtils.setDays(d, 1);// 设置日为1号 46 d = DateUtils.truncate(d, Calendar.DATE);// 过滤时分秒 47 printFormatDate(d);// 2016-02-01 00:00:00 48 }
49
50 public void test3() {// 当月最后一天最后一秒 51 Date d = new Date();
52 d = DateUtils.ceiling(d, Calendar.MONTH);// 进位月份 53 d = DateUtils.addMilliseconds(d, -1);// 减少1秒 54 printFormatDate(d);// 2016-01-31 23:59:59 55 }
56
57 public void test4() {// 当月第一天第一秒 58 Date d = new Date();
59 d = DateUtils.truncate(d, Calendar.MONTH);// 截取时间到月份 60 printFormatDate(d);// 2016-01-01 00:00:00 61 }
62
63 public void test5() {// 下个月的这个时候 64 Date d = new Date();
65 d = DateUtils.addMonths(d, 1);
66 printFormatDate(d);// 2016-02-21 09:46:02 67 }
68
69 public void test6() {// 昨天的这个时候 70 Date d = new Date();
71 d = DateUtils.addDays(d, -1);// 增加1月,如果下个月没有这1天,那就不加 72 printFormatDate(d);// 2016-01-20 09:46:48 73 }
74
75 public void test7() {// addMonth的注意点:2月没有29号 76 Date d = new Date();
77 d = DateUtils.setDays(d, 29);
78 d = DateUtils.addMonths(d, 1);// 并没有增加 79 printFormatDate(d);// 2016-01-29 09:47:45 80 }
81
82 public void test8() {// 这个月15号 83 Date d = new Date();
84 d = DateUtils.setDays(d, 15);
85 d = DateUtils.truncate(d, Calendar.DATE);// 截取时间到日 86 printFormatDate(d);// 2016-01-15 00:00:00 87 }
88
89 public void test9() {// 输出包含今天的这个星期的每一天,星期第一天是周日 90 Date d = new Date();
91 Iterator<Calendar> c = DateUtils.iterator(d, DateUtils.RANGE_WEEK_SUNDAY);
92 System.out.println();
93 while (c.hasNext()) {
94 printFormatDate(new Date(c.next().getTimeInMillis()));
95 }
96 }
97
98 public void test10() {// 今天是今年第几天 99 Date d = new Date();
100 System.out.println(DateUtils.getFragmentInDays(d, Calendar.YEAR));// 21101 }
102
103 public void test11() {// 这个月第一个周日104 Date d = new Date();
105 d = DateUtils.setDays(d, 1);
106 while (true) {
107 Calendar c = Calendar.getInstance();
108 c.setTime(d);
109 if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
110 printFormatDate(d);// 2016-01-03 10:31:43111 break;
112 } else {
113 d = DateUtils.addDays(d, 1);
114 }
115 }
116 }
117
118 public void test12() {// 距2月1号还有多少天119 Date d = new Date();
120 Date d2 = new Date();
121 d2 = DateUtils.ceiling(d, Calendar.MONTH);// 2.1号122 long day2 = DateUtils.getFragmentInDays(d2, Calendar.YEAR);//2.1是今年第几天?123 long day1 = DateUtils.getFragmentInDays(d, Calendar.YEAR);//今天是今年第几天?124 System.out.println(day2 - day1);//11125 }
 
01.获取当月的天数
 Calendar a = Calendar.getInstance();  
        a.set(Calendar.DATE, 1);  
        a.roll(Calendar.DATE, -1);  
        int maxDate = a.get(Calendar.DATE);  
        return maxDate; 
 
02.根据年 月 获取对应的月份 天数      
    Calendar a = Calendar.getInstance();     
    a.set(Calendar.YEAR, year);     
    a.set(Calendar.MONTH, month - 1);     
    a.set(Calendar.DATE, 1);     
    a.roll(Calendar.DATE, -1);      
  int maxDate = a.get(Calendar.DATE); 
 
03.根据日期 找到对应日期的 星期 
String dayOfweek = "-1";  
        try {  
            SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
            Date myDate = myFormatter.parse(date);  
            SimpleDateFormat formatter = new SimpleDateFormat("E");  
            String str = formatter.format(myDate);  
            dayOfweek = str;  
              
        } catch (Exception e) {  
            System.out.println("错误!");  
        }  
 
 
 /**
     * 计算开始日期到结束日期之间月数
     * @param begDate  开始日期
     * @param endDate  结束日期
     * @return
     */
    public static BigDecimal calculationAmount(Date begDate,Date endDate) {
        Calendar cal1 = Calendar.getInstance(); 
        cal1.setTime(begDate);
        cal1.add(Calendar.MONTH, 1); 
        cal1.set(Calendar.DAY_OF_MONTH, cal1.getActualMinimum(Calendar.DAY_OF_MONTH));  
        Date preMonth =cal1.getTime(); //开始日期的下一个月
        Calendar cal2 = Calendar.getInstance(); 
        cal2.setTime(endDate); 
        cal2.add(Calendar.MONTH, -1); 
        cal2.set(Calendar.DAY_OF_MONTH, cal2.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date lastMonth = cal2.getTime(); //结束日期的上一个月
        long midValue = 0;//开始日期的下一个月和结束日期的上一个月之间有多少月
        if(preMonth.before(lastMonth)) {
            midValue = DataTimeUtils.differentDate("M", preMonth, lastMonth)+1;
        }
        long begDateNum = DataTimeUtils.differentDate("D", begDate, getEndMonthDate(begDate))+1;//开始日期到当前月底的的天数
        long begMouthNum = getCurrentMonthDay(begDate);//开始日期当前月有多少天
        BigDecimal begValue = new BigDecimal(begDateNum).divide(new BigDecimal(begMouthNum),6,BigDecimal.ROUND_HALF_UP);
        long endDateNum = DataTimeUtils.differentDate("D", getPreMonthDate(endDate),endDate)+1;//结束日期当月的第一天到结束日期的天数
        long endMouthNum = getCurrentMonthDay(endDate);//结束日期当前月有多少天
        BigDecimal endValue = new BigDecimal(endDateNum).divide(new BigDecimal(endMouthNum),6,BigDecimal.ROUND_HALF_UP);
        BigDecimal sumDate =begValue.add(endValue.add(new BigDecimal(midValue)));
        return sumDate;
    }
 
/**
     * 判断日期是否在自然季内
     */
    public static int getNaturalSeason(Date begDate) {
        Calendar beg = Calendar.getInstance();//设置开始日期
        beg.setTime(begDate);
        if((beg.get(Calendar.MONTH)+1)>0 && (beg.get(Calendar.MONTH)+1)<=3) {
            return 1;
        }else if((beg.get(Calendar.MONTH)+1)>=4 && (beg.get(Calendar.MONTH)+1)<=6) {
            return 2;
        }else if((beg.get(Calendar.MONTH)+1)>=7 && (beg.get(Calendar.MONTH)+1)<=9) {
            return 3;
        }else if((beg.get(Calendar.MONTH)+1)>=10 && (beg.get(Calendar.MONTH)+1)<=12) {
            return 4;
        }
        return 0;
    }
    
    /**
     * 判断日期是否在自然半年内
     */
    public static int getNaturalHalfYear(Date begDate) {
        Calendar beg = Calendar.getInstance();//设置开始日期
        beg.setTime(begDate);
        if((beg.get(Calendar.MONTH)+1)>0 && (beg.get(Calendar.MONTH)+1)<=6) {
            return 1;
        }else if((beg.get(Calendar.MONTH)+1)>=7 && (beg.get(Calendar.MONTH)+1)<=12) {
            return 2;
        }
        return 0;
    }
    /**
     * 判断日期是否在自然年内
     */
    public static int getNaturalYear(Date begDate) {
        Calendar beg = Calendar.getInstance();//设置开始日期
        beg.setTime(begDate);
        if((beg.get(Calendar.MONTH)+1)>0 && (beg.get(Calendar.MONTH)+1)<=12) {
            return 1;
        }
        return 0;
    }
 
 
 
/**
     * 获取任意时间的月第一天
     * @param repeatDate
     * @return
     */ 
    public static Date getPreMonthDate(Date repeatDate){ 
        return DateUtils.truncate(repeatDate,Calendar.MARCH);
    }
        
    /**
     * 获取任意时间的月的最后一天
     * @param repeatDate
     * @return
    */ 
    public static Date getEndMonthDate(Date repeatDate) { 
        Calendar calendar = Calendar.getInstance(); 
        calendar.setTime(repeatDate); 
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); 
        return calendar.getTime(); 
    } 
 
 
/**
     * 根据日期计算当月有多少天
     * @param date
     * @return
     */
    public static long getCurrentMonthDay(Date date) { 
        Calendar a = Calendar.getInstance(); 
        a.setTime(date);
        a.set(Calendar.DATE, 1); 
        a.roll(Calendar.DATE, -1); 
        int maxDate = a.get(Calendar.DATE); 
        return maxDate; 
    }
    
 
/**
     * 判断时间格式
     */
    public static boolean isValidDate(String str) {
         boolean convertSuccess=true;
         // 指定日期格式为四位年/两位月份/两位日期,注意yyyy-MM-dd区分大小写;
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
              try {
         // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007-02-29会被接受,并转换成2007-03-01
                  format.setLenient(false);
                  format.parse(str);
               } catch (ParseException e) {
         // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
                    convertSuccess=false;
                }
                return convertSuccess;
         }
 
 /**
     *
     * @param ts MinimumGuaranteeDomain 的list
     * @return  false 时间交叉  true 时间不交叉
     */
    public static  boolean check(List<MinimumGuaranteeDomain>  ts ) {
        Collections.sort(ts, new MinimumGuaranteeDomain());
        boolean flag=true;
        for(int i=0; i<ts.size()-1;i++) {
            MinimumGuaranteeDomain item1=ts.get(i);;
            MinimumGuaranteeDomain item2=ts.get(i+1);
            Date end = new Date(DataTimeUtils.getTimeLong(item1.getEnddate())*1000);
            Date beg = new Date(DataTimeUtils.getTimeLong(item2.getBegdate())*1000);
            if(DataTimeUtils.differentDate("D",end,beg)<0) {
                flag=false;
                break;
            }            
         }
        return flag ;
    }
posted @ 2019-03-18 10:33  仙子说  阅读(5361)  评论(1编辑  收藏  举报