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"));
        }
  • 相关阅读:
    SQL Server(00):JSON 函数
    Oracle(00):SQL Developer官方工具 初探
    SQL Server(00):搜索特定的对象
    三星平板SM-T320刷机
    SQL Server(00):聚合函数
    SQL Server(00):日期时间函数
    SQL Server(00):字符串函数
    SQL Server(00):文本和图像函数
    SQL Server(00):数学函数
    SQL Server(00):元数据函数
  • 原文地址:https://www.cnblogs.com/jiuyang/p/10735902.html
Copyright © 2011-2022 走看看