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

    学着把生活的苦酒当成饮料一样慢慢品尝, 不论生命经过多少委屈和艰辛, 我们总是以一个朝气蓬勃的面孔, 醒来在每一个早上。
  • 相关阅读:
    UIWindow与UIView
    UIView与CALayer 区别
    setter getter 方法
    KVC、KVO 理解
    c语言实现单链表
    浅谈C的应用与常见error
    POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)
    Google Code Jam 2008 Round 1A C Numbers(矩阵快速幂+化简方程,好题)
    POJ 3686 The Windy's(思维+费用流好题)
    POJ 2686 Traveling by Stagecoach(状压二维SPFA)
  • 原文地址:https://www.cnblogs.com/yhm9/p/11549352.html
Copyright © 2011-2022 走看看