1、日期及时间 java.text.SimpleDateFormat java.text.Date java.text.Calendar 时间比较 时间是越往后值越大 时间类 GregorianCalendar 此类继承 Calendar(抽象类)。获得日期时间的方法: 类对象.get(Calendar.YEAR)获得年, 类对象.get(Calendar.AM_PM) 获得上午还是下午,0 为 AM,1 为 PM,12 点 均是 0 类对象.get(Calen
2、dar.DAY_OF_WEEK) 获得星期,周日为 1,周六为 7 类对象.get(Calendar.MONTH) 获得月值为 0-11,所以应当再加 1 其它的类推,查看 Calendar 类的 Field Summary 日期时间运用 获得当前时间(毫秒) long nowsencond = System.currentTimeMillis() 获得当前日期 System.out.println(new Date()) 如 Sun Mar 09 09:56:04 CST 2008
3、 获得前一天日期 Date date = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar. DAY_OF_MONTH , -1); date = calendar.getTime(); //SimpleDateFormat format = new SimpleDateFormat(); //format.applyPattern("yyyy-MM-dd"); //String strDate =
4、format.format(date); //System.out.println( strDate); 获得后一天日期 Date date = new Date(); Calendar calendar = Calendar. getInstance(); calendar.setTime(date); calendar.add(Calendar. DAY_OF_MONTH , 1); date = calendar.getTime(); 时间加减 a)先将所两个时间转化为 long 型 Date time = new Da
5、te() SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS") long timeLong = Long.parseLong(dateFormat.format(time )) b)将两个毫秒数时间进行加减,然后把 long 再封装成 Data Date result= new Date(相加或相减后的毫秒数) 例:得到当前时间之后 90 天的毫秒数 long endsencond = System.curr
6、entTimeMillis() + 90L * 24L * 60L * 60L * 1000L 日期转化为字符串 Date date = new Date() //得到当前的日期 Date String strDate = "" 1、SimpleDateFormat 构造式 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS") 2、SimpleDateFormat 引入式 SimpleDateFormat f
7、ormat = new SimpleDateFormat(); format.applyPattern("yyyy-MM-dd HH:mm:ss"); strDate = format.format(date); System.out.println(strDate) 输出格式:2008-03-09 09:56:04 format.applyPattern("yyyy-MM-dd"); strDate = format.format(date); System.out.println(strDate) 输出格式:2008-03-09
8、 format.applyPattern("yyyyMMddHHmmss"); strDate = format.format(date); System.out.println(strDate) 输出格式:20080309095604 format.applyPattern("yyyy 年 MM 月 dd 日"); strDate = format.format(date); System.out.println(strDate) 输出格式:2008 年 03 月 09 日 字符串转化为日期
9、 String startDate = "2008-4-12"; SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd"); String startDate = "2008-4-12 12:32:11"; SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); Date dateStart = df.parse(startDate); Date 与 Calendar 互转 1、 Date 转化为 Calendar






