Calendar 對象的使用實例

1.Calendar demo例子json

Java Calendar 類時間操做,示範代碼。app

public class CalendarDemo {   
    private static SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");   
    public static void main(String[] args) {   
           
        //獲取calendar實例;   
        Calendar calendar = Calendar.getInstance();   
           
        //判斷calendar是否是GregorianCalendar類的實例;   
        if(calendar instanceof GregorianCalendar){   
            System.out.println("屬於GregorianCalendar類的實例!");   
        }   
           
        //從calendar對象中得到date對象,當前時間;   
        Date dates =  calendar.getTime();   
           
        //格式化時間;   
        String date_str= date_format.format(dates);   
        System.out.println(date_str);   
           
        //設置月份05;表明日曆的月份6月,由於月份從0開始。   
        calendar.set(Calendar.MONTH, 05);   
           
        int months = calendar.get(Calendar.MONTH);   
        System.out.println(months);  //輸出05;   
           
        //設置日期爲2011-07-24 09:59:50   
        calendar.set(2011, 06, 24, 9, 59, 50);     
        String getDate = date_format.format(calendar.getTime());   
        System.out.println(getDate);   //輸出2011-07-24 09:59:50;   
           
        //比較日前大小;   
        if(new Date().getTime() > calendar.getTimeInMillis()){   
            System.out.println("當前日期在後!");   
        }else{   
            System.out.println("當前日期在前!");   
        }   
           
        //設置當前時間爲:2011-07-24 11:06:00   
        calendar.setTime(new Date());     
        int year   = calendar.get(Calendar.YEAR);   //獲取年;   
        int month  = calendar.get(Calendar.MONTH);  //獲取月;   
        int date   = calendar.get(Calendar.DATE);   //獲取天;   
        int hour   = calendar.get(Calendar.HOUR);   //獲取小時;   
        int minute = calendar.get(Calendar.MINUTE); //獲取分鐘;   
        int second = calendar.get(Calendar.SECOND); //獲取秒鐘;   
        int hour_of_day = calendar.get(Calendar.HOUR_OF_DAY);    //第幾個小時,   
        int day_of_month  = calendar.get(Calendar.DAY_OF_MONTH); //這天,在一個月內是第幾天.    
        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);    //這天,在一週內,是第幾天.   
        int day_of_year = calendar.get(Calendar.DAY_OF_YEAR);    //這天,在一年內,是第幾天。   
        int week_of_year = calendar.get(Calendar.WEEK_OF_YEAR);  //這周,在一年內是第幾周;   
        int week_of_month = calendar.get(Calendar.WEEK_OF_MONTH);//這周,在這個月是第幾周;以以星爲標準;   
        int zone_offset = calendar.get(Calendar.ZONE_OFFSET);    //獲取時區;   
        int day_of_week_in_month = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);  //某月中第幾周,按這個月1號算,1號起就是第1周,8號起就是第2周。以月份天數爲標準    
        int r = calendar.get(Calendar.AM_PM);   
        if(r==calendar.AM){   
            System.out.println("如今是上午");   
        }   
           
        if(r==calendar.PM){   
            System.out.println("如今是下午");   
        }   
        System.out.println("==================================================");   
        System.out.println(year);   
        System.out.println(month);   
        System.out.println(date);   
        System.out.println(hour);   
        System.out.println(minute);   
        System.out.println(second);   
        System.out.println(hour_of_day);   
        System.out.println(day_of_month);   
        System.out.println(day_of_week);   
        System.out.println(day_of_year);   
        System.out.println(week_of_year);   
        System.out.println(week_of_month);   
        System.out.println(zone_offset);   
        System.out.println(day_of_week_in_month);   
    }   
}

 

 

2.項目中應用less

@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
    @ResponseBody
    public ListWithTotalCount<AuctionsDTO> aucLotQuery(@ModelAttribute("selectedAgency") SysAgencyDto selectedAgency,
        int page, int rows, String order, String sort) {
        Pageable pageable;
        String agencyId = selectedAgency.getId().toString();

        if (sort != null && !sort.isEmpty()) {
            pageable = new PageRequest(page - 1, rows, Direction.fromStringOrNull(order), sort);
        } else {
            pageable = new PageRequest(page - 1, rows);
        }

        Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); Date less = cal.getTime(); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MILLISECOND, 999); Date great = cal.getTime();
        Specification<Auction> spec = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<Predicate>();

            if (agencyId != null && !agencyId.isEmpty() && !"0".equals(agencyId)) {
                Predicate predicate = cb.equal(root.get(Auction_.agencyId), agencyId);
                predicates.add(predicate);
            }

            Predicate published = cb.equal(root.get(Auction_.isPublished), Auction.AUCLOT_ISPUBLISHED_FINISH);
            predicates.add(published);

       //獲取當天的檢索條件 Predicate time
=cb.between(root.get(Auction_.startTime),less,great); predicates.add(time); return cb.and(predicates.toArray(new Predicate[0])); }; Page<Auction> pageresult = auctionRepository.findAll(spec, pageable); List<AuctionsDTO> dtoList = (new AuctionsDTOAssembler()).toDTOList(pageresult.getContent()); return new ListWithTotalCount<AuctionsDTO>(dtoList, (int) pageresult.getTotalElements()); }
相關文章
相關標籤/搜索