JAVA学习笔记_GregorianCalendar_Calendar_date.getYear()

按照月份进行查询

        DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");   
        Calendar calendar = new GregorianCalendar(date.getYear()+1900, date.getMonth(), 1,0,0,0);       
        Date a=calendar.getTime();      
        calendar.add(Calendar.MONTH, 1);        
        Date b=calendar.getTime();          
        //system.out.println(new SimpleDateFormat("yyyy-MM-dd").format(a));
        //system.out.println(new SimpleDateFormat("yyyy-MM-dd").format(b));
        String sql="select a from  t_table a where a.xxx=?1 and  "
        + " a.tDate between '"+df.format(a)+"' and '"+df.format(b)+"'" ;        

java.util.Date的getYear() 计算当前年份,需再加上1900

/**
     * Returns a value that is the result of subtracting 1900 from the 
     * year that contains or begins with the instant in time represented 
     * by this Date object, as interpreted in the local 
     * time zone.
     *
     * @return  the year represented by this date, minus 1900.
     * @see     java.util.Calendar
     * @deprecated As of JDK version 1.1,
     * replaced by Calendar.get(Calendar.YEAR) - 1900.
     */
    @Deprecated
    public int getYear() {
        return normalize().getYear() - 1900;
    }

Calendar和GregorianCalendar日历类

Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。
GregorianCalendar 是 Calendar 的一个具体子类,提供了世界上大多数国家/地区使用的标准日历系统。
注意月份的表示,一月是0,二月是1,以此类推,12月是11。因为大多数人习惯于使用单词而不是使用数字来表示月份,这样程序也许更易读,父类Calendar使用常量来表示月份:JANUARY, FEBRUARY,等等。


注意: 
----月份:一月是0,二月是1,以此类推,12月是11 
----星期:周日是1,周一是2,。。。。。周六是7 

Calendar http://blog.csdn.net/myjlvzlp/article/details/8065775

package util;  

import java.text.SimpleDateFormat;  
import java.util.Calendar;  
import java.util.Date;  

/**  
 *  
 * @author yjmao  
 * @deprecated Calendar的常用方法和常用属性小结  
 * @version V1.0.0  
 */  
public class LearnCalendar {  

