当前位置 : 主页 > 编程语言 > c++ >

java实现时间格式化

来源:互联网 收集:自由互联 发布时间:2021-06-30
DateFormate.txt import java.text.DateFormat;import java.text.DecimalFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.TimeZone;import org.slf4j.Logger;i
DateFormate.txt
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateSyncUtil {
	
	private static Logger logger = LoggerFactory.getLogger(DateSyncUtil.class);

	public static final String YYYY_MM_DD = "yyyy-MM-dd";

	public static final String YYYYMMddHHmmss = "yyyyMMddHHmmss";
	
	public static final String YYYY_MM_DDHHMMSS_UPPER = "yyyy-MM-dd HH:mm:ss";
	
	public static final String YYYY_MM_DDHHMMSS_LOWER = "yyyy-MM-dd hh:mm:ss";
	
	public static final String YYYYMMDD = "yyyyMMdd";
	
	public static final String YYYY_MM_DDTHHMMSSSSS000Z = "yyyy-MM-dd'T'HH:mm:ss.SSS000Z";
	
	public static final String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
	
	public static final String YYYY_MM_DDHHMM = "yyyy-MM-dd hh:mm";

	public static final String TIMEZONE = "GMT+8:00";

	private static SimpleDateFormat yyyy_mm_dd = new SimpleDateFormat(YYYY_MM_DD);
	private static SimpleDateFormat yyyymmddhhmmss = new SimpleDateFormat(YYYYMMddHHmmss);
	private static SimpleDateFormat yyyy_mm_ddhhmmss_upper = new SimpleDateFormat(YYYY_MM_DDHHMMSS_UPPER);
	private static SimpleDateFormat yyyy_mm_ddhhmmss_lower = new SimpleDateFormat(YYYY_MM_DDHHMMSS_LOWER);
	private static SimpleDateFormat yyyymmdd = new SimpleDateFormat(YYYYMMDD);
	private static SimpleDateFormat yyyy_mm_ddthhmmsssss000z = new SimpleDateFormat(YYYY_MM_DDTHHMMSSSSS000Z);
	private static SimpleDateFormat yyyymmddhhmmsssss = new SimpleDateFormat(YYYYMMDDHHMMSSSSS);
	private static SimpleDateFormat yyyy_mm_ddhhmm = new SimpleDateFormat(YYYY_MM_DDHHMM);
	
	public static Date parse(String source, String farmat) {
 
		Date parse = null;
		try {
			switch (farmat) {
			case "yyyy-MM-dd":
				synchronized (yyyy_mm_dd) {
					parse = yyyy_mm_dd.parse(source);
				}
				break;
			case "yyyyMMddHHmmss":
				synchronized (yyyymmddhhmmss) {
					parse = yyyymmddhhmmss.parse(source);
				}
				break;
			case "yyyy-MM-dd HH:mm:ss":
				synchronized (yyyy_mm_ddhhmmss_upper) {
					parse = yyyy_mm_ddhhmmss_upper.parse(source);
				}
				break;
			case "yyyy-MM-dd hh:mm:ss":
				synchronized (yyyy_mm_ddhhmmss_lower) {
					parse = yyyy_mm_ddhhmmss_lower.parse(source);
				}
				break;
			case "yyyyMMdd":
				synchronized (yyyymmdd) {
					parse = yyyymmdd.parse(source);
				}
				break;
			case "yyyy-MM-dd'T'HH:mm:ss.SSS000Z":
				synchronized (yyyy_mm_ddthhmmsssss000z) {
					parse = yyyy_mm_ddthhmmsssss000z.parse(source);
				}
				break;
			case "yyyyMMddHHmmssSSS":
				synchronized (yyyymmddhhmmsssss) {
					parse = yyyymmddhhmmsssss.parse(source);
				}
				break;
			case "yyyy-MM-dd hh:mm":
				synchronized (yyyy_mm_ddhhmm) {
					parse = yyyy_mm_ddhhmm.parse(source);
				}
				break;
			}
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
			//throw new BusinessException(ServiceConstant.SIMPLEDATEFORMAT_ERROR, source);
		}
		return parse;
	}
	
	public static Date parse(String source, String format, TimeZone timeZone) {

		Date parse = null;
		try {
			switch (format) {
			case "yyyy-MM-dd":
				synchronized (yyyy_mm_dd) {
					yyyy_mm_dd.setTimeZone(timeZone);
					parse = yyyy_mm_dd.parse(source);
				}
				break;
			case "yyyyMMddHHmmss":
				synchronized (yyyymmddhhmmss) {
					yyyymmddhhmmss.setTimeZone(timeZone);
					parse = yyyymmddhhmmss.parse(source);
				}
				break;
			case "yyyy-MM-dd HH:mm:ss":
				synchronized (yyyy_mm_ddhhmmss_upper) {
					yyyy_mm_ddhhmmss_upper.setTimeZone(timeZone);
					parse = yyyy_mm_ddhhmmss_upper.parse(source);
				}
				break;
			case "yyyy-MM-dd hh:mm:ss":
				synchronized (yyyy_mm_ddhhmmss_lower) {
					yyyy_mm_ddhhmmss_lower.setTimeZone(timeZone);
					parse = yyyy_mm_ddhhmmss_lower.parse(source);
				}
				break;
			case "yyyyMMdd":
				synchronized (yyyymmdd) {
					yyyymmdd.setTimeZone(timeZone);
					parse = yyyymmdd.parse(source);
				}
				break;
			case "yyyy-MM-dd'T'HH:mm:ss.SSS000Z":
				synchronized (yyyy_mm_ddthhmmsssss000z) {
					yyyy_mm_ddthhmmsssss000z.setTimeZone(timeZone);
					parse = yyyy_mm_ddthhmmsssss000z.parse(source);
				}
				break;
			case "yyyyMMddHHmmssSSS":
				synchronized (yyyymmddhhmmsssss) {
					yyyymmddhhmmsssss.setTimeZone(timeZone);
					parse = yyyymmddhhmmsssss.parse(source);
				}
				break;
			case "yyyy-MM-dd hh:mm":
				synchronized (yyyy_mm_ddhhmm) {
					yyyy_mm_ddhhmm.setTimeZone(timeZone);
					parse = yyyy_mm_ddhhmm.parse(source);
				}
				break;
			}
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
			//throw new BusinessException(ServiceConstant.SIMPLEDATEFORMAT_ERROR, source);
		}
		return parse;
	}

	public static String format(Date source, String format) {
		String result = null;
		switch (format) {
		case "yyyy-MM-dd":
			synchronized (yyyy_mm_dd) {
				result = yyyy_mm_dd.format(source);
			}
			break;
		case "yyyyMMddHHmmss":
			synchronized (yyyymmddhhmmss) {
				result = yyyymmddhhmmss.format(source);
			}
			break;
		case "yyyy-MM-dd HH:mm:ss":
			synchronized (yyyy_mm_ddhhmmss_upper) {
				result = yyyy_mm_ddhhmmss_upper.format(source);
			}
			break;
		case "yyyy-MM-dd hh:mm:ss":
			synchronized (yyyy_mm_ddhhmmss_lower) {
				result = yyyy_mm_ddhhmmss_lower.format(source);
			}
			break;
		case "yyyyMMdd":
			synchronized (yyyymmdd) {
				result = yyyymmdd.format(source);
			}
			break;
		case "yyyy-MM-dd'T'HH:mm:ss.SSS000Z":
			synchronized (yyyy_mm_ddthhmmsssss000z) {
				result = yyyy_mm_ddthhmmsssss000z.format(source);
			}
			break;
		case "yyyyMMddHHmmssSSS":
			synchronized (yyyymmddhhmmsssss) {
				result = yyyymmddhhmmsssss.format(source);
			}
			break;
		case "yyyy-MM-dd hh:mm":
			synchronized (yyyy_mm_ddhhmm) {
				result = yyyy_mm_ddhhmm.format(source);
			}
			break;
		}
		return result;
	}
	
	public static String format(Date source, String format ,TimeZone timeZone) {
		String result = null;
		switch (format) {
		case "yyyy-MM-dd":
			synchronized (yyyy_mm_dd) {
				yyyy_mm_dd.setTimeZone(timeZone);
				result = yyyy_mm_dd.format(source);
			}
			break;
		case "yyyyMMddHHmmss":
			synchronized (yyyymmddhhmmss) {
				yyyymmddhhmmss.setTimeZone(timeZone);
				result = yyyymmddhhmmss.format(source);
			}
			break;
		case "yyyy-MM-dd HH:mm:ss":
			synchronized (yyyy_mm_ddhhmmss_upper) {
				yyyy_mm_ddhhmmss_upper.setTimeZone(timeZone);
				result = yyyy_mm_ddhhmmss_upper.format(source);
			}
			break;
		case "yyyy-MM-dd hh:mm:ss":
			synchronized (yyyy_mm_ddhhmmss_lower) {
				yyyy_mm_ddhhmmss_lower.setTimeZone(timeZone);
				result = yyyy_mm_ddhhmmss_lower.format(source);
			}
			break;
		case "yyyyMMdd":
			synchronized (yyyymmdd) {
				yyyymmdd.setTimeZone(timeZone);
				result = yyyymmdd.format(source);
			}
			break;
		case "yyyy-MM-dd'T'HH:mm:ss.SSS000Z":
			synchronized (yyyy_mm_ddthhmmsssss000z) {
				yyyy_mm_ddthhmmsssss000z.setTimeZone(timeZone);
				result = yyyy_mm_ddthhmmsssss000z.format(source);
			}
			break;
		case "yyyyMMddHHmmssSSS":
			synchronized (yyyymmddhhmmsssss) {
				yyyymmddhhmmsssss.setTimeZone(timeZone);
				result = yyyymmddhhmmsssss.format(source);
			}
			break;
		case "yyyy-MM-dd hh:mm":
			synchronized (yyyy_mm_ddhhmm) {
				yyyy_mm_ddhhmm.setTimeZone(timeZone);
				result = yyyy_mm_ddhhmm.format(source);
			}
			break;
		}
		return result;
	}
	/**
	 * 转化毫秒为分钟保留一位小数
	 * 
	 * @param ss
	 * @return
	 */
	public static String formatSsTOMin(long ss) {
		double dd = (double) ss / (60 * 1000);
		DecimalFormat dfmt = new DecimalFormat("#.0");
		return Double.parseDouble(dfmt.format(dd)) + "";

	}
	/**
	 * 转换毫秒为秒
	 * @param ss
	 * @return
	 */
	public static String formatSsTOSec(long ss){
		long s = ss/1000;
		DecimalFormat dfmt = new DecimalFormat("#");
		return dfmt.format(s);
	}

	/**
	 * 根据格式转化毫秒秒为分钟
	 * 
	 * @param ss
	 * @return
	 */
	public static String formatMsTOMin(long ms, String format) {
		double min = (double) ms / (60 * 1000);
		DecimalFormat dfmt = new DecimalFormat("#.0");
		try {
			dfmt = new DecimalFormat(format);
		} catch (Exception e) {
			dfmt = new DecimalFormat("#.0");
		}
		return Double.parseDouble(dfmt.format(min)) + "";
	}

	/**
	 * 根据格式转化毫秒秒为分钟
	 * 
	 * @param ss
	 * @return
	 */
	public static String formatMsTOMin(double ms, String format) {
		double min = (double) ms / (60 * 1000);
		DecimalFormat dfmt = new DecimalFormat("#.0");
		try {
			dfmt = new DecimalFormat(format);
		} catch (Exception e) {
			dfmt = new DecimalFormat("#.0");
		}
		return Double.parseDouble(dfmt.format(min)) + "";
	}

	public static String formatMsTODate(long ms) {
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		formatter.setTimeZone(TimeZone.getTimeZone(TIMEZONE));
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(ms);
		return formatter.format(calendar.getTime());
	}

	public static String formatSqldatestrTODate(String str) {
		SimpleDateFormat f_timestamp = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		f_timestamp.setTimeZone(TimeZone.getTimeZone(TIMEZONE));
		java.sql.Timestamp timestamp = null;
		try {
			timestamp = java.sql.Timestamp.valueOf(str);
		} catch (Exception e) {
			return "";
		}
		return f_timestamp.format(timestamp);
	}

}
网友评论