java的日期处理

private void getTime(Companyissue company,HttpSession session){
String date1=company.getStartTime();
Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date2 = formatter.format(currentTime);
        boolean flg= false;
        Date d1= null;
        Date d2 = null;
        try {
d1=formatter.parse(date1);
d2=formatter.parse(date2);
} catch (ParseException e) {
e.printStackTrace();

        DateFormat df = DateFormat.getDateTimeInstance();
        try {
flg =df.parse(date1).before(df.parse(date2));
} catch (ParseException e) {
e.printStackTrace();
}
        if(flg==true){
        try {
        String str = "距离展会开始还有"+daysBetween(d1,d2)+"天";
session.setAttribute("sartTime", str);
} catch (ParseException e) {

e.printStackTrace();
}
        }else{
        session.setAttribute("startTime", "展会已结束");
        }
       
}


    public static int daysBetween(Date smdate,Date bdate) throws ParseException   
    {   
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
        smdate=sdf.parse(sdf.format(smdate)); 
        bdate=sdf.parse(sdf.format(bdate)); 
        Calendar cal = Calendar.getInstance();   
        cal.setTime(smdate);   
        long time1 = cal.getTimeInMillis();                
        cal.setTime(bdate);   
        long time2 = cal.getTimeInMillis();        
        long between_days=(time2-time1)/(1000*3600*24); 
           
       return Integer.parseInt(String.valueOf(between_days));          
    }

你可能感兴趣的:(java)