java日期格式转换大全

public class DateFormatUtils {
	private static Log logger = LogFactory.getLog(DateFormatUtils.class);

	public static String formatDate(String formater,Date date){
		SimpleDateFormatformate = new SimpleDateFormat(formater);
		formate.format(date);
		return formate.format(date);
	}

	/**
	 *
	 * @Title:formatDateToCommon
	 * @Description: 通用时间转换类型
	 * @param date
	 * @return
	 */
	public static String formatDateToCommon(Date date){
		SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formate.format(date);
	}

	/**
	 *
	 * @Title:getSystemDate
	 * @Description: 获取系统当前时间
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getSystemDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(sdf.format(new Date()));
		} catch (ParseException e) {
			logger.error("", e);
		}
		return null ;
	}

	/**
	 *
	 * @Title:SystemDateFormatToCommon
	 * @Description: 获取系统当前时间
	 * @return
	 */
	public static String getSystemDateFormatToCommon(){
		SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formate.format(new Date());
	}

	public static String getSystemDateFormatToYYYYMMDD(){
		SimpleDateFormat formate = new SimpleDateFormat("yyyyMMdd");
		return formate.format(new Date());
	}

	public static String getSystemDateFormatToYYYYMMDDHHmmss(){
		SimpleDateFormat formate = new SimpleDateFormat("yyyyMMddHHmmss");
		return formate.format(new Date());
	}

	/**
	 *
	 * @Title:getFormatDateCommon
	 * @Description: 格式化时间
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getFormatDateCommon(Date date) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return sdf.parse(sdf.format(date));
		} catch (ParseException e) {
			logger.error("", e);
		}
		return null;
	}

	/**
	 *
	 * @Title:StringToDate
	 * @Description: 字符串转换成日期
	 * @param dateStr
	 * @param formatStr
	 * @return
	 * @throws ParseException
	 */
	public static Date StringToDate(String dateStr) throws ParseException {
		DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=null;
		date = sdf.parse(dateStr);
		return date;
	}

	public static Date StringToDate(String dateStr, String pattern){
		try{
			DateFormat sdf=new SimpleDateFormat(pattern);
			Date date = sdf.parse(dateStr);
			return date;
		}catch(ParseException ex){
			return null;
		}
	}

	/**
	 *
	 * @Title:fromDateStringToLong
	 * @Description: 获取字符串时间格式的毫秒数
	 * @param inVal
	 * @return
	 */
	public static long fromDateStringToLong(String inVal) {
		return fromDateStringToLong(inVal, "yyyy-MM-dd HH:mm:ss");
	}
	public static long fromDateStringToLong(String inVal,String format) {
		Date date = null; // 定义时间类型
		SimpleDateFormat inputFormat = new SimpleDateFormat(format);
		try {
			date = inputFormat.parse(inVal); // 将字符型转换成日期型
		} catch (Exception e) {
			logger.error("", e);
		}
		return date.getTime(); // 返回毫秒数
	}

	/**
	 *
	 * @Title:getMillForDateTimeDouble
	 * @Description: 获取两个时间之间的毫秒数
	 * @param inVal
	 * @return
	 */
	public static long getMillForDateTimeDouble(Date startTime, Date endTime) {
		long lTime = startTime.getTime();
		long eTime = endTime.getTime();
		long s = eTime - lTime ;
		return s;
	}

	/**
	 *
	 * @Title:formatDuring
	 * @Description: 将毫秒数转换为时分秒
	 * @param mss
	 * @return
	 */
	public static String formatDuring(long mss) {
	    long days = mss / (1000 * 60 * 60 * 24);
	    long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
	    long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
	    long seconds = (mss % (1000 * 60)) / 1000;
    	return days + " 天 " + hours + " 时 " + minutes + " 分 "
	            + seconds + " 秒 ";
	}  

	/**
     * 计算两个日期之间相差的天数
     * @param format yyyyMMdd
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(String format,Date smdate,Date bdate) throws ParseException
    {
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        smdate=sdf.parse(sdf.format(smdate));
        bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);  

       return Integer.parseInt(String.valueOf(between_days));
    }    

	/**
     * 计算两个日期之间相差的天数
     * 字符串的日期格式的计算
     * @param format yyyyMMdd
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(String format,String smdate,String bdate) throws ParseException{
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);  

       return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     *
     * @Title:getSystemAddMinute
     * @Description: 获取当前系统时间毫秒数 + n分钟后的时间
     * @param currentTimeMillis 当前系统时间毫秒数
     * @param minute 分
     * @return
     */
    public static String getSystemByCurrentTimeMillisAndMinute(long currentTimeMillis, int minute){
    	long currentTime = System.currentTimeMillis() + minute * 60 * 1000;
    	Date date = new Date(currentTime);
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
    }

