java常用类-------Calendar类和CregorianCalendar类使用

package cn.zxg.PackgeUse;

import java.time.Year;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

/** * 测试日期类使用 */ public class TestCanlendar { public static void main(String[] args) { Calendar calendar=new GregorianCalendar(2019,10,9,22,10,50); //获取上面对象的年 int year=calendar.get(Calendar.YEAR); //获取上面对象的月 int month=calendar.get(Calendar.MONTH); //星期几,1表示星期日,7表示星期一 int weekday=calendar.get(Calendar.DAY_OF_WEEK); //获得几号,或者使用DAY_OF_MONTH int date=calendar.get(Calendar.DATE); System.out.println(year+"年"+month+"月"); System.out.println(weekday); System.out.println(date); //设置日期的相关元素 //获取今天的日期 Calendar c2=new GregorianCalendar(); c2.set(Calendar.YEAR,9012); System.out.println(c2); //日期的计算,往后100是几号 Calendar c3=new GregorianCalendar(); c3.add(Calendar.DATE,100); System.out.println(c3); //日期对象和时间对象的转化 // Date d4=c3.getTime(); Calendar c4=new GregorianCalendar(); c4.setTime(new Date()); printCalender(c4); } public static void printCalender(Calendar c){ int year=c.get(Calendar.YEAR); int month=c.get(Calendar.MONTH)+1; int day=c.get(Calendar.DATE); int hour=c.get(Calendar.HOUR); int minute=c.get(Calendar.MINUTE); int second=c.get(Calendar.SECOND); System.out.println(year+"年"+month+"月"+day+"日"+" "+hour +"时"+minute+"分"+second+"秒"); } }

posted on 2019-05-22 21:34  zz测试笔记  阅读(334)  评论(0编辑  收藏  举报