    public static void main(String[] args){  
        //常用方法  
        param();  
        //常用属性  
        method();  
    }  
    //常用方法  
    public static void method(){  
        Date date = new Date();  
        Calendar c = Calendar.getInstance();  
        //setTime():使用给定的Date设置此 Calendar 的时间  
        c.setTime(date);  
        //获取Calendar对象  
        Calendar cm = Calendar.getInstance();  
        //getTime():获取当前时间,类似于new Date();  
        Date d = cm.getTime();  
        System.err.println("Calendar获得时间:" + d);  
        System.err.println("new Date创建的时间:" + date);  
        //getTimeInMillis():返回此 Calendar 的时间值,以毫秒为单位。   
        long dl = c.getTimeInMillis();  
        long ddate = cm.getTimeInMillis();  
        System.err.println("毫秒数:" + dl);  
        System.err.println("毫秒数:" + ddate);  

        //setTimeInMillis():用给定的 long 值设置此Calendar的当前时间值。   
        long sv = 123456;  
        Calendar sc = Calendar.getInstance();  
        sc.setTimeInMillis(sv);  
        SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String st = ss.format(sc.getTime());  
        System.err.println(st);  

        //get():返回给定日历字段的值。  
        int year = c.get(Calendar.YEAR);  
        System.err.println(year);  

        //set():将给定的日历字段设置为给定值  
        c.set(Calendar.YEAR, 2);  
        int y = c.get(Calendar.YEAR);  
        System.err.println(y);//输出2  

        //Calendar比较:before(),after(),equals(),compareTo().  
        try{  
            String startTime = "2012-12-12 12:45:39";  
            String endTime = "2012-12-12 12:45:40";  
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
            Date startDate = sdf.parse(startTime);  
            Date endDate = sdf.parse(endTime);  
            Calendar start = Calendar.getInstance();  
            Calendar end = Calendar.getInstance();  
            start.setTime(startDate);  
            end.setTime(endDate);  
            if(start.before(end)){  
                System.err.println("开始时间小于结束时间");  
            }else if(start.after(end)){  
                System.err.println("开始时间大于结束时间");  
            }else if(start.equals(end)){  
                System.err.println("开始时间等于结束时间");  
            }  
            /*  
             * start < end 返回-1  
             * start = end 返回0  
             * start > end 返回1  
             */  
            int count = start.compareTo(end);  
            System.err.println(count);  

            //add():为给定的日历字段添加或减去指定的时间量  
            start.add(Calendar.YEAR, -3);  
            System.err.println("原来的时间:" + startTime);  
            System.err.println("add后的时间:" + sdf.format(start.getTime()));  

            //toString():转换为字符串  
            System.err.println(start.toString());  
        }catch(Exception e){}  
    }  
    //常用属性  
    public static void param(){  
        Date date = new Date();  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        //Calendar.YEAR:日期中的年  
        int year = c.get(Calendar.YEAR);  
        //Calendar.MONTH:日期中的月,需要加1  
        int mounth = c.get(Calendar.MONTH) + 1;  
        //Calendar.DATE:日期中的日  
        int day = c.get(Calendar.DATE);  
        //Calendar.HOUR:日期中的小时(12小时制)  
        int hour = c.get(Calendar.HOUR);  
        //Calendar.HOUR_OF_DAY:24小时制  
        int HOUR_OF_DAY = c.get(Calendar.HOUR_OF_DAY);  
        //Calendar.MINUTE:日期中的分钟  
        int minute = c.get(Calendar.MINUTE);  
        //Calendar.SECOND:日期中的秒  
        int second = c.get(Calendar.SECOND);  
        System.err.println(year + "-" + mounth + "-" + day + " " + hour + ":" + minute + ":" + second);  

        //Calendar.WEEK_OF_YEAR:当前年中星期数  
        int WEEK_OF_YEAR = c.get(Calendar.WEEK_OF_YEAR);  
        //Calendar.WEEK_OF_MONTH:当前月中星期数  
        int WEEK_OF_MONTH = c.get(Calendar.WEEK_OF_MONTH);  
        //Calendar.DAY_OF_YEAR:当前年中的第几天  
        int DAY_OF_YEAR = c.get(Calendar.DAY_OF_YEAR);  
        //Calendar.DAY_OF_MONTH:当前月中的第几天  
        int DAY_OF_MONTH = c.get(Calendar.DAY_OF_MONTH);  
        //Calendar.DAY_OF_WEEK:当前星期的第几天(星期天表示第一天,星期六表示第七天)  
        int  DAY_OF_WEEK = c.get(Calendar.DAY_OF_WEEK);  
        //Calendar.DAY_OF_WEEK_IN_MONTH:当前月中的第几个星期  
        int DAY_OF_WEEK_IN_MONTH = c.get(Calendar.DAY_OF_WEEK_IN_MONTH);  

        try{  
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
            Date ampm = format.parse("2012-12-15 21:59:59");  
            Calendar cc = Calendar.getInstance();  
            cc.setTime(ampm);  
            //AM_PM:HOUR 是在中午之前还是在中午之后,在中午12点之前返回0,在中午12点(包括12点)之后返回1  
            int AM_PM = cc.get(Calendar.AM_PM);  
        }catch(Exception e){}  

    }  

GregorianCalendar http://blog.csdn.net/qq_21122243/article/details/73294199

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class TestCalendar {
    public static void main(String[] args) {
        Calendar calendar = new GregorianCalendar(2015, 11, 9, 11, 9, 50);
        Calendar calendar2 = new GregorianCalendar();
        // 设定日期
        calendar2.set(Calendar.YEAR, 2015);
        calendar2.set(Calendar.MONTH, 11);
        calendar2.set(Calendar.DATE, 9);
        calendar2.set(Calendar.HOUR_OF_DAY, 11);
        calendar2.set(calendar.MINUTE, 45);
        calendar2.set(calendar.SECOND, 34);
        // 得到相关的日期元素。
        int year = calendar2.get(Calendar.YEAR);
        int month = calendar2.get(Calendar.MONTH);
        int Date = calendar2.get(Calendar.DATE);
        // 日:Calendar.Date和Calendar.DAY_OF_MONTH同义。
        int hour = calendar2.get(Calendar.HOUR);
        int Minute = calendar2.get(Calendar.MINUTE);
        int ss = calendar2.get(Calendar.SECOND);
        int week = calendar2.get(Calendar.DAY_OF_WEEK);
        // 星期几 这里:1-7.周日是1,周一是2,,,,,周六是7
        System.out.printf("%d年%d月%d日,星期%d\n", year, month, Date, week);
        // 日期计算
        GregorianCalendar calendar3 = new GregorianCalendar(2015, 11, 9, 14, 55, 50);
        ;
        calendar3.add(Calendar.MONTH, -7);// 月份减7
        calendar3.add(Calendar.DATE, 7);// 增加7天
        printCalendar(calendar3);
        // 日历对象和时间对象转化。
        Date d = calendar3.getTime();
        long millSecond = calendar3.getTimeInMillis();
        Date date2 = new Date();
        GregorianCalendar calendar4 = new GregorianCalendar();
        calendar4.setTime(date2);

long g = System.currentTimeMillis();
        printCalendar(calendar4);
    }

public static void printCalendar(Calendar calendar) {
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int day2 = calendar.get(Calendar.DATE);
        // 日:Calendar.Date和Calendar.DAY_OF_MONTH
        int date = calendar.get(Calendar.DAY_OF_WEEK) - 1;// 星期几
        String week = "" + ((date == 0) ? "日" : date);
        System.out.printf("%d年%d月%d日,星期%s\n", year, month, day, week);
    }
}

你可能感兴趣的:(JAVA学习)