java关于时间之间的加减,字符串和时间的转换,年月日的加减的工具类

package com.jszh.ECPlatform.os.util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

import com.jszh.ECPlatform.order.entity.Order;
import com.jszh.common.util.DateUtils;

public class TimeUtil {
	/**
	 * 描述:
	 * @param args
	 * @author feitian
	 * 2017年12月1日上午11:41:52
	 */
	public static void main(String[] args) {
		// 获取当前年份、月份、日期  
        Calendar cale = null;  
        cale = Calendar.getInstance();  
        int year = cale.get(Calendar.YEAR);  
        int month = cale.get(Calendar.MONTH) + 1;  
        int day = cale.get(Calendar.DATE);  
        int hour = cale.get(Calendar.HOUR_OF_DAY);  
        int minute = cale.get(Calendar.MINUTE);  
        int second = cale.get(Calendar.SECOND);  
        int dow = cale.get(Calendar.DAY_OF_WEEK);  
        int dom = cale.get(Calendar.DAY_OF_MONTH);  
        int doy = cale.get(Calendar.DAY_OF_YEAR);  
  
        System.out.println("Current Date: " + cale.getTime());  
        System.out.println("Year: " + year);  
        System.out.println("Month: " + month);  
        System.out.println("Day: " + day);  
        System.out.println("Hour: " + hour);  
        System.out.println("Minute: " + minute);  
        System.out.println("Second: " + second);  
        System.out.println("Day of Week: " + dow);  
        System.out.println("Day of Month: " + dom);  
        System.out.println("Day of Year: " + doy);  
  
        // 获取当月第一天和最后一天  
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
        String firstday, lastday;  
        // 获取前月的第一天  
        cale = Calendar.getInstance();  
        cale.add(Calendar.MONTH, 0);  
        cale.set(Calendar.DAY_OF_MONTH, 1);  
        firstday = format.format(cale.getTime());  
        // 获取前月的最后一天  
        cale = Calendar.getInstance();  
        cale.add(Calendar.MONTH, 1);  
        cale.set(Calendar.DAY_OF_MONTH, 0);  
        lastday = format.format(cale.getTime());  
        System.out.println("本月第一天和最后一天分别是 : " + firstday + " and " + lastday);  
  
        // 获取当前日期字符串  
        Date d = new Date();  
        System.out.println("当前日期字符串1:" + format.format(d));  
        System.out.println("当前日期字符串2:" + year + "/" + month + "/" + day + " "  
                + hour + ":" + minute + ":" + second);  
        System.out.println( getAssignFirstDayOfMonth(2020,1));  
        System.out.println( getAssignLastDayOfMonth(2020,1));  
        
        
        Order order =new Order();
        order.setMonthCount(1);
        order.setSeatCount(1);
        BigDecimal seatfee  =  new BigDecimal(Long.valueOf(100));
        BigDecimal monthcount = new BigDecimal(Long.valueOf(order.getMonthCount()));
    	BigDecimal seatCount = new BigDecimal(Long.valueOf(order.getSeatCount()-1));
        BigDecimal seatfeeTotal =(seatfee.multiply(seatCount)).add(seatfee.multiply(monthcount));	
        System.out.println(seatfeeTotal.intValue());
        
        BigDecimal test = getOrderAmountForBasics(new BigDecimal("12000"),1,2);
        System.out.println(test.intValue());
	}
  /**
   * 获取本月最后一天
   * @return
   */
   public static String  getLastDayOfMont(){
	   Calendar cale = null;  
       cale = Calendar.getInstance();  
       // 获取当月第一天和最后一天  
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
       String  lastday;  
       // 获取前月的最后一天  
       cale = Calendar.getInstance();  
       cale.add(Calendar.MONTH, 1);  
       cale.set(Calendar.DAY_OF_MONTH, 0);  
       lastday = format.format(cale.getTime());
       System.out.println("本月最后一天:"+ lastday);
	return lastday;  
   }
   /**
    * 获取本月第一天
    * @return
    */
   public static String  getFirstDayOfMont(){
	   Calendar cale = null;  
       cale = Calendar.getInstance();   
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
       String firstday; 
       // 获取前月的第一天  
       cale = Calendar.getInstance();  
       cale.add(Calendar.MONTH, 0);  
       cale.set(Calendar.DAY_OF_MONTH, 1);  
       firstday = format.format(cale.getTime()); 
       System.out.println("本月第一天: " + firstday );
	return firstday;  
   }
   
   
   /**
	 * 
	 * @param nowTime   当前时间	
	 * @param startTime	开始时间
	 * @param endTime   结束时间
	 * @return
	 * @author sunran   判断当前时间在时间区间内
	 */
	public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
       if (nowTime.getTime() == startTime.getTime()
               || nowTime.getTime() == endTime.getTime()) {
           return true;
       }

       Calendar date = Calendar.getInstance();
       date.setTime(nowTime);

       Calendar begin = Calendar.getInstance();
       begin.setTime(startTime);

       Calendar end = Calendar.getInstance();
       end.setTime(endTime);

