首页>代码>java date 时间处理类>/1017817702319104.java
package com.azcctl.zdjcx.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateTime {
	public static final SimpleDateFormat FORMAT = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
	
	/**
	 * 比较两日期大小
	 * @param d1
	 * @param d2
	 * @return
	 */
	
	
	/*
	 * 
	 * --取得当天0时0分0秒
select TRUNC(SYSDATE) FROM dual;

--取得当天23时59分59秒(在当天0时0分0秒的基础上加1天后再减1秒)
SELECT TRUNC(SYSDATE)+1-1/86400 FROM dual;

--取得当前日期是一个星期中的第几天,注意:星期日是第一天
select to_char(sysdate,'D'),to_char(sysdate,'DAY') from dual;

--在oracle中如何得到当天月份的第一天和最后一天
select to_char(sysdate,'yyyy-mm')||'-01' firstday,to_char(last_day(sysdate),'yyyy-mm-dd') lastday from dual

--实现当天16点前数据减去昨天16点的,过了16点减去今天16点
select field1, field2 from mytable
where time = decode(sign(sysdate-trunc(sysdate)-16/24), --时间判断
                     -1,trunc(sysdate)-1+16/24, --小于16点取昨天16点
                        trunc(sysdate)+16/24)  --大于16点取当天16点 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 */
	public static int compare(Date d1, Date d2) {   
        String str1 = FORMAT.format(d1);   
        							System.out.println("str1: " + str1);   
        String str2 = FORMAT.format(d2);   
        							System.out.println("str2: " + str2);   
  
        int result = str1.compareTo(str2);
        
        return result;
        /*if (result > 0) {   
            System.out.println(str1 + " 晚于 " + str2);   
        } else if (result == 0) {   
            System.out.println(str1 + " 等于 " + str2);   
        } else {   
            System.out.println(str1 + " 早于 " + str2);   
        }   */
    } 
	
	/**
	 * 获取当天23:59:59
	 * @return
	 */
	public static Date getBeforZeroTime() {
		Date beforZeroDate = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, 0);
		try {
			beforZeroDate = sdf2.parse(sdf.format(c.getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return beforZeroDate;
	}
	
	/**
	 * 获取当天00:00:00
	 * @return
	 */
	public static Date getZeroTime() {
		Date zeroDate = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, 0);
		try {
			zeroDate = sdf.parse(sdf.format(c.getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return zeroDate;
	}
	
	/**
	 * 输入时、分、秒,转换为小时返回
	 * @param Hour
	 * @param minutes
	 * @param sec
	 * @return
	 */
	public static double getHours(double Hour,double minutes,double sec) {
		double hours = Hour+minutes/60+sec/3600;
		return hours;
	}
	/*
	 * 得到当前时间 
	 */
	public static String  getNowTime(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Timestamp nowTimestamp = new Timestamp(new Date().getTime());
		String nowTime = sdf.format(new java.util.Date(nowTimestamp.getTime()));
		return nowTime;
	}
	/*
	 * 得到当前时间 
	 */
	public static String  getNowDate(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Timestamp nowTimestamp = new Timestamp(new Date().getTime());
		String nowTime = sdf.format(new java.util.Date(nowTimestamp.getTime()));
		return nowTime;
	}
	/*
	 * formate date
	 */
	public static String getFormatDate(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
		String dateStr = sdf.format(date);
		return dateStr;
	}
	
	
    public static String getTimeSS(){
    	String str = "";    
	    SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");        
	    Calendar lastDate = Calendar.getInstance();    
	    str=sdf.format(lastDate.getTime());    
	    return str;  
    }
	/*
	 * string to date 
	 */
	public static Date StrToDate(String str){
		
		SimpleDateFormat sdf = null;
		Date date = null;
		sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return date;
	}
	
	  public static Date stringToDate(String str) { 
	        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
	        Date date = null; 
	        try { 
	            // Fri Feb 24 00:00:00 CST 2012 
	            date = format.parse(str);  
	        } catch (ParseException e) { 
	            e.printStackTrace(); 
	        } 
	        // 2012-02-24 
	        //date = java.sql.Date.valueOf(str); 
	                                              
	        return date; 
	    } 
	  public  String  getCurrentDate(){
	    	 Date date = new Date();  
		    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		    String  years= sdf.format(date);
		    return years;    
	  }
	  
	 // 2011-04-13 09:41 java 判断今天 昨天 前天... 
	  public String parseDate(String createTime){    
	        try {
	            String ret = "";
	            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	            long create = sdf.parse(createTime).getTime();
	            Calendar now = Calendar.getInstance();
	            long ms  = 1000*(now.get(Calendar.HOUR_OF_DAY)*3600+now.get(Calendar.MINUTE)*60+now.get(Calendar.SECOND));//毫秒数
	            long ms_now = now.getTimeInMillis();
	            if(ms_now-create<ms){
	                ret = "今天";
	            }else if(ms_now-create<(ms+24*3600*1000)){
	                ret = "昨天";
	            }else if(ms_now-create<(ms+24*3600*1000*2)){
	                ret = "前天";
	            }else{
	                ret= "更早";
	            }
	            return ret;
	            } catch (Exception e) {
	            e.printStackTrace();
	            }
	        return null;
	    }


}
最近下载更多
你是傻子  LV9 2021年1月20日
最代码官方  LV167 2012年10月23日
最近浏览更多
1358849392  LV21 2021年10月27日
你是傻子  LV9 2021年1月20日
fashion1314  LV5 2020年5月26日
晴风8815  LV2 2020年4月20日
fpxrng  LV9 2019年12月12日
二十一颜  LV2 2019年11月25日
xp9522  LV9 2019年2月10日
fly666  LV11 2018年9月27日
kong.yee  LV40 2018年9月17日
eeehhh  LV3 2018年8月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友