Date
时间
世界标准时间 格林尼治 GMT
现在原子钟 北京=世界标准时间+8小时
1s=1000ms
计算机时间原点1970.1.1.00:00:00 算C语言
Date 精确到毫秒 java.util
无参构造 现在时间 有参构造计算机原点+参数(毫秒)
setTime 从时间原点开始 设置时间
getTime() 获取当前时间的毫秒值 和System.currentTimeMillis() 一样
SimpleDateFormat 对Dtae对象格式化 和解析 (化为想要的格式 转化后格式化为Date)
格式化
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM--dd HH:mm;ss");
sdf.fomat(date);
解析
String s="2048-01-01";
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM--dd");
Date date=sdf.parse(s);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.yang.API.TimeClass; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class MyDate { public static void main(String[] args) throws ParseException { // 无参构一个计算机现在的时间 Date date= new Date(); Date date1= new Date(); SimpleDateFormat simpleDateFormat= new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); <br> // 格式化Date类自带的时间表示格式 System.out.println(simpleDateFormat.format(date)); String time= "2018王11-15 15:10:55" ; SimpleDateFormat simpleDateFormat1= new SimpleDateFormat( "yyyy王MM-dd HH:mm:ss" ); date1=simpleDateFormat1.parse(time); System.out.println(date1); } } |
LocalDateTime() 获取时间 时间+日期 构造方法私有
静态方法 now() 获取当前时间 静态方法 of()按照指定时间获取一个LocalDateTime对象
int get_______() 年月日时分秒 public DayOfWeek getDayOfWeek()周
转换to______()
格式 化和解析
Jdk8 DateTimeFormatter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.yang.API.TimeClass; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.DateTimeException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; public class MyDateTimeFormatter { public static void main(String[] args) throws ParseException { // 无参构一个计算机现在的时间 LocalDateTime localDateTime=LocalDateTime.now();<br> //DateTimeFoematter构造函数私有 DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern( "yyyy王MM-dd HH:mm:ss" ); // 格式化LocalDateTime类自带的时间表示格式()<br> System.out.println(dateTimeFormatter.format(localDateTime)); String time= "2018王11-15 15:10:55" ; //解析格式 是LocalDateTIme自带的 localDateTime=LocalDateTime.parse(time,dateTimeFormatter); System.out.println(localDateTime); }<br> } |
LocalDateTime增加或者减少时间
LocalDateTime自带格式化和解析方法 format(指定格式 DateTimeFormatter) paser(准备解析字符串,DateTimeFormatter)
plusYears(int ) Plus___s() 增加哪个时间段 参数为负数就是减 minus___s
修改时间With___()
时间间隔 Period .between(LocalDate,LocalDate).get__(); 获得get省略的时间间隔最多精确到天 例如间隔年 ,getYears()
Duration.between(LocalDate,LocalDate).to____();获取秒和毫秒;