一、演示date类
import java.util.Date; /** * 演示date类 * @author:csjin * 创建日期:2019-06-23-16:32 */ public class Test01 { public static void main(String[] args){ //1、无参构造,返回当前时间 Date date = new Date(); System.out.println(date); //2、通过构造方法指定一个从1970年-1-1 00:00:00经过的毫秒数 long millis = System.currentTimeMillis();//返回当前日期从1970年-1-1 00:00:00经过的毫秒数 System.out.println(millis); Date date1 =new Date(millis); System.out.println(date1); date1 = new Date(millis+1000*62); System.out.println(date1); System.out.println(date.after(date1));//false System.out.println(date.getTime());//回当前日期从1970年-1-1 00:00:00经过的毫秒数 } }
二、日期转换
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * @author:csjin * 创建日期:2019-06-23-16:44 */ public class Test02 { public static void main(String[] args) throws ParseException { Date date = new Date(); System.out.println(date); //2、把date转化为指定格式的字符串 SimpleDateFormat ftime = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss SSS"); System.out.println(ftime.format(date));//2019年06月23日 16:48:28 959 //3、把日期字符串转换为Date对象 String text = "2088-08-08 10:11:12 233"; ftime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); date=ftime.parse(text); //把文本按指定的格式转化为字符串 System.out.println(date); } }
三、Calendar类
import java.util.Calendar; import java.util.Date; import static java.util.Calendar.DAY_OF_MONTH; /** * @author:csjin * 创建日期:2019-06-23-16:56 */ public class Test03 { public static void main(String[] args){ //1、获取当前日历 Calendar cal = Calendar.getInstance(); System.out.println(cal); //[time=1561280380276,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=29,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=5,WEEK_OF_YEAR=26,WEEK_OF_MONTH=5,DAY_OF_MONTH=23,DAY_OF_YEAR=174,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=59,SECOND=40,MILLISECOND=276,ZONE_OFFSET=28800000,DST_OFFSET=0] //2、获得各个日历字段值 System.out.println(cal.get(Calendar.YEAR)); //Calendar.MONTH字段值是从0开始的 System.out.println(cal.get(Calendar.MONTH));//5 System.out.println(cal.get(DAY_OF_MONTH));//23 System.out.println(cal.get(Calendar.HOUR_OF_DAY)); System.out.println(cal.get(Calendar.MINUTE)); System.out.println(cal.get(Calendar.SECOND)); //3、修改日历各个字段,使用set()设置字段值时,可能会影响到相邻字段的值 cal.set(DAY_OF_MONTH,40); System.out.println(cal.get(DAY_OF_MONTH));//10 System.out.println(cal.get(Calendar.MONTH));//6 //4、 Date date = cal.getTime(); System.out.println(date);//Wed Jul 10 17:13:41 CST 2019 long millis = cal.getTimeInMillis(); System.out.println(millis);//1562750021076 } }
四、LocalDate、LocalTime、LocalDateTime介绍
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; /** * @author:csjin * 创建日期:2019-06-23-17:18 */ public class Test04 { public static void main(String[] args){ LocalDate localDate = LocalDate.now(); System.out.println(localDate);//2019-06-23 LocalTime localTime = LocalTime.now(); System.out.println(localTime);//17:22:13.990 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime);//2019-06-23T17:22:13.990 //对日期格式化 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //LocalDateTime有一个实例方法format对日期格式化 System.out.println(localDateTime.format(dateTimeFormatter));//2019-06-23 17:38:56 //把字符串转换为日期 String text = "2088-06-06 10:11:22"; //LocalDateTime有一个parse静态方法可以把字符串转换为日期 LocalDateTime date = LocalDateTime.parse(text, dateTimeFormatter); System.out.println(date);//2088-06-06T10:11:22 } }