Java中日期操作

package com.zx.common.util;

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

public class DateUtil {
   private static final String FORMAT="yyyy-MM-dd HH:mm:ss";
	
   public static Date getTime(){
		Calendar now=Calendar.getInstance();
		return now.getTime();
	}
	
	public static String formatTime(Date time){
		SimpleDateFormat sdf=new SimpleDateFormat(FORMAT);
		return sdf.format(time);
	}
	
	public static String getCurrentTime(){
		Calendar now=Calendar.getInstance();
		StringBuffer time=new StringBuffer();
		time.append(now.get(now.YEAR));
		time.append(now.get(now.MONTH)+1);
		time.append(now.get(now.DAY_OF_MONTH));
		time.append(now.get(now.HOUR));
		time.append(now.get(now.MINUTE));
		time.append(now.get(now.SECOND));
		
		return time.toString();
		
	}

/**
	 * 判断是否在发短信的时间范围内(8:00-23:00) (penghuaiyi 2008-12-22)
	 * @return
	 */
	public static boolean  isSendTime(){
		boolean flag=false;
		
		Calendar current=Calendar.getInstance();
		int year=current.get(Calendar.YEAR);
		int month=current.get(Calendar.MONTH);
		int date=current.get(Calendar.DAY_OF_MONTH);
	    //设置短信发送开始时间:8:00
		current.set(year, month, date, BEGIN_TIME,0,0);
		Date beginTime=current.getTime();
		//设置短信发送截止时间:23:00
		current.set(year, month, date, END_TIME,0,0);
		Date endTime=current.getTime();
		
		Date now=new Date();  //当前时间
		if(now.after(beginTime) && now.before(endTime))
		{
			flag=true;
		}
		
		return flag;
	}
	
	
	public static void main(String args[]){
	       Date date=DateUtil.getTime();
	       String time=DateUtil.formatTime(date);
	       System.out.println(time);
	       
	       String str=DateUtil.getCurrentTime();
	       System.out.println(str);
	}
}

你可能感兴趣的:(java)