Java中的日期常用格式化

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

import org.apache.log4j.Logger;
 
public class Time {
public static 	Logger logger=Logger.getLogger(Time.class);
public static GregorianCalendar ggcalendar=new GregorianCalendar(Locale.CHINA);
	/**
	 * 
	 * @param datestr
	 * @return yyyy-MM-dd hh:mm:ss  
	 * @throws ParseException
	 */
	public static Date switchToyMdhms(String datestr) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date nowtime=null;
		try {
			if(datestr.trim().length()<11)
			{
				datestr=datestr.trim()+" 00:00:00";
				
			}
			nowtime = df.parse(datestr);
			nowtime = new java.sql.Timestamp(nowtime.getTime());
		} catch (ParseException e) {
			logger.info(e);
		}
		
		return nowtime;
	}

	/**
	 * 
	 * @param datestr
	 * @return yyyy-MM-dd 
	 * @throws ParseException
	 */
	public static Date switchToyMd(String datestr) throws ParseException {
		Date nowtime=null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		nowtime = df.parse(datestr);
		nowtime = new java.sql.Date(nowtime.getTime());
		return nowtime;
	}

	/**
	 * 
	 * @return yyyy-MM-dd
	 * @throws ParseException 
	 */
	public static Date getsystimeyMd() throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date2 = df.format(new Date());
		java.util.Date nowtime =null;
		nowtime= df.parse(date2);
		nowtime= new java.sql.Date(nowtime.getTime());
		return nowtime;
	}

	/**
	 * 
	 * @return yyyy-MM-dd hh:mm:ss
	 * @throws ParseException 
	 */
	public static Date getsystimeyMdhms()   {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date2 = df.format(new Date());
		java.util.Date nowtime =null;
		try {
			nowtime= df.parse(date2);
		} catch (ParseException e) {
			logger.info(e);
		}
		nowtime= new java.sql.Timestamp(nowtime.getTime());
		return nowtime;
	}
	
	/**
	 * 
	 * @return yyyy-MM-dd hh:mm:ss
	 * @throws ParseException 
	 */
	public static Date getTodayStart()   {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00");
		String date2 = df.format(new Date());
		java.util.Date nowtime =null;
		try {
			nowtime= df.parse(date2);
		} catch (ParseException e) {
			logger.info(e);
		}
		nowtime= new java.sql.Timestamp(nowtime.getTime());
		return nowtime;
	}
	/**
	 * 
	 * @return yyyy-MM-dd hh:mm:ss
	 * @throws ParseException 
	 */
	public static Date getTodayEnd() throws ParseException   {
		return switchToyMdhms(getsystimeyMd().toString()+" 23:59:59");
	}
	/**
	 *  
	 * @param date
	 * @return Stringyyyy-MM-dd
	 */
	public static String getFormatYYMMDD(Date date){
		if(date==null)
			return "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}
	/***
	 * 将Timestamp类型的数据转换成字符串
	 * @param date
	 * @return
	 */
	public static String getFormatYYYYMMDDHHMMSSS(Timestamp date){
//		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//hh:am/pm 中的小时数(1-12)
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//HH:一天中的小时数(0-23)
		return df.format(date);
	}
	public static String getFormatYYMMDD(){
		Date date=new Date();
		if(date==null)
			return "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(date);
	}
	/**
	 * 返回当前的年
	 * @return
	 */
	public static Integer year(){
		return ggcalendar.get(Calendar.YEAR);
	}
	/**
	 * 返回当前月份
	 * @return
	 */
	public static  Integer month(){
		return ggcalendar.get(Calendar.MONTH)+1;
	}
	
	/**
	 * 返回当前是几号
	 * @return
	 */
	public static  Integer date(){
		return ggcalendar.get(Calendar.DATE);
	}
	
	public static void main(String[] args) throws ParseException {
/*		Time.getsystimeyMd();
	Time.getsystimeyMdhms();
	Time.switchToyMd("2009-12-10");
	Time.switchToyMdhms("2008-02-02 13:25:03");*/
		System.out.println(Time.year().toString()+Time.month().toString()+Time.date().toString());
		Timestamp a=new Timestamp(new Date().getTime());
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(df1.format(a));
		SimpleDateFormat df = new SimpleDateFormat("E MM月dd日");
		String date = df.format(a);
		System.out.println(date);
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(a.getTime());
		cal.add(Calendar.DATE, 1);
		System.out.println(df.format(cal.getTime()));
		cal.add(Calendar.DATE, 1);
		System.out.println(df.format(cal.getTime()));
		cal.add(Calendar.DATE, 1);
		System.out.println(df.format(cal.getTime()));
		cal.add(Calendar.DATE, 1);
		System.out.println(df.format(cal.getTime()));
		cal.add(Calendar.DATE, 1);
		System.out.println(df.format(cal.getTime()));
		cal.add(Calendar.DATE, 1);
		System.out.println(df.format(cal.getTime()));
		System.out.println((new SimpleDateFormat("E")).format(cal.getTime()));
	}
}

猜你喜欢

转载自1017401036.iteye.com/blog/2232637
今日推荐