Java 有关日期和时间的类

1.JDK8之前的日期时间类

1.1 java.lang.System

 public static native long currentTimeMillis();
 返回当前时间与197011000秒之间的时间差(以毫秒为单位)1.可以用来表示当前时间与1970-1-1 00:00:00的时间差
 2.可以用来计算任意两个时刻的时间差

public static native long nanoTime();
只能用于计算时间差 (以纳秒为单位)
//源码注释如下:The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). 
//翻译如下:返回值是一个从确定的值算起的,但是这个值是任意的 (可能是一个未来的时间,所以返回值可能是负数)

1.2 java.util.Date

  • 构造器
1. Date() 创建一个对应当前时间的Date对象
2. Date(long date) 创建指定毫秒数的Date对象
  • 两个常用方法
1.toString()  Date类重写了toString()方法用以显示当前的年、月、日、时、分、秒
2.getTime() 获取当前Date对象与1970-1-1 00:00:00的时间差(以毫秒为单位)
  • 代码示例
public void test()
{
    
    
	Date date1=new Date();
	System.out.println(date1.toString());//Mon Feb 01 14:20:08 GMT+08:00 2021
	System.out.println(date1.getTime());//1612160408939
	System.out.println(System.currentTimeMillis());//1612160408939
		
	Date date2=new Date(123456789L);
	System.out.println(date2.toString());//Fri Jan 02 18:17:36 GMT+08:00 1970
	System.out.println(date2.getTime());//test1(atguigu.TimeTest)

}

1.3 java.text.SimpleDateFormat

  • SimpleDateFormat可以对日期Date类的格式化和解析
  • 创建SimpleDateFormat类对象
1.SimpleDateFormat()  使用默认的模式和语言环境创建对象
2.SimpleDateFormat(String pattern) 用参数pattern指定的格式创建一个对象
  • 格式化:日期→字符串
public String format(Date date) 格式化时间对象date
  • 解析:字符串→日期
public Date parse(String source)
  • 代码示例
public void test2() throws ParseException
{
    
    
   //使用默认的模式和语言环境创建对象
   SimpleDateFormat sdf = new SimpleDateFormat();
   
   //格式化: 日期 --->字符串
   Date date = new Date();
   System.out.println(date);//Mon Feb 01 14:52:48 GMT+08:00 2021
   String format = sdf.format(date);
   System.out.println(format); //2021/2/1 下午2:52

   //解析: 字符串 ---> 日期
   String str = "2020/12/31 上午11:43";
   Date date1 = sdf.parse(str);
   System.out.println(date1);//Thu Dec 31 11:43:00 GMT+08:00 2020
}
public void test3() throws ParseException
{
    
    
    //用参数pattern指定的格式创建一个对象
	SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	//格式化:日期--字符串
	Date date=new Date();
	System.out.println(date.toString());//Mon Feb 01 15:20:45 GMT+08:00 2021
	String formate=dateFormat.format(date);
	System.out.println(formate);//2021-02-01 15:20:45
		
	//解析:字符串-->日期
	String str="2020-12-31 23:59:59";//一定要保证str的格式和创建对象的格式相同
	Date date1=dateFormat.parse(str);
	System.out.println(date1);	//Thu Dec 31 23:59:59 GMT+08:00 2020
	}

1.4 java.util.Calendar

  • 创建 Calendar 对象
因为Calendar是抽象类,所以不能直接造对象,可以用如下的方法创建对象
1.使用Calendar.getInstance()方法
2.调用Calendar的子类GregorianCalendar的构造器
  • 常用方法
    注意:获取月份时:一月是0,二月是1,以此类推。
               获取星期时:周日是1,周一是2 … 周六是7
//相关filed属性请查看JDK源码,此处仅列举几个 均为静态常量
WEEK_OF_YEAR、WEEK_OF_MONTH、DAY_OF_MONTH、
DAY_OF_YEAR、 DAY_OF_WEEK、DAY_OF_WEEK_IN_MONTH 

1.public int get(int field)

Calendar calendar=Calendar.getInstance();
int value=calendar.get(calendar.DAY_OF_WEEK);
System.out.println(value); //2


2.public void set(int field,int value)//无返回值,直接将原值更改 (可变性)

calendar.set(calendar.DAY_OF_MONTH,4);
int value=calendar.get(calendar.DAY_OF_MONTH);
System.out.println(value);//4


3.public void add(int field,int amount) //无返回值,直接将原值更改 (可变性)

