`
sunrenen
  • 浏览: 40179 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

实用j2ee DateUtil

阅读更多
package com.worthtech.app.util;

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

public class DateUtil {

	/**
	 * 获得当前时间,格式yyyy-MM-dd hh:mm:ss
	 * @param format
	 * @return
	 */
	public static String getCurrentDate(){
		return getCurrentDate("yyyy-MM-dd hh:mm:ss");
	}
	/**
	 * 获得当前时间,格式自定义
	 * @param format
	 * @return
	 */
	public static String getCurrentDate(String format){
		Calendar day=Calendar.getInstance(); 
		day.add(Calendar.DATE,0); 
		SimpleDateFormat sdf=new SimpleDateFormat(format);//"yyyy-MM-dd"
		String date = sdf.format(day.getTime());
		return date;
	}
	/**
	 * 获得昨天时间,格式自定义
	 * @param format
	 * @return
	 */
	public static String getYesterdayDate(String format){
		Calendar day=Calendar.getInstance(); 
		day.add(Calendar.DATE,-1); 
		SimpleDateFormat sdf=new SimpleDateFormat(format);//"yyyy-MM-dd"
		String date = sdf.format(day.getTime());
		return date;
	}
	 /**  
      * @param date1 需要比较的时间 不能为空(null),需要正确的日期格式 ,如:2009-09-12 
      * @param date2 被比较的时间  为空(null)则为当前时间  
      * @param stype 返回值类型   0为多少天,1为多少个月,2为多少年  
      * @return  
      * 举例:
      * compareDate("2009-09-12", null, 0);//比较天
      * compareDate("2009-09-12", null, 1);//比较月
      * compareDate("2009-09-12", null, 2);//比较年
      */ 
     public static int compareDate(String startDay,String endDay,int stype){  
         int n = 0;  
         String[] u = {"天","月","年"};  
         String formatStyle = stype==1?"yyyy-MM":"yyyy-MM-dd";  
           
         endDay = endDay==null?getCurrentDate("yyyy-MM-dd"):endDay;  
           
         DateFormat df = new SimpleDateFormat(formatStyle);  
         Calendar c1 = Calendar.getInstance();  
         Calendar c2 = Calendar.getInstance();  
         try {  
             c1.setTime(df.parse(startDay));  
             c2.setTime(df.parse(endDay));
         } catch (Exception e3) {  
             System.out.println("wrong occured");  
         }  
         //List list = new ArrayList();  
         while (!c1.after(c2)) {                   // 循环对比,直到相等,n 就是所要的结果  
             //list.add(df.format(c1.getTime()));    // 这里可以把间隔的日期存到数组中 打印出来  
             n++;  
             if(stype==1){  
                 c1.add(Calendar.MONTH, 1);          // 比较月份,月份+1  
             }  
             else{  
                 c1.add(Calendar.DATE, 1);           // 比较天数,日期+1  
             }  
         }  
         n = n-1;  
         if(stype==2){  
             n = (int)n/365;  
         }     
//         System.out.println(startDay+" -- "+endDay+" 相差多少"+u[stype]+":"+n);        
         return n;  
     }
     /**
      * 判断时间是否符合时间格式
      */
	public static boolean isDate(String date, String dateFormat) {
		if (date != null) {
			java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(dateFormat);
			format.setLenient(false);
			try {
				format.format(format.parse(date));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				return false;
			}
			return true;
		}
		return false;
	}
	 /**
	  * 实现给定某日期,判断是星期几
	  * date:必须yyyy-MM-dd格式
	  */
	 public static String getWeekday(String date){
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");  
     SimpleDateFormat sdw = new SimpleDateFormat("E");  
     Date d = null;  
     try {  
         d = sd.parse(date);  
     } catch (ParseException e) {  
         e.printStackTrace();  
     }
     return sdw.format(d);
	 }
	 /**
	  * 用来全局控制 上一周,本周,下一周的周数变化
	  */
	private static int weeks = 0;
	/**
	 * 获得当前日期与本周一相差的天数
	 */
	 private static int getMondayPlus() {
	     Calendar cd = Calendar.getInstance();
	     // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
	     int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
	     if (dayOfWeek == 1) {
	         return -6;
	     } else {
	         return 2 - dayOfWeek;
	     }
	 }
	 /**
	  * 获得本周星期一的日期
	  */
	 public static String getCurrentMonday(String format) {
	     weeks = 0;
	     int mondayPlus = getMondayPlus();
	     Calendar currentDate=Calendar.getInstance();
	     currentDate.add(Calendar.DATE, mondayPlus);
	     SimpleDateFormat sdf=new SimpleDateFormat(format);//"yyyy-MM-dd"
	     String date = sdf.format(currentDate.getTime());
	     return date;
	 }
	 /**
	  * 获得上周星期一的日期
	  */
	 public static String getPreviousMonday(String format) {
	     weeks--;
	     int mondayPlus = getMondayPlus();
	     Calendar currentDate=Calendar.getInstance();
	     currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);
	     SimpleDateFormat sdf=new SimpleDateFormat(format);//"yyyy-MM-dd"
	     String date = sdf.format(currentDate.getTime());
	     return date;
	 }
	 /**
	  * 获得下周星期一的日期
	  */
	 public static String getNextMonday(String format) {
	     weeks++;
	     int mondayPlus = getMondayPlus();
	//     GregorianCalendar currentDate = new GregorianCalendar();
	     Calendar currentDate=Calendar.getInstance();
	     currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);
	     SimpleDateFormat sdf=new SimpleDateFormat(format);//"yyyy-MM-dd"
	     String date = sdf.format(currentDate.getTime());
	     return date;
	 }
	 /**
	  * 获得相应周的周日的日期
	  * 此方法必须写在getCurrentMonday,getPreviousMonday或getNextMonday方法之后
	  */
	 public static String getSunday(String format) {
	     int mondayPlus = getMondayPlus();
	     Calendar currentDate=Calendar.getInstance();
	     currentDate.add(Calendar.DATE, mondayPlus + 7 * weeks + 6);
	     SimpleDateFormat sdf=new SimpleDateFormat(format);//"yyyy-MM-dd"
	     String date = sdf.format(currentDate.getTime());
	     return date;
	 }


/**
	  *method 将字符串类型的日期转换为一个timestamp(时间戳记java.sql.Timestamp)
	  *@param dateString 需要转换为timestamp的字符串
	  *@return dataTime timestamp
	  */
	public final static java.sql.Timestamp string2Time(String dateString) {
		DateFormat dateFormat;
		dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);// 设定格式
		dateFormat.setLenient(false);
		java.util.Date date = null;
		try {
			date = dateFormat.parse(dateString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		java.sql.Timestamp dateTime = new java.sql.Timestamp(date.getTime());
		return new java.sql.Timestamp(date.getTime());// Timestamp类型,timeDate.getTime()返回一个long型
	}

	/**
	 *method 将字符串类型的日期转换为一个Date(java.sql.Date)
	 * 
	 * @param dateString
	 *            需要转换为Date的字符串
	 *@return dataTime Date
	 */
	public final static java.sql.Date string2Date(String dateString) {
		DateFormat dateFormat;
		dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
		dateFormat.setLenient(false);
		java.util.Date date = null;
		try {
			date = dateFormat.parse(dateString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		java.sql.Date dateTime = new java.sql.Date(date.getTime());// sql类型
		return new java.sql.Date(date.getTime());
	}

//记录考勤, 记录迟到、早退时间
public static String getState() {   
        String state = "正常";   
        DateFormat df = new SimpleDateFormat("HH:mm:ss");   
        Date d = new Date();   
        try {   
            Date d1 = df.parse("08:00:00");   
            Date d2 = df.parse(df.format(d));   
            Date d3 = df.parse("18:00:00");   
  
            int t1 = (int) d1.getTime();   
            int t2 = (int) d2.getTime();   
            int t3 = (int) d3.getTime();   
            if (t2 < t1) {   
  
                long between = (t1 - t2) / 1000;// 除以1000是为了转换成秒   
                long hour1 = between % (24 * 3600) / 3600;   
                long minute1 = between % 3600 / 60;   
  
                state = "迟到 :" + hour1 + "时" + minute1 + "分";   
  
            } else if (t2 < t3) {   
                long between = (t3 - t2) / 1000;// 除以1000是为了转换成秒   
                long hour1 = between % (24 * 3600) / 3600;   
                long minute1 = between % 3600 / 60;   
                state = "早退 :" + hour1 + "时" + minute1 + "分";   
            }   
            return state;   
        } catch (Exception e) {   
            return state;   
        }   
  
    }   
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

        String s = getState();   
        System.out.println(s);
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics