zoukankan      html  css  js  c++  java
  • 字符串日期转换||字符串时间戳之间的相互转换

    //格式化成日期

    String date="2019-12-01";
    java.util.Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse(date);
    String now = new SimpleDateFormat("yyyy年MM月dd日").format(date2);
    System.out.println(now);

    /**
    *
    * @Description:将时间戳转换成日期
    * @Title: getDateDifference
    * @date 2019-09-26 12:01
    * @param @param timestamep
    * @param @return 参数
    * @return String 返回类型
    * @throws @return String
    * @param timestamep
    * @return
    */
    public static String getDifferenceDate(long timestamep) {
    String result1 = new SimpleDateFormat("yyyy-MM-dd").format(new Date(timestamep * 1000));
    return result1;
    }

    /**
    *
    * @Description: 根据当前时间参数得到后面第三天的一个时间戳
    * @Title: getDateDifference
    * @date 2019-09-26 11:54
    * @param @param date
    * @param @return
    * @param @throws ParseException 参数
    * @return String 返回类型
    * @throws @return String
    * @param date
    * @return
    * @throws ParseException
    */
    public static String getDateDifference2(String date) throws ParseException {
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    Date date2 = sdf1.parse(date);
    Calendar calendar1 = Calendar.getInstance();
    calendar1.setTime(date2);

    calendar1.add(Calendar.DAY_OF_MONTH, 3);
    String format = sdf1.format(calendar1.getTime());
    return getDateTimestamp(format);
    }
    /**
    *
    * @Description: 根据当前时间参数得到前面第三天的一个时间戳
    * @Title: getDateDifference
    * @date 2019-09-26 11:54
    * @param @param date
    * @param @return
    * @param @throws ParseException 参数
    * @return String 返回类型
    * @throws @return String
    * @param date
    * @return
    * @throws ParseException
    */
    public static String getDateDifference(String date) throws ParseException {
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    Date date2 = sdf1.parse(date);
    Calendar calendar1 = Calendar.getInstance();
    calendar1.setTime(date2);

    calendar1.add(Calendar.DAY_OF_MONTH, -3);
    String format = sdf1.format(calendar1.getTime());
    return getDateTimestamp(format);
    }

    /**
    *
    * @Description:将字符串时间转成String类型的时间戳
    * @Title: getDateDifference
    * @date 2019-09-26 11:04
    * @param @param date
    * @param @return 参数
    * @return String 返回类型
    * @throws @return String
    * @param date
    * @return
    */
    public static String getDateTimestamp(String date) {
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    try {
    Date date2 = sdf2.parse(date);
    long strLong = Long.parseLong(String.valueOf(date2.getTime() / 1000));
    return String.valueOf(strLong);
    } catch (ParseException e) {
    e.printStackTrace();
    }
    return "";
    }

    /**
    *
    * @Description: 根据时间得到月份
    * @Title: getSyq
    * @date 2019-11-02 01:21
    * @param @return 参数
    * @return Integer 返回类型
    * @throws
    * @return Integer
    * @return
    * @throws ParseException
    */
    public static Integer getSyq(String date1,String date2) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.setTime(sdf.parse(date1));
    int year1 = c.get(Calendar.YEAR);
    int month1 = c.get(Calendar.MONTH);
    c.setTime(sdf.parse(date2));
    int year2 = c.get(Calendar.YEAR);
    int month2 = c.get(Calendar.MONTH);

    int result;
    if(year1 == year2) {
    result = month1 - month2;
    } else {
    result = 12*(year1 - year2) + month1 - month2;
    }
    return result;
    }

    学着把生活的苦酒当成饮料一样慢慢品尝, 不论生命经过多少委屈和艰辛, 我们总是以一个朝气蓬勃的面孔, 醒来在每一个早上。
  • 相关阅读:
    manage.py migrate 报错
    pip安装django出错 Could not install packages due to an EnvironmentError: [Errno 13]
    利用beautifulsoup下载网页html代码中的css, js, img文件并保存
    itchat库微信自动回复祝福语
    关于阿里云Mysql分页查询不走索引的问题
    如何成为PHP程序员?
    Linux常用命令之scp
    PHP版本的区别与用法详解
    Linux常用命令之ftp
    Linux常用命令之权限管理
  • 原文地址:https://www.cnblogs.com/yhm9/p/11549352.html
Copyright © 2011-2022 走看看