使用方法

LocalDate LocalTime LocalDateTime使用的方式是一样的

package java8.Local;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneOffset;

import org.junit.Test;

/**
* LocalDate LocalTime LocalDateTime使用的方式是一样的
*
* @author 陈霓清
*/
public class TestLocalDateTime {

@Test
public void test1() {
// 获取当前时间
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);

// 指定年月日时分秒
LocalDateTime ldt2 = LocalDateTime.of(2019, 3, 12, 15, 8, 33);
System.out.println(ldt2);

// 加2年
LocalDateTime ldt3 = ldt.plusYears(2);
System.out.println(ldt3);

// 减2个月
LocalDateTime ldt4 = ldt.minusMonths(2);
System.out.println(ldt4);

// 获取年月日时分秒
System.out.println(ldt.getYear());
System.out.println(ldt.getMonthValue());
System.out.println(ldt.getDayOfMonth());
System.out.println(ldt.getHour());
System.out.println(ldt.getMinute());
System.out.println(ldt.getSecond());
}

/**
* Instant:时间戳(以Unix元年:1970年1月1日 00:00:00到某个时间之间的毫秒值)
*/
@Test
public void test2() {
Instant ins = Instant.now(); //默认获取的是UTC时区
System.out.println(ins);
System.out.println(ins.toEpochMilli()); // 毫秒数

// 加60毫秒
Instant ins2 = Instant.ofEpochMilli(60);
System.out.println(ins2);

OffsetDateTime odt = ins.atOffset(ZoneOffset.ofHours(8));
System.out.println(odt);
}

/**
* Duration:计算两个"时间"之间的间隔
* Period:计算两个"日期"之间的间隔
*/
@Test
public void test3() {
Instant ins3 = Instant.now();
Instant ins4 = Instant.now();
Duration duration = Duration.between(ins3, ins4);
System.out.println(duration.toMillis());

LocalTime lt1 = LocalTime.now();
LocalTime lt2 = LocalTime.now();
System.out.println(Duration.between(lt1, lt2).toMillis());

LocalDate ld1 = LocalDate.now();
LocalDate ld2 = LocalDate.of(2019, 3, 12);
System.out.println(Period.between(ld1, ld2));
System.out.println(Period.between(ld1, ld2).getYears());
System.out.println(Period.between(ld1, ld2).getDays());
System.out.println(Period.between(ld1, ld2).getMonths());
}

}


Java8时间校正器

/**
* Java8时间校正器:TemporalAdjuster
*/
@Test
public void test4() {
// 获取当前系统时间
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);

// 指定成10日
LocalDateTime ldt2 = ldt.withDayOfMonth(10);
System.out.println(ldt2);

// 指定成下一个周日
LocalDateTime ldt3 = ldt.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(ldt3);

// 自定义时间校正器:下一个工作日的日期
LocalDateTime ldt5 = ldt.with((temp) -> {
LocalDateTime ldt4 = (LocalDateTime)temp;
DayOfWeek dow = ldt4.getDayOfWeek();

if (dow.equals(DayOfWeek.FRIDAY)) {
return ldt4.plusDays(3);
}

if (dow.equals(DayOfWeek.SATURDAY)) {
return ldt4.plusDays(2);
}

return ldt4.plusDays(1);
});
System.out.println(ldt5);
}



Java8时间日期格式化

/**
* Java8时间日期格式化:DateTimeFormatter
*/
@Test
public void test6() {
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
LocalDateTime ldt = LocalDateTime.now();

String strDate = ldt.format(dtf);
System.out.println(strDate);

DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String strDate2 = dtf2.format(ldt);
System.out.println(strDate2);

LocalDateTime newDate = ldt.parse(strDate2, dtf2);
System.out.println(newDate);

// 对时区的操作ZonedDate/ZonedTime/ZonedDateTime
Set<String> set = ZoneId.getAvailableZoneIds();
set.forEach(System.out::println);

LocalDateTime ldt3 = LocalDateTime.now(ZoneId.of("Europe/Tallinn"));
System.out.println(ldt3);

LocalDateTime ldt4 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
ZonedDateTime zdt = ldt4.atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(zdt);
}


回到顶部