java.time新API 处理日期选择

近期遇到一个需求,做数据查询的时候要求,根据当前时间的今天,昨天,本周,上周,上月,今年做查询。

开始用java的Calendar类实现,的确很繁琐。

public void test(Integer select){
        Date satrtTime = null;

        Calendar calendar = Calendar.getInstance();
        Date endTime = calendar.getTime();

        calendar.set(Calendar.HOUR_OF_DAY,0 );
        calendar.set(Calendar.MINUTE,0 );
        calendar.set(Calendar.SECOND,0 );
        calendar.set(Calendar.MILLISECOND, 0);
        Date tmp = calendar.getTime();


        switch (select){
            case 1:{
                satrtTime = tmp;
            }
            break;
            case 2:{
                calendar.setTime(tmp);
                calendar.add(Calendar.DAY_OF_MONTH,-1);
                satrtTime = calendar.getTime();
            }
            break;
            case 3:{
                calendar.setTime(tmp);
                calendar.add(Calendar.DAY_OF_MONTH,-2);
                satrtTime = calendar.getTime();
            }
            break;
            case 4:{
                calendar.setTime(tmp);
                int week = calendar.get(Calendar.DAY_OF_WEEK);
                calendar.add(Calendar.DAY_OF_MONTH,-week+2);
                satrtTime = calendar.getTime();
            }
            break;
            case 5:{
                //本月
                calendar.setTime(tmp);
                calendar.set(Calendar.DAY_OF_MONTH,1);
                satrtTime = calendar.getTime();
            }
            break;
            case 6:{
                //上月
                calendar.setTime(tmp);
                calendar.add(Calendar.MONTH,-1);
                calendar.set(Calendar.DAY_OF_MONTH,1);
                satrtTime = calendar.getTime();
            }
            break;
            case 7:{
                //本年
                calendar.setTime(tmp);
                calendar.set(Calendar.MONTH,1);
                calendar.set(Calendar.DAY_OF_MONTH,1);
                satrtTime = calendar.getTime();
            }
            break;
        }
        System.out.println("start: "+ satrtTime +",end: " + endTime);

    }
查了下java8新特性,有很好用的替代API,下面是实现

public void time(Integer selec){

        LocalDate endTime = LocalDate.now();
        LocalDateTime dateTime = null;

        switch (select){
            case 1:{
                dateTime = endTime.atStartOfDay();
            }
            break;
            case 2:{
                dateTime = endTime.minusDays(1).atStartOfDay();
            }
            break;
            case 3:{
                dateTime = endTime.minusDays(2).atStartOfDay();
            }
            break;
            case 4:{
                int week = endTime.getDayOfWeek().getValue();
                dateTime = endTime.minusDays(week-1).atStartOfDay();
            }
            break;
            case 5:{
                //本月
                dateTime = endTime.withDayOfMonth(1).atStartOfDay();
            }
            break;
            case 6:{
                //上月
                dateTime = endTime.minusMonths(1).withDayOfMonth(1).atStartOfDay();
            }
            break;
            case 7:{
                //本年
                dateTime = endTime.withMonth(1).withDayOfMonth(1).atStartOfDay();
            }
            break;
        }
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = dateTime.atZone(zone).toInstant();
        java.util.Date startTime = Date.from(instant);
        System.out.println(new Date());

        System.out.println(startTime);
    }

JDBC

最新JDBC映射将把数据库的日期类型和Java 8的新类型关联起来:

SQL -> Java
--------------------------
date -> LocalDate
time -> LocalTime
timestamp -> LocalDateTime


Mybatis3.4.0以上使用新API,直接加入如下依赖

 
    org.mybatis  
    mybatis-typehandlers-jsr310  
    1.0.1  
 


你可能感兴趣的:(Java知识点)