       if (date.after(begin) && date.before(end)) {
           return true;
       } else {
           return false;
       }

	}
	
	 /**
     * 获取指定年月的第一天
     * @param year
     * @param month
     * @return
     */
    public static String getAssignFirstDayOfMonth(int year, int month) {     
        Calendar cal = Calendar.getInstance();   
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份 
        cal.set(Calendar.MONTH, month-1); 
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数 
        cal.set(Calendar.DAY_OF_MONTH,firstDay);  
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());  
    }
    
    /**
     * 获取指定年月的最后一天
     * @param year
     * @param month
     * @return
     */
     public static String getAssignLastDayOfMonth(int year, int month) {     
         Calendar cal = Calendar.getInstance();     
         //设置年份  
         cal.set(Calendar.YEAR, year);  
         //设置月份  
         cal.set(Calendar.MONTH, month-1); 
         //获取某月最大天数
         int lastDay = cal.getActualMaximum(Calendar.DATE);
         //设置日历中月份的最大天数  
         cal.set(Calendar.DAY_OF_MONTH, lastDay);  
         //格式化日期
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
         return sdf.format(cal.getTime());
     }
     private static BigDecimal getOrderAmountForBasics(BigDecimal price,int monthCount,int seatCount) {
    		int day = DateUtils.getType(Calendar.DATE);
 	   	Calendar cal = Calendar.getInstance();     
    		int lastDay = cal.getActualMaximum(Calendar.DATE);
    		int disparity = lastDay-day==0?1:lastDay-day+1;
    		BigDecimal bdLastDay = new BigDecimal(lastDay);
    		BigDecimal disparityDay = new BigDecimal(disparity);
    		//no四舍五入
    		BigDecimal dayPrice = price.divide(bdLastDay,2,BigDecimal.ROUND_HALF_UP);
    		//四舍五入
//    	BigDecimal dayPrice = price.divide(bdLastDay,2,  BigDecimal.ROUND_HALF_UP);
    		BigDecimal monthPrice = dayPrice.multiply(disparityDay);
    		BigDecimal bdSeatCount = new BigDecimal(seatCount);
    		if(monthCount == 1) {
    			return monthPrice.multiply(bdSeatCount);
    		}else {
    			BigDecimal bdMonthCount = new BigDecimal(monthCount-1);
    			return (monthPrice.multiply(bdSeatCount)).add(price.multiply(bdMonthCount).multiply(bdSeatCount));
    		}
    		
 	}
    /**
     * 获取年月日,出参(例子:20180401)
     * @return
     */
    public static String getserialNumber(){
    	 Calendar cale = null;  
         cale = Calendar.getInstance();  
         int year = cale.get(Calendar.YEAR);  
         int month = cale.get(Calendar.MONTH) + 1;  
         int day = cale.get(Calendar.DATE);
         String yearstr = String.valueOf(year);
         String monthstr = String.valueOf(month);
         String daystr = String.valueOf(day);
         if(month<10){
        	 monthstr = "0"+monthstr;
         }
         if(day<10){
        	 daystr = "0"+daystr;
         }
         
         return yearstr+monthstr+daystr;
         
    }
   /**
    * 计算两个时间之间相差的秒数
    * @param startDate
    * @param endDate
    * @return
    */
    public static long betweenS(Date startDate,Date endDate){ 	 
    	         long betweenS = (endDate.getTime() - startDate.getTime()) / (1000);
    	         return betweenS;
    	     }

    /*计算俩个日期之间相差多少个小时*/
    /*调用完这个相减方法,后面的需要你先判断返回值是否为空,不为空才能继续后面的操作*/
    public static Long getBetweenDateHour(Date startDate,Date endDate){
        Calendar cal=Calendar.getInstance();
        cal.setTime(startDate);
        long start = cal.getTimeInMillis();
        cal.setTime(endDate);
        long end = cal.getTimeInMillis();
        if (end>start){
            return (end-start)/(1000 * 3600);
        }
        return null;
    }

    /*计算俩个日期之间相差多少天*/
    /*调用完这个相减方法,后面的需要你先判断返回值是否为空,不为空才能继续后面的操作*/
    public static Long getBetweenDate(Date startDate,Date endDate){
        Calendar cal=Calendar.getInstance();
        cal.setTime(startDate);
        long start = cal.getTimeInMillis();
        cal.setTime(endDate);
        long end = cal.getTimeInMillis();
        if (end>start){
            return (end-start)/(1000 * 3600 * 24);
        }
        return null;
    }

    /**
     * 获取两个日期相差的月数
     */
    public static int getMonthbetweenDates(Date startDate, Date endDate) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(startDate);
        c2.setTime(endDate);
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        // 获取年的差值 
        int yearInterval = year1 - year2;
        // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
        if (month1 < month2 || month1 == month2 && day1 < day2) {
            yearInterval--;
        }
        // 获取月数差值
        int monthInterval = (month1 + 12) - month2;
        if (day1 < day2) {
            monthInterval--;
        }
        monthInterval %= 12;
        int months= Math.abs(yearInterval * 12 + monthInterval);
        return months;
    }

    /*将一个日期加上多少年*/
    public static Date getDateAddYear(Date date,int number){
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.YEAR,number);
        Date addYearDate = cal.getTime();
        return addYearDate;
    }

    /*将一个日期加上多少月*/
    public static Date getDateAddMonth(Date date,int number){
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH,number);
        Date addMonthDate = cal.getTime();
        return addMonthDate;
    }

    /*将一个日期加上多少天*/
    public static Date getDateAddDate(Date date,int number){
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE,number);
        Date addDate = cal.getTime();
        return addDate;
    }
    
    /*将Date日期转成 年-月-日 的形成*/
    public static String DateToStringyyyyMMdd(Date date){
        SimpleDateFormat spdf=new SimpleDateFormat("yyyy-MM-dd");
        return spdf.format(date);
    }

    /*将String日期转成 年-月-日:时-分-秒 的形成*/
    public static Date DateToStringyyyyMMddHHmmss(String date) throws ParseException {
        SimpleDateFormat spdf=new SimpleDateFormat("yyyy-MM-dd:HH-mm-ss");
        return spdf.parse(date);
    }

}

你可能感兴趣的:(Java学习)