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;
    }

    学着把生活的苦酒当成饮料一样慢慢品尝, 不论生命经过多少委屈和艰辛, 我们总是以一个朝气蓬勃的面孔, 醒来在每一个早上。
  • 相关阅读:
    【百度地图API】让用户选择起点和终点的驾车导航
    JS解决通过按钮切换图片的问题
    JavaScript (JS)基础:DOM 浅析 (含数组Array、字符串String基本方法解析)
    JavaScript (JS)基础:ECMAScript 浅析 (含Math基本方法解析)
    感谢Sylvia的技术支持
    0904 存储过程、触发器、事务、视图、生成脚本
    0903 连接查询
    0901 子查询
    0831 模糊查询,排序查询,聚合函数,时间日期函数,数学函数,字符串函数
    0829 数据库的增删改查
  • 原文地址:https://www.cnblogs.com/yhm9/p/11549352.html
Copyright © 2011-2022 走看看