java8中的LocalDate,LocalTime,LocalDateTime与Date的转换和使用


基本概念

时刻:所有计算机系统内部都用一个整数表示时刻,这个整数是距离格林尼治标准时间1970年1月1日0时0分0秒的毫秒数,可以理解时刻就是绝对时间,它与时区无关,不同时区对同一时刻的解读,即年月日时分秒是不一样的;

时区:同一时刻,世界上各个地区的时间可能是不一样的,具体时间与时区有关,一共有24个时区,英国格林尼治是0时区,北京是东八区,也就是说格林尼治凌晨1点,北京是早上9点;

年历:我们都知道,中国有公历和农历之分,公历和农历都是年历,不同的年历,一年有多少月,每月有多少天,甚至一天有多少小时,这些可能都是不一样的,我们主要讨论公历。

Java 8中表示日期和时间的类有多个,主要的有:

Instant:它代表的是时间戳,表示时刻,不直接对应年月日信息,需要通过时区转换

LocalDateTime: 表示与时区无关的日期和时间信息,不直接对应时刻,需要通过时区转换

LocalDate:表示与时区无关的日期,与LocalDateTime相比,只有日期信息,没有时间信息

LocalTime:表示与时区无关的时间,与LocalDateTime相比,只有时间信息,没有日期信息

ZonedDateTime: 表示特定时区的日期和时间

ZoneId/ZoneOffset:表示时区
---------------------
原文:https://blog.csdn.net/u010002184/article/details/79713573

 

代码:

package com.wangwenjun.java8;


import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;


public class LocalDateUtil {


private final static String FORMAT_PATTERN1="yyyy-MM-dd HH:mm:ss";
private final static String FORMAT_PATTERN2="yyyyMMddHHmmss";
private final static String FORMAT_PATTERN3="yyyy-MM-dd";
private final static String FORMAT_PATTERN4="yyyyMMdd";

public static void main(String[] args) {
Date date1=new Date();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(localDateFormat(LocalDate.now(),FORMAT_PATTERN3));
System.out.println(localDateTimeFormat(LocalDateTime.now(),FORMAT_PATTERN1));
System.out.println(localDateTimeFormat(LocalDateTime.now(),FORMAT_PATTERN2));
System.out.println(localDateTimeToDate(LocalDateTime.now()));
System.out.println(dateFormat(new Date(),FORMAT_PATTERN4));
System.out.println(minusToMillsLocalDateTime(LocalDateTime.now(),LocalDateTime.now().minusSeconds(1)));
System.out.println(minusToMillsDate(date1,new Date()));
System.out.println(localDateParse("2018-06-12",FORMAT_PATTERN3));
System.out.println(localDateTimeParse("2018-06-12 16:04:43",FORMAT_PATTERN1));
Period p=periodDate(date1,new Date());
System.out.println("year:"+p.getYears()+"month:"+p.getMonths()+"day:"+p.getDays());
System.out.println("----------------------------------------------------------------");
Date date2=localDateToDate(LocalDate.now().minusMonths(1).minusDays(2));
Date date3=localDateToDate(LocalDate.now());
Period p2=periodDate(date2,date3);
System.out.println("year:"+p2.getYears()+"month:"+p2.getMonths()+"day:"+p2.getDays());
System.out.println("----------------------------------------------------------------");
Period p1=periodLocalDate(LocalDate.now().minusDays(2),LocalDate.now());
System.out.println("year:"+p1.getYears()+"month:"+p1.getMonths()+"day:"+p1.getDays());
}

/**
* 将localDate 按照一定的格式转换成String
* @param localDate
* @param pattern
* @return
*/
public static String localDateFormat(LocalDate localDate,String pattern){
return localDate.format(DateTimeFormatter.ofPattern(pattern));
}

/**
* 将localDateTime 按照一定的格式转换成String
* @param localDateTime
* @param pattern
* @return
*/
public static String localDateTimeFormat(LocalDateTime localDateTime,String pattern){
return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
}

/**
* 将String 按照pattern 转换成LocalDate
* @param time
* @param pattern
* @return
*/
public static LocalDate localDateParse(String time,String pattern){
return LocalDate.parse(time,DateTimeFormatter.ofPattern(pattern));
}

/**
* 将String 按照pattern 转换成LocalDateTime
* @param time
* @param pattern
* @return
*/
public static LocalDateTime localDateTimeParse(String time,String pattern){
return LocalDateTime.parse(time,DateTimeFormatter.ofPattern(pattern));
}

/**
* 将date转换成String
* @param date
* @param pattern
* @return
*/
public static String dateFormat(Date date,String pattern){
return localDateTimeFormat(dateToLocalDateTime(date),pattern);
}

/**
* 将LocalDate 转换成 Date
* @param localDate
* @return
*/
public static Date localDateToDate(LocalDate localDate){
ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
return Date.from(zdt.toInstant());
}

/**
* 将LocalDateTime 转换成 Date
* @param localDateTime
* @return
*/
public static Date localDateTimeToDate(LocalDateTime localDateTime){
ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
return Date.from(zdt.toInstant());
}

/**
* 将 Date 转换成LocalDate
* atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
* @param date
* @return
*/
public static LocalDate dateToLocalDate(Date date){
Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
return instant.atZone(zoneId).toLocalDate();
}

/**
* 将 Date 转换成LocalDateTime
* atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
* @param date
* @return
*/
public static LocalDateTime dateToLocalDateTime(Date date){
Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
return instant.atZone(zoneId).toLocalDateTime();
}

/**
* 计算两个LocalDateTime 之间的毫秒数
* @param time1
* @param time2
* @return
*/
public static Long minusToMillsLocalDateTime(LocalDateTime time1,LocalDateTime time2){
return Duration.between(time1, time2).toMillis();
}

/**
* 计算两个LocalTime 之间的毫秒数
* @param time1
* @param time2
* @return
*/
public static Long minusToMillsLocalTime(LocalTime time1,LocalTime time2){
return Duration.between(time1, time2).toMillis();
}

/**
* 计算两个LocalDate 之间的毫秒数
* @param time1
* @param time2
* @return
*/
public static Long minusToMillsLocalDate(LocalDate time1,LocalDate time2){
return Duration.between(time1, time2).toMillis();
}
/**
* 计算两个LocalDate 之间的Period
* @param time1
* @param time2
* @return
*/
public static Period periodLocalDate(LocalDate time1,LocalDate time2){
return Period.between(time1,time2);
}
/**
* 计算两个Date 之间的Period
* @param date1
* @param date2
* @return
*/
public static Period periodDate(Date date1,Date date2){
return periodLocalDate(dateToLocalDate(date1),dateToLocalDate(date2));
}
/**
* 计算两个Date之间的 Period
* @param time1
* @param time2
* @return
*/
public static Long minusToMillsDate(Date time1,Date time2){
return minusToMillsLocalDateTime(dateToLocalDateTime(time1),dateToLocalDateTime(time2));
}
}
--------------------- 
原文:https://blog.csdn.net/qq_36305027/article/details/80668211 

 

