package com.house365.web.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarUtil {
    /**
     * @param args
     */
//    public static void main(String[] args) {
//        
//        int time = formatDateTime(new Date());
//        System.out.println("time:"+time);
//        time = formatDateTime("2015-05-13 15:45:11");
//        System.out.println("time:"+time);
//        time = formatDateTime("2013-08-11 15:43:11");
//        System.out.println("time:"+time);
//    }
//    
    /**
     * 格式化时间
     * @param time
     * @return
     */
    public static int formatDateTime(Date date) {
        SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        
        Calendar current = Calendar.getInstance();
        current.setTime(date);
        Calendar today = Calendar.getInstance();    
        //今天
        today.set(Calendar.YEAR, today.get(Calendar.YEAR));
        today.set(Calendar.MONTH, today.get(Calendar.MONTH));
        today.set(Calendar.DAY_OF_MONTH,today.get(Calendar.DAY_OF_MONTH));
        today.set( Calendar.HOUR_OF_DAY, 0);
        today.set( Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        
        //获取当前月第一天：
        Calendar c = Calendar.getInstance();    
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        String first = format.format(c.getTime()).split(" ")[0]+" 00:00:00";
        try {
            Date date1 = format.parse(first);
            c.setTime(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();    
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));  
        String last = format.format(ca.getTime()).split(" ")[0]+" 23:59:59";
        try {
            Date date2 = format.parse(last);
            ca.setTime(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
//        String curr = format.format(current.getTime());
//        String to = format.format(today.getTime());
//        String c1 = format.format(c.getTime());
//        String ca1 = format.format(ca.getTime());
//        System.out.println("curr="+curr);
//        System.out.println("to="+to);
//        System.out.println("c1="+c1);
//        System.out.println("ca1="+ca1);

        if(current.after(today)){
            return 0;
        }else if(current.before(ca) && current.after(c)){
            return 1;
        }else{
            return 2;
        }
    }

}
