zoukankan      html  css  js  c++  java
  • 日期转化类 ,日期格式处理

    package utile;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;

    import org.springframework.expression.ParseException;
    import org.springframework.validation.DefaultMessageCodesResolver.Format;

    public class TimeUtils {
    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年

    /**
    * @param @return 设定文件
    * @return Long 返回类型
    * @throws
    * @Title: getCurrentSystemTime
    * @Description: 获取当前系统时间
    */
    public static Long getCurrentSystemTime() {
    return Calendar.getInstance().getTimeInMillis();
    }

    /**
    * 取出当前的日期格式yyyy-MM-dd HH:mm:ss
    *
    * @return
    */
    public static String getCurrentTimeStr() {
    Calendar curCalendar = Calendar.getInstance();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return df.format(curCalendar.getTime());
    }

    /**
    * 将时间戳转成格式化时间
    *
    * @param milliseconds
    */
    public static String getFormatTime(long milliseconds) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    return sdf.format(new Date(milliseconds));
    }

    /**
    * 将时间戳转成格式化时间
    *
    * @param milliseconds
    */
    public static String getFormatTimeYY(long milliseconds) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(new Date(milliseconds));
    }


    /**
    * 返回文字描述的日期
    *
    * @param date
    * @return
    */
    public static String getTimeFormatText(long date) {
    if (date == 0) {
    return "";
    }
    date = date * 1000;
    long diff = new Date().getTime() - date;
    long r = 0;
    if (diff > year) {
    r = (diff / year);
    return r + "年前";
    }
    if (diff > month) {
    r = (diff / month);
    return r + "月前";
    }
    if (diff > day) {
    r = (diff / day);
    return r + "天前";
    }
    if (diff > hour) {
    r = (diff / hour);
    return r + "小时前";
    }
    if (diff > minute) {
    r = (diff / minute);
    return r + "分钟前";
    }
    return "刚刚";
    }

    /**
    * 将时间戳转成格式化时间
    *
    * @param milliseconds
    */
    public static String getFormatTimeHHMM(long milliseconds) {
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
    return sdf.format(new Date(milliseconds));
    }

    /**
    * 将时间戳转成格式化时间
    *
    * @param milliseconds
    */
    public static String getFormatTime24(long milliseconds) {
    SimpleDateFormat sdf = new SimpleDateFormat("kk:mm");
    return sdf.format(new Date(milliseconds));
    }

    /**
    * 计算两个日期之间相差的天数
    *
    * @param smdate 较小的时间
    * @param bdate 较大的时间
    * @return 相差天数
    * @throws ParseException
    * @throws java.text.ParseException
    */
    public static int daysBetween(Date smdate, Date bdate)
    throws ParseException, java.text.ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    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));
    }

    /**
    * 字符串的日期格式的计算
    * @throws java.text.ParseException
    */
    public static int daysBetween(String smdate, String bdate)
    throws ParseException, java.text.ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    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));
    }

    /**
    * 截取Date(1418784200000-0000)的固定长度
    */
    public static String spliteTime(String dateStr) {
    String sequence = dateStr.replace("/Date(", "");
    return sequence.substring(0, 13).trim();
    }

    /**
    * 获取分秒格式化字符串
    *
    * @param duration
    * @return
    */
    public static String getFormatMiniteSecString(int duration) {
    int minutes = duration % (60 * 60) / 60;//分钟时长
    int seconds = duration % 60;//秒时长
    return String.format("%d'%d''", minutes, seconds);
    }

    /**
    * 格式化时间字符串
    * <p/>
    * 显示规则大于1天,显示天. 大于1小时,显示1=小时. 大于1分钟, 显示分钟
    * 其中,大于7天以上的均显示7天前
    *
    * @param time
    * @return
    */
    public static String getFormatTimeString(long time) {

    Long currentTime = new Date().getTime() / 1000;//获得当前时间
    Long diffTime = currentTime - time;//当前时间减去创建时间,得到时间差
    long diffDay = diffTime / (24 * 3600); //得到天数
    long diffHour = diffTime % (24 * 3600) / 3600; //得到小时数
    long diffMinute = diffTime % 3600 / 60; //得到分钟数

    String result = null;

    if (diffDay >= 1) {
    if (diffDay >= 7) {
    result = "7天前";
    } else {
    result = diffDay + "天前";
    }
    } else if (diffHour >= 1) {
    result = diffHour + "小时前";
    } else if (diffMinute >= 1) {
    result = diffMinute + "分钟前";
    } else {
    result = "";
    }
    return result;
    }


    /**
    * 1.1
    * 将用户传递的时间类型转换为字符串类型
    * @Title: getStringDate
    * @Description:
    * @param date
    * @return
    * @return String
    * @throws
    @date 2018年6月4日 上午9:17:26
    */
    public static String getStringDate(Date date) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String currentTime = formatter.format(date);
    return currentTime;
    }
    /**
    * 1.2将标准字符串转化类型的时间 转化为Date类型
    * @Title: getStringToDate
    * @Description:
    * @param time
    * @return
    * @throws ParseException
    * @return Date
    * @throws java.text.ParseException
    * @throws
    @date 2018年8月8日 下午10:57:37
    */
    public static Date getStringToDate(String time) throws ParseException, java.text.ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
    Date date = sdf.parse(time);
    return date;
    }

    /**
    *2.1 等到年月日
    * @Title: getStringShortDate
    * @Description:
    * @param date
    * @return
    * @return String
    * @throws
    @date 2018年7月6日 下午1:27:15
    */
    public static String getStringShortDate(Date date) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String currentTime = formatter.format(date);
    return currentTime;
    }
    /**
    * 2.2 得到 年月
    * @Title: getStringYearMouthDate
    * @Description:
    * @param date
    * @return
    * @return String
    * @throws
    @date 2018年8月12日 上午9:58:49
    */
    public static String getStringYearMouthDate(Date date) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
    String currentTime = formatter.format(date);
    return currentTime;
    }
    /**
    * 3.计算两个日期相差的年数
    * @Title: getYearDisparity
    * @Description:
    * @param beginTime
    * @param endTime
    * @return
    * @throws Exception
    * @return String
    * @throws
    @date 2018年7月6日 下午2:20:40
    */
    public static String getYearDisparity(String beginTime,String endTime) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    Calendar bef = Calendar.getInstance();
    Calendar aft = Calendar.getInstance();
    bef.setTime(sdf.parse(beginTime));
    aft.setTime(sdf.parse(endTime));
    int year = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR));
    System.out.println(Math.abs(year));
    return year+"";
    }
    /**
    * 4.计算两个日期相差的月数
    * @Title: getMonthsDisparity
    * @Description:
    * @param beginTime
    * @param endTime
    * @return
    * @throws Exception
    * @return String
    * @throws
    @date 2018年7月6日 下午2:30:54
    */
    public static String getMonthsDisparity(String beginTime,String endTime) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    String str1 = "2011-02";
    String str2 = "2010-01";
    Calendar bef = Calendar.getInstance();
    Calendar aft = Calendar.getInstance();
    bef.setTime(sdf.parse(str1));
    aft.setTime(sdf.parse(str2));
    int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
    int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
    String months = ( result+month)+"";
    return months;
    }



    /**
    * 5.计算两个日期相差的天数
    * @Title: getMonthsDisparity
    * @Description:
    * @param beginTime
    * @param endTime
    * @return
    * @throws Exception
    * @return String
    * @throws
    @date 2018年7月6日 下午2:21:41
    */
    public static String getdaysDisparity(String beginTime,String endTime) throws Exception {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date date2 = format.parse(endTime);
    Date date = format.parse(beginTime);
    int days = (int) ((date2.getTime() - date.getTime()) / (1000*3600*24));
    return days+"";
    }
    /**
    * 6. 拿到现在时间加上(减去) d天后的时间 d是正数加 负数减去
    * @Title: getStringAfterOperationDay
    * @Description:
    * @param d
    * @return
    * @return String
    * @throws
    @date 2018年7月30日 上午11:18:39
    */

    public static String getStringAfterOperationDay(Integer d) {
    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.add(Calendar.DAY_OF_MONTH, d);// 昨天-1天
    Date yesterday = c.getTime();
    String yesterdayBegin = f.format(yesterday).toString();
    return yesterdayBegin;
    }
    /***
    * 7.1 获取距离今天 天 的0点时间 比如2018-07-30 00:00:000
    * @Title: getStringAfterOperationBeginDay
    * @Description:
    * @param d
    * @return
    * @return String
    * @throws
    @date 2018年7月30日 下午6:30:04
    */
    public static String getStringAfterOperationBeginDay(Integer d) {
    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.add(Calendar.DAY_OF_MONTH, d);// 正数 加
    Date yesterday = c.getTime();
    String beginTime = f.format(yesterday).toString()+" 00:00:00";
    return beginTime;
    }
    /**
    * 7.2 获取距离今天 天 的0点时间 比如2018-07-30 23:59:000
    * @Title: getStringAfterOperationEndDay
    * @Description:
    * @param d
    * @return
    * @return String
    * @throws
    @date 2018年7月30日 下午6:33:50
    */
    public static String getStringAfterOperationEndDay(Integer d) {
    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.add(Calendar.DAY_OF_MONTH, d);// 负数减
    Date yesterday = c.getTime();
    String endTime = f.format(yesterday).toString()+" 23:59:59";
    return endTime;
    }
    /**
    * 获取时间当前时间到毫秒
    * @return
    */
    public static String getssstime() {
    Date dat=new Date();
    SimpleDateFormat datf=new SimpleDateFormat("yyyyMMddHHmmSSS");
    return datf.format(dat).toString();
    }


    }

  • 相关阅读:
    iOS 宏(define)与常量(const)的正确使用
    Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法(转)
    如何在 GitHub 建立个人主页和项目演示页面
    Git 使用指南(cmd + gui)
    Google C++ 编码规范(中文版)
    SVN服务器搭建和使用
    演化理解 Android 异步加载图片(转)
    Android之断点续传下载(转)
    Android学习笔记——关于onConfigurationChanged(转)
    (转)Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
  • 原文地址:https://www.cnblogs.com/xianz666/p/12035614.html
Copyright © 2011-2022 走看看