package com.cloudcross.ssp.common.utils;


import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 时间日期处理类
 */
public class DateUtils 
{
    public static final String DATEFORMATLONG = "yyyy-MM-dd HH:mm:ss";
    public static final String DATEFORMATMEDIUM = "yyyy-MM-dd HH:mm";
    public static final String DATEFORMATSHORT = "yyyy-MM-dd";
    private static Date date;
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
 
    public DateUtils()
    {
    }

    public static Date getCurrentDate()
    {
        return new Date();
    }

    public static long getCurrentTimeMillis()
    {
        return System.currentTimeMillis();
    }

    public static synchronized String getCurrentFormatDate(String formatDate)
    {
        date = getCurrentDate();
        simpleDateFormat.applyPattern(formatDate);
        return simpleDateFormat.format(date);
    }

    public static String getCurrentFormatDateLong()
    {
        return getCurrentFormatDate(DATEFORMATLONG);
    }
    
    public static String getCurrentFormatDateHour(){
    	
    	   return getCurrentFormatDate("yyyyMMddHH");
    }

    public static String getCurrentFormatDateMedium()
    {
        return getCurrentFormatDate(DATEFORMATMEDIUM);
    }

    public static String getCurrentFormatDateShort()
    {
        return getCurrentFormatDate(DATEFORMATSHORT);
    }

    private static synchronized String getDate2String(String format, Date date)
    {
        simpleDateFormat.applyPattern(format);
        return simpleDateFormat.format(date);
    }

    public static synchronized String getDateToString(String format, Date date)
    {
        simpleDateFormat.applyPattern(format);
        return simpleDateFormat.format(date);
    }
    public static String getDate2LongString(Date date)
    {
        return getDate2String(DATEFORMATLONG, date);
    }

    public static String getDate2MediumString(Date date)
    {
        return getDate2String(DATEFORMATMEDIUM, date);
    }

    public static String getDate2ShortString(Date date)
    {
        return getDate2String(DATEFORMATSHORT, date);
    }
    

    public static String getLong2LongString(long l)
    {
        date = getLong2Date(l);
        return getDate2LongString(date);
    }

    public static String getLong2MediumString(long l)
    {
        date = getLong2Date(l);
        return getDate2MediumString(date);
    }

    public static String getLong2ShortString(long l)
    {
        date = getLong2Date(l);
        return getDate2ShortString(date);
    }

    private static synchronized Date getString2Date(String format, String str)
    {
        simpleDateFormat.applyPattern(format);
        ParsePosition parseposition = new ParsePosition(0);
        return simpleDateFormat.parse(str, parseposition);
    }

    public static Date getString2LongDate(String str)
    {
        return getString2Date(DATEFORMATLONG, str);
    }

    public static Date getString2MediumDate(String str)
    {
        return getString2Date(DATEFORMATMEDIUM, str);
    }

    public static Date getString2ShortDate(String str)
    {
        return getString2Date(DATEFORMATSHORT, str);
    }
    
    /**
     * 用于建立一个空的日期对象,对象的默认值是1900-01-01
     * @return
     */
    public static Date getEmptyDate(){
    	return getString2ShortDate("1900-01-01"); 
    }
    
    /**
     * 用于获得当前指定年，月 的当月的总天数
     * @param y  年
     * @param m  月
     * @return 
     */
    public static int getMonths(int y,int m){
    	Calendar time=Calendar.getInstance(); 
    	time.clear(); 
    	time.set(Calendar.YEAR,y); 
    	time.set(Calendar.MONTH,m-1);//Calendar对象默认一月为0            
    	int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数	
    	return day;
    }
    
    /**
     * 用于建立一个空的日期对象,对象的默认值是1900-01-01
     * @return
     */
    public static String getEmptyDateString(){
    	return "1900-01-01";
    }

    public static Date getLong2Date(long l)
    {
        return new Date(l);
    }

    public static int getOffMinutes(long l)
    {
        return getOffMinutes(l, getCurrentTimeMillis());
    }

