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

 

转载于:https://www.cnblogs.com/jiuyang/p/10735902.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值