zoukankan      html  css  js  c++  java
  • Java_Date_02_截断日期到日

    oracle 的 trunc 函数能很方便的将日期截断。现在有个需求,需要用java实现与 oracle 的 trunc 函数 相同的功能。

    1.需求:将日期截断到日

              即 将格式为 2018-01-04 03:06:49  日期转换为 格式为  2018-01-04  的日期

    2.实现方法:

     用 DateFormat .format 方法获取只包含年月日的日期字符串,然后再通过 DateFormat .parse方法将日期字符串转为日期

            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            
            //1.获取开始日期,当前日期          2018-01-04 03:06:49
            String startDateStr=df.format(new Date());
            Date startDate=df.parse(startDateStr);
    System.out.println(
    "开始日期:"+new Date()); System.out.println("startDate:"+startDate);

    3.需求拓展:获取日期相隔天数

    方法:

    按照之前的思路,我们将两个日期截断到日,然后相减即可得到 日期相隔天数

    代码实现:

            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            
            //1.获取开始日期,当前日期          2018-01-04 03:06:49
            String startDateStr=df.format(new Date());
            Date startDate=df.parse(startDateStr);
            System.out.println("开始日期:"+new Date());
            System.out.println("startDate:"+startDate);
            
            //2.获取结束日期
            String endDateStr="2018-01-06 03:06:49";
            Date endDate=df.parse(endDateStr);
            System.out.println("结束日期:"+endDate);
            
            //3.求间隔天数
            long spanDays=(endDate.getTime()-startDate.getTime() )/ (1000*3600*24);
            
            System.out.println("间隔天数:"+spanDays);
  • 相关阅读:
    一些链接
    svn
    the source attachment does not contain the source for the file StrutsPrepareAndExecuteFilter.class
    hibernate学习
    hibernate
    mysql front查看所有数据库
    SSD: Single Shot MultiBox Detector 论文解读,附代码
    YOLO算法-论文笔记
    Deep Learning菜鸡篇,我的第一个深度神经网络
    小白成长之路:初识python(六) --python线程池
  • 原文地址:https://www.cnblogs.com/shirui/p/8193673.html
Copyright © 2011-2022 走看看