    /**
     *
     * @Title:getDateStrByTimeMillis
     * @Description: 以字符串形式根据毫秒数获取时间
     * @param currentTimeMillis
     * @return
     */
    public static String getDateStrByTimeMillis(long currentTimeMillis){
    	Date date = new Date(currentTimeMillis);
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
    }

    /**
     *
     * @Title:getDateStrByTimeMillis
     * @Description: 以字符串形式根据毫秒数获取时间
     * @param currentTimeMillis
     * @return
     */
    public static Date getDateByTimeMillis(long currentTimeMillis){
    	Date date = new Date(currentTimeMillis);
    	return date;
    }

    /****
     * 传入具体日期 ,返回具体日期减一个月。
     *
     * @param date
     *            日期(2014-04-20)
     * @return 2014-03-20
     * @throws ParseException
     */
    public static String addMonth(String yearMonth) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Date dt = sdf.parse(yearMonth);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);  

        rightNow.add(Calendar.MONTH, +1);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);  

        return reStr;
    }

    /**
     *
     * @param dateStr   传入的日期字符串
     * @param formater  对传入日期和传出日期的格式化类型
     * @param monthCount 增加月份传入正值,减去月份传入负值
     * @return
     * @throws ParseException
     */
    public static String calMonth(String dateStr, String formater, int monthCount) throws ParseException{
    	SimpleDateFormat sdf = new SimpleDateFormat(formater);
        Date dt = sdf.parse(dateStr);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);

        rightNow.add(Calendar.MONTH, monthCount);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }
}

原文地址:https://www.cnblogs.com/wangyayun/p/11732224.html

时间: 2024-08-04 00:21:02

java日期格式转换大全的相关文章

JAVA时间格式转换大全

Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @return 返回时间类型 yyyy-MM-dd HH:mm:ss */ public static Date getNowDate() { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateForma

Java日期格式转换

Java时间格式转换大全 import java.text.*;import java.util.Calendar;public class VeDate {/**   * 获取现在时间   *    * @return 返回时间类型 yyyy-MM-dd HH:mm:ss   */public static Date getNowDate() {   Date currentTime = new Date();   SimpleDateFormat formatter = new Simple

java 日期格式转换,加减等

将string转换成日期Date SimpleDateFormat re = new SimpleDateFormat(String formate);----:yyyy-MM-dd System.out.println(re.parse("1990-02-02"));----Fri Feb 02 00:00:00 CST 1990 将日期转换成String SimpleDateFormat sfdate = new SimpleDateFormat(String formate);-

C#常用日期格式处理转换[C#日期格式转换大全

DateTime dt = DateTime.Now; Label1.Text = dt.ToString();//2005-11-5 13:21:25 Label2.Text = dt.ToFileTime().ToString();//127756416859912816 Label3.Text =dt.ToFileTimeUtc().ToString();//127756704859912816 Label4.Text =dt.ToLocalTime().ToString();//2005

java 日期格式转换EEE MMM dd HH:mm:ss z yyyy

SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");

Java时间日期格式转换 转自:http://www.cnblogs.com/edwardlauxh/archive/2010/03/21/1918615.html

Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @return 返回时间类型 yyyy-MM-dd HH:mm:ss */ public static Date getNowDate() { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateForma

java日期格式大全 format SimpleDateFormat

/**    * 字符串转换为java.util.Date<br>    * 支持格式为 yyyy.MM.dd G 'at' hh:mm:ss z 如 '2002-1-1 AD at 22:10:59 PSD'<br>    * yy/MM/dd HH:mm:ss 如 '2002/1/1 17:55:00'<br>    * yy/MM/dd HH:mm:ss pm 如 '2002/1/1 17:55:00 pm'<br>    * yy-MM-dd HH:

Java练习 SDUT-2246_时间日期格式转换

时间日期格式转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于日期的常用格式,在中国常采用格式的是"年年年年/月月/日日"或写为英语缩略表示的"yyyy/mm/dd",此次编程竞赛的启动日期"2010/11/20"就是符合这种格式的一个日期, 而北美所用的日期格式则为"月月/日日/年年年年"或"mm/dd /yyyy",

日期格式转换 java 2016-09-03T00:00:00.000+08:00

/**  * 日期格式转换yyyy-MM-dd'T'HH:mm:ss.SSSXXX  TO  yyyy-MM-dd HH:mm:ss  * @throws ParseException   */ public static String dealDateFormat(String oldDateStr) throws ParseException{  //此格式只有  jdk 1.7才支持  yyyy-MM-dd'T'HH:mm:ss.SSSXXX  DateFormat df = new Si