calendar.add(calendar.DAY_OF_WEEK, 2);
int value=calendar.get(calendar.DAY_OF_WEEK);
System.out.println(value);// 4


4.public final Date getTime()

Date date=calendar.getTime();
System.err.println(date);//Mon Feb 01 16:09:42 GMT+08:00 2021


5.public final void setTime(Date date)
Date date=new Date(987654321L);
calendar.setTime(date);
System.out.println(date); //Mon Jan 12 18:20:54 GMT+08:00 1970
int value=calendar.get(calendar.MONTH);
System.out.println(value);//0

2. JDK8中的新日期时间类

2.1 为何JDK8需要引入新日期时间类

  • 可变性:像日期和时间这样的类应该是不可变的 (例如:Calendar类set ()方法会永久改变日期)
  • 偏移性:月份从0开始。
  • 格式化:格式化只对Date有用,Calendar则不行。
  • 安全性:不是线程安全的

2.2 LocalDate、LocalTime、LocalDateTime

  • 创建对象
//  因为三者类似,以下均用LocalDateTime 举例
1.public static LocalDateTime now() 根据当前时间创建对象
2.public static LocalDateTime of(LocalDate date, LocalTime time) 根据指定日期/时间创建对象

LocalDateTime localDateTime=LocalDateTime.now();
System.out.println(localDateTime);//2021-02-01T18:37:51.58
LocalDateTime localDateTime1=LocalDateTime.of(2020,12,31,23,59,59);
System.out.println(localDateTime1);//2020-12-31T23:59:59
  • 获取函数 get
public int getDayOfMonth() 获得月份天数
public int getDayOfYear() 获得年份天数
public DayOfWeek getDayOfWeek() 获得星期几
public Month getMonth() 获得月份
public int getMonthValue() 获得月份
public int getYear() 获得年份
public int getHour() 获得当前对象对应的小时
public int getMinute() 获得当前对象对应的分钟
public int getSecond() 获得当前对象对应的秒
  • 设置(修改)函数 with
修改(设置)后返回新对象,不修改原对象
public LocalDateTime withDayOfMonth()
public LocalDateTime withDayOfYear()
public LocalDateTime withMonth()
public LocalDateTime withYear() 
  • 增加函数 plus
增加后返回新对象,不修改原对象
public LocalDateTime plusDays()
public LocalDateTime plusWeeks()
public LocalDateTime plusMonths(),
public LocalDateTime plusYears()
public LocalDateTime plusHours()
  • 相减函数 minus
相减后返回新对象,不修改原对象
public LocalDateTime minusMonths() 
public LocalDateTime minusWeeks()
public LocalDateTime minusDays()
public LocalDateTime minusYears()
public LocalDateTime minusHours() 

2.3 java.time.Instant

  • 创建对象
1. public static Instant now() 获取本初子午线对应的标准时间
   public OffsetDateTime atOffset(ZoneOffset offset) 根据时区再设置对应的偏移量

//举例 中国在东8区 所以offset应该是 +8
Instant instant=Instant.now();
System.out.println(instant); //2021-02-01T11:15:54.753115Z
OffsetDateTime chinaDateTime=instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(chinaDateTime);//2021-02-01T19:15:54.753115+08:00

2. public static Instant ofEpochMilli(long epochMilli) //返回在1970-01-01 00:00:00基础上加上指定毫秒数之后的Instant类的对象
  • 其他方法
// 返回自1970-01-01 00:00:00到当前时间的毫秒数
p**加粗样式**ublic long toEpochMilli() 

2.4 DateTimeFormatter

  • 相关函数
与SimpleDateFormat类似,用于格式化LocalDateTime、LocalDate、LocalTime

1.public static DateTimeFormatter ofPattern(String pattern) //指定字符串格式
2.public String format(TemporalAccessor temporal) //格式化日期/时间
3.public TemporalAccessor parse(CharSequence text) //指定格式的字符序列解析为日期/时间
  • 代码举例
DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		
LocalDateTime localDateTime=LocalDateTime.now();
String time=dtf.format(localDateTime);
System.out.println(time);//2021-02-01 19:40:59
	
String NewTimeStr="2020-12-31 23:59:59";
TemporalAccessor NewTime=dtf.parse(NewTimeStr);
System.out.println(NewTime);//{},ISO resolved to 2020-12-31T23:59:59

猜你喜欢

转载自blog.csdn.net/weixin_43956248/article/details/113485911