zoukankan      html  css  js  c++  java
  • java时间处理,获取当前时间的小时,天,本周周几,本周周一的日期,本月一号的日期

    1、时间转时间戳

    public static long strToTimestamp(String dateTimeStr) throws Exception {
            Timestamp time = Timestamp.valueOf(dateTimeStr);
            return time.getTime();
        }

    2、时间戳转时间

    public static String timestampToStr(long timestamp) throws Exception {
            Timestamp ts = new Timestamp(timestamp);
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(ts);
        }

    3、时间转换

        public static Map strTimeTomap(String dateTimeStr) throws Exception {
    //        Timestamp ts = new Timestamp(timestamp);
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DateFormat dsdf = new SimpleDateFormat("yyyy-MM-dd");
    
            Calendar cal = Calendar.getInstance();
    
            Date date = sdf.parse(dateTimeStr);
            cal.setTime(date);
            String week_day = String.valueOf(cal.get(Calendar.DAY_OF_WEEK) - 1);
            String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
    
            cal.set(Calendar.DAY_OF_MONTH, 1);
            String first_day_month = dsdf.format(cal.getTime());
    //        System.out.println(first_day_month);
    
            cal.setTime(date);
            cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            String first_day_week = dsdf.format(cal.getTime());
    //        System.out.println(first_day_week);
    
            String day = dsdf.format(date);
            Map map = new HashMap<String,String>();
            map.put("hour",hour);
            map.put("day",day);
            map.put("week_day",week_day);
            map.put("first_day_week",first_day_week);
            map.put("first_day_month",first_day_month);
    
            return map;
        }
    
        @Test
        public void test() throws Exception {
            System.out.println(strTimeTomap("2019-04-18 12:31:05"));
        }
  • 相关阅读:
    ionic入门之AngularJS扩展基本布局
    ionic入门之AngularJS扩展(一)
    test
    面试题小整理
    使用Code first 进行更新数据库结构(数据迁移)
    SQL模糊查询与删除多条语句复习
    GridView 根据要求显示指定值
    个人工作记录---工作中遇到的sql查询语句解析
    数据库,inner join,left join right join 的区别
    利用set实现去重
  • 原文地址:https://www.cnblogs.com/jiuyang/p/10735902.html
Copyright © 2011-2022 走看看