将毫秒转成日期格式yyyy-MM-dd HH:mm:ss 整理的时间格式的转换

最近工作经常碰到传输时,日期格式的要求,整理最近用到的时间格的转换打包成工具类

1 将毫秒字转换成日期格式yyyy-MM-dd HH:mm:ss
2 将日期格式yyyy-MM-dd HH:mm:ss转成毫秒
3 将日期格式yyyyMMddHHmmss转成毫秒
4 将毫秒转成时间串yyyyMMddHHmmss
5 将日期格式yyyyMMddHHmmss转成将日期格式yyyy-MM-dd HH:mm:ss
6 将日期格式yyyy-MM-dd HH:mm:ss转成将日期格式yyyyMMddHHmmss
7 计算两个时间的间隔,返回分钟数值,不足一分钟舍去,计算的时间为标准日期格式yyyy-MM-dd HH:mm:ss
8 将分钟数值转成时间字符串

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

public class TimeChangeUtils {
    /**
     * 将毫秒字符串转成时间格式yyyy-MM-dd HH:mm:ss
     * @param time
     * @return
     */
    public static String getTimeFormat(String timeStr) {
        long time=Long.parseLong(timeStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        String monthStr = addZero(month);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String dayStr = addZero(day);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);//24小时制
        String hourStr = addZero(hour);
        int minute = calendar.get(Calendar.MINUTE);
        String minuteStr = addZero(minute);
        int second = calendar.get(Calendar.SECOND);
        String secondStr =addZero(second);
        return(year + "-" + monthStr  + "-" + dayStr + " "
                + hourStr + ":" + minuteStr + ":" + secondStr);
    }
    private static String addZero(int param) {
        String paramStr= param<10 ? "0"+param : "" + param ;
        return paramStr;
    }
    /**
     * 将时间格式yyyy-MM-dd HH:mm:ss转成毫秒
     * @param time
     * @return
     * @throws ParseException 
     */
    public static Long getTimeFormat2mill(String timeStr) throws ParseException {
         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         Date date = simpleDateFormat.parse(timeStr);
         long ts = date.getTime();
         return ts;
    }
    /**
     * 将时间格式yyyyMMddHHmmss转成毫秒
     * @param time
     * @return
     * @throws ParseException 
     */
    public static Long getTimeString2mill(String timeStr) throws ParseException {
         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
         Date date = simpleDateFormat.parse(timeStr);
         long ts = date.getTime();
         return ts;
    }
    /**
     * 将毫秒转成时间串yyyyMMddHHmmss
     * @param time
     * @return
     * @throws ParseException 
     */
    public static String getmill2TimeString(String timeStr) throws ParseException {
         return getTimeFormat(timeStr).replaceAll("-", "").replaceAll(" ", "").replaceAll(":", "");
    }
    /**
     * 将时间格式yyyyMMddHHmmss转成将时间格式yyyy-MM-dd HH:mm:ss
     * @param time
     * @return
     * @throws ParseException 
     */
    public static String getTimeString2TimeFormat(String timeStr) throws ParseException {
         return getTimeFormat(""+getTimeString2mill(timeStr));
    }
    /**
     * 将时间格式yyyy-MM-dd HH:mm:ss转成将时间格式yyyyMMddHHmmss
     * @param time
     * @return
     * @throws ParseException 
     */
    public static String getTimeFormat2TimeString(String timeFormat) throws ParseException {
      return getmill2TimeString(""+getTimeFormat2mill(timeFormat)); } /** * 计算两个时间的间隔,返回分钟数值,不足一分钟舍去,计算的时间为标准时间格式yyyy-MM-dd HH:mm:ss * @param time * @return */ public static String getIntevalTime(String startTime,String endTime) throws ParseException { long intevaltime=getTimeFormat2mill(endTime)-getTimeFormat2mill(startTime); return ""+intevaltime/60000; } /** * 将分钟数值转成时间字符串 * @param time * @return */ public static String minute2Hour(String min){ int num=Integer.parseInt(min); int hour=num/60; int minute=num%60; if (hour==0) { return minute+"分钟"; } return hour+"小时"+minute+"分钟"; } }

 

posted @ 2018-09-13 13:20  不爱熬夜的猫  阅读(15712)  评论(0编辑  收藏  举报