Java常用日期类 时区和时钟 格式化日期 currentTimeMillis函数说明

2016-08-02 10:06:00
admin
原创 1883
摘要:Java常用日期类 时区和时钟 格式化日期 currentTimeMillis函数说明

一、Java常用日期类

1、java.sql.Date继承java.util.Date;

2、Date精度毫秒,无法修改毫秒,不建议使用Date修改时间

3、Calendar精度毫秒,getTime获取的时间对象是new出来的;

4、Calendar默认宽松模式,设置的日期字段可以溢出,调用get函数会触发日期计算;

5、java.time.Instant高精度日期时间,使用seconds和nanos存储数据,只读且线程安全;

6、Java.time.LocalDate高精度日期,可以表示很大年份,底层使用年月日,只读且线程安全;

7、java.time.LocalTime高精度时间,精度到纳秒,底层使用时分秒纳秒只读且线程安全;

8、java.time.Duration高精度时间片段,使用seconds和nanos存储数据,只读且线程安全;


时间相关基础:

1、java.time.temporal.Temporal,时间相关接口;

2、java.time.temporal.TemporalAmount,时间量接口;

3、java.time.temporal.ChronoField,时间字段枚举定义;

4、java.time.temporal.ValueRange,时间字段取值范围;

5、java.time.temporal.ChronoUnit,时间单位枚举定义;


时间相关函数:

1、Temporal:long until(Temporal endExclusive, TemporalUnit unit),根据指定单位计算两个时间之间的间隔,会丢失精度;

2、Duration:Duration between(Temporal startInclusive, Temporal endExclusive),计算两个时间之间的间隔,不会丢失精度;


二、时区和时钟

时区相关:

1、java.time.ZoneId,时区抽象类;

2、ZoneId:String getId(),获取时区ID;

3、ZoneId:boolean equals(Object obj),实际比较时区ID;

4、java.time.ZoneOffset,时区实现类,底层使用totalSeconds;

5、ZoneOffset:ZoneOffset ofTotalSeconds(int totalSeconds),生成时区对象;

6、ZoneOffset:boolean equals(Object obj),实际比较totalSeconds;


系统时钟:

1、java.time.Clock,时钟抽象类;

2、Clock:ZoneId getZone(),获取时钟使用的时区;

3、Clock:Clock systemUTC(),获取系统时钟,使用UTC时区;

4、Clock:Clock systemDefaultZone(),获取系统时钟,使用默认时区;

5、Clock:Clock system(ZoneId zone),获取系统时钟,使用指定时区;

6、SystemClock extends Clock:long millis(),获取系统当前时间,时区无关;

7、SystemClock extends Clock:Instant instant(),获取系统当前时间,时区无关;


三、SimpleDateFormat格式化日期

1、java.text.Format子类DateFormat、MessageFormat、NumberFormat;

2、DateFormat子类SimpleDateFormat,SDF多线程不安全,可以使用线程局部变量保证安全;

3、SimpleDateFormat默认宽松模式,解析的日期字段可以溢出,返回的时间对象经过重新计算;


日期格式化:

public class DateFormatTest {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date now = new Date();
String text = df.format(now);
System.out.println(text);
}
}

字符串是否是日期:

public static boolean isDate(String dateStr) {
try {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
Date date = df.parse(dateStr);
String realDataStr = df.format(date);
if (!dateStr.equals(realDataStr))
return false;
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}


四、currentTimeMillis函数

1、时间换算:1秒=1000毫秒,1毫秒=1000微秒,1微秒=1000毫微秒,毫微秒=纳秒;

2、1970-01-01T00:00:00Z,T表示分隔符,Z表示UTC世界标准时间;

3、2038年有符号整数表达的时间将会溢出;

4、currentTimeMillis返回系统当前时间,使用毫秒表示当前时间;

5、nanoTime返回虚拟机的计时器的当前纳秒值,只能用于计时,与日期时间无关;


public static void testTimeMillis() {
long beginTime = System.currentTimeMillis();
long beginNano = System.nanoTime();
long endTime = System.currentTimeMillis();
long endNano = System.nanoTime();
System.out.println(beginTime);
System.out.println(endTime);
System.out.println("diff1 is " + (endTime - beginTime));
System.out.println(beginNano);
System.out.println(endNano);
System.out.println("diff2 is " + (endNano - beginNano));
}


输出:

1476882336328
1476882336328
diff1 is 0
1152734306175
1152734307293
diff2 is 1118

发表评论
评论通过审核之后才会显示。