package com.orcal.demc01; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Sj { public static void main(String[] args) throws ParseException { // TODO Auto-generated method stub method1(); method2(); method3(); } public static void method1(){ //日期格式化对象 SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); //将日期对象转换成字符串 Date d=new Date(); String date=sdf.format(d); System.out.println(date); } public static void method2() throws ParseException{ //将字符串转换成日期对象 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); String date="1992-02-02"; Date d=sdf.parse(date); System.out.println(d); } public static void method3(){ //日历类 Calendar c=Calendar.getInstance(); System.out.println(c); System.out.println(c.get(Calendar.YEAR)); System.out.println(c.get(Calendar.DAY_OF_WEEK)); System.out.println(c.get(Calendar.MONTH)); System.out.println(c.get(Calendar.DATE)); c.add(Calendar.DATE,3); System.out.println(c.get(Calendar.DATE)); //将日历类转换成日期对象 Date d=c.getTime(); System.out.println(d); //设置指定日期 c.set(Calendar.YEAR, 2099); c.set(Calendar.MONTH, 6); c.set(Calendar.DATE, 6); c.set(2055, 6,7); System.out.println(c.get(Calendar.YEAR));} public static void a(){ //创建今天的日历类对象 long today=System.currentTimeMillis(); //创建我出生的日期 Calendar birth=Calendar.getInstance(); birth.set(2000,11,31); //转换日期类对象 Date b=birth.getTime(); //获取出生那天的毫秒值 long bir=b.getTime(); //我活了多少毫秒值 long cha=(today-bir)/1000/60/60/24; System.out.println(cha); } }