java的Calendar和Date类

来源:互联网 发布:淘宝网中年女夏装 编辑:程序博客网 时间:2024/06/05 14:32

JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理。下面通过程序简单介绍下:

<span style="white-space:pre"></span>public class DateTest {public static void main(String[] args) {//用new Date的方式输出现在时间String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());      System.out.println(str);      //用Calendar类的方法输出现在的时间    Calendar  calendar=Calendar.getInstance();    String s=(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(calendar.getTime());    System.out.println(s);    //取得Calendar中的年,用同样的方法可以取得月日和具体的时分秒    int year=calendar.get(Calendar.YEAR);    System.out.println("[1]the year is:"+ year);    //Calendar类提供了一组方法用来取得今天是这年,这个月,这周的第几天    int TheDayOfMonth=calendar.get(Calendar.DAY_OF_MONTH);    System.out.println("[2]the day of month is:"+TheDayOfMonth);    //现在是这个月的的几周    int TheWeekOfMonth=calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);    System.out.println("[3]the week of month is:"+TheWeekOfMonth);    //将时间改为3天后的这个时候,同样可以改年份,月份,和具体的时分秒,如果是改到现在时间之前的某个时间,则用负数    calendar.add(Calendar.DATE, 3);    int ThreeDayAfter=calendar.get(Calendar.DATE);    System.out.println("[4]3 days later is:"+ThreeDayAfter);        //Calendar类提供了一组方法(getTime和setTime)用于和Date类的转换    Date d = new Date();            Calendar c6 = Calendar.getInstance();            //Calendar类型的对象转换为Date对象            Date d1 = c6.getTime();            //Date类型的对象转换为Calendar对象            Calendar c7 = Calendar.getInstance();            c7.setTime(d);                        //Calender类提供一组方法用于和相对时间的转化            Calendar c8 = Calendar.getInstance();            long t = 1252785271098L;            //将Calendar对象转换为相对时间            long t1 = c8.getTimeInMillis();            //将相对时间转换为Calendar对象            Calendar c9 = Calendar.getInstance();            c9.setTimeInMillis(t1);                        //Calendar提供了一组方法用于两个时间的比较,before和after            Calendar c4 = Calendar.getInstance();            c4.set(2009, 10 - 1, 10);            Calendar c5 = Calendar.getInstance();            c5.set(2010, 10 - 1, 10);            boolean b = c5.after(c4);            System.out.println("[11]c5在c4后面?:"+b);        //Date类的很多方法从jdk1.1开始都不推荐使用    Date date=new Date();    int DateOfYear=date.getYear();    System.out.println("[5]the date year is:"+(DateOfYear+1900));    int day=date.getDate();    System.out.println("[6]the date day is:"+day);    int DateMonth=date.getMonth();    System.out.println("[7]the date month is:"+(DateMonth+1));    int DateHour=date.getHours();    System.out.println("[8]the date hour is:"+DateHour);    int DateMinute=date.getMinutes();    System.out.println("[9]the date minute is:"+DateMinute);    int DateSecond=date.getSeconds();    System.out.println("[10]the date hour is:"+DateSecond);}    }

项目中的应用

项目要求是:要在日志中显示这周一到现在的登录次数。程序实现是:

        Date endTime=new Date();        Calendar calendar = Calendar.getInstance();        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);        // 减去dayOfWeek,得到第一天的日期,因为Calendar用0-6代表一周七天,所以要减一        calendar.add(Calendar.DAY_OF_WEEK, -(dayOfWeek - 1));        calendar.set(calendar.HOUR,12);        calendar.set(calendar.MINUTE,00);        calendar.set(calendar.SECOND,00);        Date startTime = calendar.getTime();
然后从数据库中查出来,统计下。。。。


项目中要求用现在的时间减去用户的出生日期得到用户的年龄
 /**     *年龄是通过计算得到的取现在的日期减去出生日期     * @return     */    public int getAge() {        if(this.getBirthdate()==null){            return -1;        }        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");        Calendar  calendar=Calendar.getInstance();        int nowYear=calendar.get(Calendar.YEAR);        String dateToString=dateFormat.format(birthdate);        int stringToInt=Integer.parseInt(dateToString);        return nowYear-stringToInt;}

测试的方法:
 @Test    public void testGetAge() throws Exception {        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Date date=simpleDateFormat.parse("2010-01-01 00:00:00");        User user=new User();        user.setBirthdate(date);        System.out.println(user.getAge());    }



0 0
原创粉丝点击