    public static int getOffMinutes(long from, long to)
    {
        return (int)((to - from) / 60000L);
    }

    public static int getYear()
    {
        return Calendar.getInstance().get(1);
    }

    public static int getMonth()
    {
        return Calendar.getInstance().get(2) + 1;
    }

    public static int getDay()
    {
        return Calendar.getInstance().get(5);
    }

    public static int getHour()
    {
        return Calendar.getInstance().get(11);
    }

    public static int getHour(long millis)
    {
    	Calendar.getInstance().setTimeInMillis(millis);
        return Calendar.getInstance().get(11);
    }
    
    public static int getMinute()
    {
        return Calendar.getInstance().get(12);
    }

    public static int getMinute(long millis)
    {
    	Calendar.getInstance().setTimeInMillis(millis);
    	
        return Calendar.getInstance().get(12);
    }
    
    public static int getSecond()
    {
        return Calendar.getInstance().get(13);
    }

    public static String add(int day){
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
    	gregorianCalendar.add(GregorianCalendar.DATE,day);
    	date = gregorianCalendar.getTime();
    	return getDate2String(DATEFORMATSHORT, date);

    }
    
    public static String subtract(int day){
    	GregorianCalendar gregorianCalendar = new GregorianCalendar();
    	gregorianCalendar.add(GregorianCalendar.DATE,-day);
    	date = gregorianCalendar.getTime();
    	return getDate2String(DATEFORMATSHORT, date);

    }
    
    public static String subtract(int day,String format){
    	GregorianCalendar gregorianCalendar = new GregorianCalendar();
    	gregorianCalendar.add(GregorianCalendar.DATE,-day);
    	date = gregorianCalendar.getTime();
    	return getDate2String(format, date);

    }
    
     public static boolean isShortDate(String str){
        	
    	 
         SimpleDateFormat   sdf   =   new   SimpleDateFormat("yyyy-MM-dd");   
         try   {   
             if   (sdf.format(sdf.parse(str)).equalsIgnoreCase(str))     
                 return true;   
             else     
            	 return false;   
           }   catch   (Exception   ex)   {   
        	   return false;   
           }
         /*
        boolean b =false;        	
        Date  date= null;                    
        simpleDateFormat.applyPattern(DATEFORMATSHORT);
        ParsePosition parseposition = new ParsePosition(0);
        try{
        	date=simpleDateFormat.parse(str, parseposition);
                     }
        catch(Exception e) {
                    	 
        	b =false;
                     }
        
        if (date==null){
        	b =false;
                     }
        else{
        	b=true;
                      }
        
        return b;
        */
        }
     
     
 	public static void main(String[] args) {
		String start= "2007-05-20"; 
        String end = "2007-06-10";
//        List dateList = getDateList(getString2Date("yyyy-MM-dd", start), getString2Date("yyyy-MM-dd", end));
//		System.out.println(dateList);
//        System.out.println("dateList's size is "+dateList.size());
//        System.out.println(getString2ShortDate(start));
        start="2001-06-88";
        System.out.println(isShortDate(start));
        
        String   tmp   =   "2005-02-15";   
        SimpleDateFormat   sdf   =   new   SimpleDateFormat("yyyy-MM-dd");   
        try   {   
            if   (sdf.format(sdf.parse(tmp)).equalsIgnoreCase(tmp))     
                System.out.println("OK");   
            else     
                System.out.println("Err");   
          }   catch   (Exception   ex)   {   
                System.out.println("Err");   
          }
 	}
    
    public static List getDateList(Date start, Date end) {
        List dateList = new ArrayList();
        Calendar s = Calendar.getInstance();
        s.setTime(start);
        Calendar e = Calendar.getInstance();
        e.setTime(end);
        while (!s.after(e)) {
            dateList.add(s.getTime());
            s.add(Calendar.DAY_OF_MONTH, 1);
        }
        return dateList;
    }

}
