mikuriya

日期格式化 工具类

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

public class DateFormat {
    public static final String DATE_FORMAT_A="yyyy-MM-dd HH:mm:ss";

    /**
     * 将int类型数字转换成时分秒毫秒的格式数据
     *
     * @param time long类型的数据
     * @return HH:mm:ss.SSS
     * @author zero 2019/04/11
     */
    public static String msecToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        int millisecond = 0;
        if (time <= 0) {
            return "00:00:00.000";
        } else {
            second = time / 1000;
            minute = second / 60;
            millisecond = time % 1000;
            if (second < 60) {
                timeStr = "00:" + unitFormat(second);
            } else if (minute < 60) {
                second = second % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {// 数字>=3600 000的时候
                hour = minute / 60;
                minute = minute % 60;
                second = second - hour * 3600 - minute * 60;
                timeStr =unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String msecToTime2(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        int millisecond = 0;
        if (time <= 0) {
            return "00:00:00.000";
        } else {
            second = time / 1000;
            minute = second / 60;
            millisecond = time % 1000;
            if (second < 60) {
                timeStr = "00:00:" + unitFormat(second);
            } else if (minute < 60) {
                second = second % 60;
                timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second);
            } else {// 数字>=3600 000的时候
                hour = minute / 60;
                minute = minute % 60;
                second = second - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }


    //时分秒的格式转换
    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10) {
            retStr = "0" + Integer.toString(i);
        }
        else {
            retStr = "" + i;
        }
        return retStr;
    }

    //毫秒的格式转换
    public static String unitFormat2(int i) {
        String retStr = null;
        if (i >= 0 && i < 10) {
            retStr = "00" + Integer.toString(i);
        }
        else if (i >= 10 && i < 100) {
            retStr = "0" + Integer.toString(i);
        } else {
            retStr = "" + i;
        }
        return retStr;
    }


    /**
     *  转换格式  00:00:00
     * @param time
     * @return
     */
    public static String tranToThreeString(int time){
        String paramRet1 = "00:00:00";
        String paramRet2 = "00:00:01";
        String hour = "00";
        String minute = "00";
        String second = "00";
        if(time <= 0){
            return paramRet1;
        }else if( time < 1000 ){
            return paramRet2;
        }else{
            int tmpSecond = time / 1000;
            int remainder = time % 1000;
            if(tmpSecond >= 60){
                // 计算分钟
                int tmpMinute = tmpSecond / 60;
                if(tmpMinute >= 60){
                    // 计算小时
                    int tmpHour = tmpMinute / 60;
                    int mpMinute = tmpMinute % 60;
                    int mmpSecond = mpMinute % 60;
                    return twoString(tmpHour) + ":" + twoString(mpMinute) + ":" +twoString(remainder > 0 && (mmpSecond + 1) != 60? mmpSecond + 1 : mmpSecond);
                }
                int mpSecond = tmpSecond % 60;
                // 分钟不够一小时
                return hour + ":" + twoString(tmpMinute) + ":" + twoString(remainder > 0  && (mpSecond + 1) != 60 ? mpSecond + 1 : mpSecond);
            }
            // 秒不够一分钟
            second = twoString(remainder > 0  && (tmpSecond + 1) != 60 ? tmpSecond + 1 : tmpSecond);;
        }
        // 默认返回结果
        return hour + ":" + minute + ":" + second;
    }


    /**
     *  转换格式  00:00
     * @param time
     * @return
     */
    public static String tranToTwoString(int time)  {
        String paramRet1 = "00:00";
        String paramRet2 = "00:01";
        String minute = "00";
        String second = "00";
        if(time <= 0){
            return paramRet1;
        }else if( time < 1000 ){
            return paramRet2;
        }else{
            int tmpSecond = time / 1000;
            int remainder = time % 1000;
            if(tmpSecond >= 60){
                // 计算分钟
                int tmpMinute = tmpSecond / 60;
                int mpSecond = tmpSecond % 60;
//                if(tmpMinute > 60){
//                    throw new Exception("分钟数大于60分钟,本api不支持");  throws Exception
//                }
                // 分钟不够一小时
                return twoString(tmpMinute) + ":" + twoString(mpSecond);
            }
            // 秒不够一分钟
            second = twoString(tmpSecond);
        }
        // 默认返回结果
        return minute + ":" + second;
    }


    private static String twoString(int time){
        String tmp =  String.valueOf(time);
        if(tmp.length() > 1){
            return tmp;
        }
        return "0" + tmp;
    }


    public static Integer computerBar(int num1,int num2){
        String result = "0";
        Integer integer = new Integer(0);
        try{
            DecimalFormat df = new DecimalFormat("#.00");
            String format = df.format(Double.valueOf(num1) / Double.valueOf(num2));
            result = format.substring(format.lastIndexOf(".")+1);
            if("00".equals(result) && num1 !=0){
                return 100;
            }else if("00".equals(result) && num1 == 0){
                return 0;
            }
            integer = Integer.valueOf(result);
        }catch (Exception e){
            return 0;
        }
       return integer;
    }

    public static String date2StringFormat(Date date, String format) {
        if((format == null || format.isEmpty())){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static Date sting2DateFormat(String date, String format) throws ParseException {
        if((format == null || format.isEmpty())){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(date);
    }


    public static String getDataBeforeYear(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        String year = format.format(y);
        return  year;
    }
}

 

posted on 2021-02-25 16:42  mikuriya  阅读(87)  评论(0编辑  收藏  举报

导航