java时间戳与Date相互转换、日期格式化、给日期加上指定时长、判断两时间点是否为同一天

1、时间戳转Date

注:以下的方法中很多常量和方法我都没有提取出来,正式项目中还是建议大家封装在时间处理类中,规范化操作.

public static void main(String[] args) {
    // 10位的秒级别的时间戳
    long longTime = 1527767665;
    String result = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(longTime * 1000));
    System.out.println("10位数的时间戳(秒)--->Date:" + result);
    Date date = new Date(time * 1000);   //对应的就是时间戳对应的Date
    // 13位的秒级别的时间戳
    double doubleTime = 1515730332000d;
    String strResult = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(doubleTime);
    System.out.println("13位数的时间戳(毫秒)--->Date:" + strResult);
}
输出结果:
10位数的时间戳(秒)--->Date:2018-05-31 19:54:25
13位数的时间戳(毫秒)--->Date:2018-01-12 12:12:12
尤其要注意上面10位的秒级别的时间戳,不能用int来定义longTime变量,否则会得到错误的结果.

2、Date转时间戳

public static void main(String[] args) {
    //获取指定时间的时间戳,除以1000说明得到的是秒级别的时间戳(10位)
    long longTime = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse("2018-06-30 20:00:00", new ParsePosition(0)).getTime() / 1000;
 
    //获取当前时间戳
    long nLongTime = System.currentTimeMillis();
    long nowLongTime = new Date().getTime();
 
    System.out.println("获取指定时间的时间戳:" + longTime);
    System.out.println("获取当前时间戳:" + nLongTime);
    System.out.println("获取当前时间戳:" + nowLongTime);
}
输出结果:
获取指定时间的时间戳:1530360000
获取当前时间戳:1527769494340
获取当前时间戳:1527769494340

3、格式化Date

public static void main(String[] args) {
    //使用common-lang包下面的DateFormatUtils类
    String format = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
    //使用最原始的SimpleDateFormat类
    String strFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    System.out.println("格式化时间1:" + format);
    System.out.println("格式化时间2:" + strFormat);
}
//输出结果:
格式化时间1:2018-05-31 20:26:49
格式化时间2:2018-05-31 20:26:49

DateFormatUtils是commons.lang3.time.DateFormatUtils下的,如果你的项目中没有,maven中引入下:
<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.6</version>
</dependency>

4、给日期加上指定时长

比如,给现在的时间加上12个小时,这个需求也很常见,例如我做过的某个秒杀接口要返回剩余秒杀时间给前端,那么我就直接计算(比如秒杀持续时间为12小时):秒杀(倒计时)截止时间=(当前时间+12H)即可。

public static void main(String[] args) {
    //将指定日期加上固定时间,DateUtils还有其它添加分钟、小时、月份之类的方法api
    //使用到的是commons-lang包下面的DateUitls类
    Date date = DateUtils.addDays(new Date(), 10);
    System.out.println("当前时间为:"+DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
    String format = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
    System.out.println("当前时间加上10天后:" + format);
}
输出结果(参考,以实际时间为准):
当前时间为:2018-05-31 20:31:53
当前时间加上10天后:2018-06-10 20:31:53

DateUtils也是commons.lang3.time包下的,别忘了加maven依赖。

5、得到指定时间节点的日期时间

    public static void main(String[] args) throws ParseException {
        //得到指定日期
        String date = "2018-03-03 15:20:12";
        Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
        System.out.println(parse);
    }

6、判断两个时间点是否为同一天、同一年

    /**
     * 判断是否为同一天:使用commons-lang包下的DateUtils类
     *
     * @param day
     * @param dateDay
     * @return
     */
    public boolean isSameDay(Date day, Date dateDay) {
        return DateUtils.isSameDay(day, dateDay);
    }
 
    /**
     * 判断是否为同一天:使用joda依赖包里的时间类,效率从一定程度上优于DateUtils.isSameDay()方        
     * 法
     *
     * @param day
     * @param dateDay
     * @return
     */
    public static boolean isSameDay(Date day, Date dateDay){
        if(day == null || dateDay == null){
            throw new IllegalArgumentException("date must be not null");
        }
        LocalDate lDate = new LocalDate(new DateTime(day.getTime()));
        LocalDate localDate = new LocalDate(new DateTime(dateDay.getTime()));
        return lDate.equals(localDate);
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值