Instant时间戳类
(一)含义:从1970-01-01 00:00:00到当前时间的毫秒值

(二)常用的方法:

 now():

获取当前的时间,获取的是当前的美国时间,和处于东八区的我们相差八个小时。

Instant ins=Instant.now();
System.out.println(ins);
atOffset():
设置偏移量例如:

OffsetDateTime time=ins.atOffset(ZoneOffset.ofHours(8));
System.out.println(time);
atZone()
获取系统默认时区时间,参数为一个时区的编号,可以通过时区编号类获取出来

还可以通过Zoneld.systemDefault()来获取本地的默认时区ID

ZonedDateTime zoneDateTime=ins.atZone(Zoneld.systemDefault());
System.out.println(zonedDateTime);
get系列的方法
getEpochSecond():获取从1970-01-01  00:00:00到当前时间的秒值

getNano():把获取到的当前时间的描述换算成纳秒


ofEpochSecond()方法:在计算机元年(1970-01-01  00:00:00)的基础上增加秒数

Instant instant=Onstant.ofEpochSecond(5);
System.out.println(instant)
Duration类
用法:用于计算两个时间间隔的类

常用方法:
between():计算两个时间的间隔,默认的单位是秒

例:Duration between =Duration.between(start,end);

toMillis()方法:将秒转换成毫秒

System.out.println(between.toMillis());

Period 类
常用方法:between():计算两个时间之间的间隔

LocalDate s=LocalDate.of(1985,03,05);
LocalDate now=LocalDate,now();
Periodbe=Period.between(s,now);
System.out.println(be.getYears());
System.out.println(be.getMonth());
System.out.println(be.getDays())
TemporalAdjuster: 时间校正器(接口)
一般我们使用该接口的一个实现类TemporalAdjusters中的一些常量来指定日期。

(1)使用TemporalAdjusters自带的常量来设置日期


LocalDate now=LocalDate.now();
System.out.println(now);
LocalDate with =now.with(TemporalAdjusters.lastDayOdYear());
System.out.println(with);
(2)采用TemporalAdjusters中的next方法来指定日期

  LocalDate date = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println(date);

(3)采用自定义的方法来指定日期

 LocalDateTime ldt = LocalDateTime.now();
        LocalDateTime workDay = ldt.with(new TemporalAdjuster() {
public Temporal adjustInto(Temporal temporal) {
               
                LocalDateTime ld = (LocalDateTime) temporal;
                
                DayOfWeek dayOfWeek = ld.getDayOfWeek();
                if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
                    return ld.plusDays(3);
                } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
                    return ld.plusDays(2);
                } else {
                    
                    return ld.plusDays(1);
                }

DateTimerFormatter
解析和格式化日期或时间

常用方法:

ofPattern("yyyy-MM-dd");:获取对象的静态方法


DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime now = LocalDateTime.now();

format():把一个日期对象的默认格式格式化成指定的格式
String format1 = dateFormat.format(now);

System.out.println(format1);

还可以使用日期类中的format 方法传入一个日期格式化类对象。


now1.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
ZoneID世界时区类
常用方法
getAvailableZonelds():获取世界各个地方的时区的集合
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
ZoneId.systemDefault():获取系统默认时区的ID

  ZoneIdzoneId = ZoneId.systemDefault(); //中国为Asia/Shanghai
--------------------- 
作者:鱼刺_64 
来源:CSDN 
原文:https://blog.csdn.net/yuyuyu1111112/article/details/79187541 
版权声明:本文为博主原创文章,转载请附上博文链接!

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM