常用公司时间转换第二弹

package com.ruipos.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class TimeUtils {

    /**
     * 获取指定日期的第二天
     */
    public static String getDateTomorrow(String time,String simpleDateFormat){
        if(StringUtils.isBlank(time)){
            return "";
        }

        String sdf = "";
        if(StringUtils.isBlank(simpleDateFormat)){
            sdf = "yyyy-MM-dd";
        }else{
            sdf = simpleDateFormat;
        }

        SimpleDateFormat s = new SimpleDateFormat(sdf);
        Calendar calendar = Calendar.getInstance();

        Date date = null;
        try
        {
            date = s.parse(time);
        } catch (ParseException e)
        {
            return "";
        }

        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE,day+1);
        String tomorrow = s.format(calendar.getTime());
        return tomorrow;
    }

    /**
     * 将指定时间转换为指定格式
     * @param time
     * @param simpleDateFormat
     * @return
     */
    public static String getTimeByFormat(String time,String simpleDateFormat){
        if(StringUtils.isBlank(time)){
            return "1";
        }

        String sdf = "";
        if(StringUtils.isBlank(simpleDateFormat)){
            sdf = "yyyy-MM-dd HH:mm:ss";
        }else{
            sdf = simpleDateFormat;
        }

        SimpleDateFormat s = new SimpleDateFormat(sdf);

        Date date = null;
        String timeString = "";
        try
        {
            date = s.parse(time);
            timeString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        } catch (ParseException e)
        {
            return "";
        }
        return timeString;
    }

    /**
     *@Description:日期转换,将接口返回的20180524转为2018-05-24
     *@param str 传递的日期字符串
     * auther: hjj
     */
    public static String dateConvertion(String str) {
        Date parse = null;
        String dateString = "";
        try {
            parse = new SimpleDateFormat("yyyyMMdd").parse(str);
            dateString = new SimpleDateFormat("yyyy-MM-dd").format(parse);
        } catch (ParseException e) {
            dateString=null;
        }

        return dateString;
    }
    //将yyyy-mm-dd 转成 yyyy-mm-00
    public static String formatDateStr(String inDateStr, String format) {
        String val = inDateStr;
        try {
            SimpleDateFormat f = new SimpleDateFormat(format);
            Date temp = f.parse(inDateStr);
            val = f.format(temp);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return val+"-00";
    }

    //将yyyy-mm-dd 转成 yyyy-mm
    public static String formatDateStrs(String inDateStr, String format) {
        String val = inDateStr;
        try {
            SimpleDateFormat f = new SimpleDateFormat(format);
            Date temp = f.parse(inDateStr);
            val = f.format(temp);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return val;
    }

    /**
     *
     * @return 返回一个日期减一天的字符串 类似于 2022-03-31 -1 = 2022-03-30
     */
    public static  String dayDeleteOne(String str)
    {
        Date date =new Date();
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        try
        {
            long dif = df.parse(str).getTime() - 86400 * 1000;//减一天
            date = new Date();
            date.setTime(dif);
            System.out.println("减少一天之后:" + df.format(date));
        } catch (ParseException e)
        {
            e.printStackTrace();
        }
        return df.format(date);
    }

    /**
     * 返回一个日期加一天的字符串 举个栗子  2022-03-30 +1 = 2022-03-31
     * @param str
     */
    public static String dayAddOne(String str){
        Calendar calendar = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        try {

            Date dd = df.parse(str);

            calendar = Calendar.getInstance();

            calendar.setTime(dd);

            calendar.add(Calendar.DAY_OF_MONTH, 1);//加一天

            System.out.println("增加一天之后:" + df.format(calendar.getTime()));

        } catch (ParseException e) {

            e.printStackTrace();

        }
        return df.format(calendar.getTime());
    }

    /**
     * 根据一段时间获取该段时间的所有日期  正序排序
     * @param startDate
     * @param endDate
     * @return
     */
    public static List getTwoDaysDayDes(String startDate, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List dateList = new ArrayList();
        try {
            Date dateOne = sdf.parse(startDate);
            Date dateTwo = sdf.parse(endDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateOne);
            dateList.add(startDate);
            while (dateTwo.after(calendar.getTime())) {
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                dateList.add(sdf.format(calendar.getTime()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateList;
    }

    /**
     * 根据一段时间获取该段时间的所有月份
     * @param minDate yyyy-MM
     * @param maxDate yyyy-MM
     * @return
     */
    public static List getMonthBetween(String minDate, String maxDate){
        ArrayList result = new ArrayList();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
            min.setTime(sdf.parse(minDate));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(sdf.parse(maxDate));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }
}

你可能感兴趣的:(java,java)