啦啦啦

package lime.java1_8.time;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date; /**
* @Author liangmy
* @Date 2018/7/16
*/
public class Localdatetime {
public static void main(String[] args) {
// 取当前日期:
LocalDate today = LocalDate.now();
System.out.println(today);
// 根据年月日取日期,12月就是12:
LocalDate crischristmas = LocalDate.of(2018, 07, 16);
System.out.println(crischristmas);
// 根据字符串取:
LocalDate endOfFeb = LocalDate.parse("2018-07-16");
System.out.println(endOfFeb);
// 取本月第1天:
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(firstDayOfThisMonth);
// 取本月第2天:
LocalDate secondDayOfThisMonth = today.withDayOfMonth(2);
System.out.println(secondDayOfThisMonth);
// 取本月最后一天,再也不用计算是28,29,30还是31:
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(lastDayOfThisMonth);
// 取下一天:
LocalDate firstDayOf2018 = lastDayOfThisMonth.plusDays(1);
System.out.println(firstDayOf2018);
// 取2018年1月第一个周一,这个计算用Calendar要死掉很多脑细胞:
LocalDate firstMondayOf2018 = today.withMonth(1).with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println(firstMondayOf2018); //LocalTime包含毫秒:
LocalTime now = LocalTime.now();
System.out.println(now);
//清除毫秒数:
LocalTime nowWithNano0 = LocalTime.now().withNano(0);
System.out.println(nowWithNano0);
//构造时间:
LocalTime zeroHM = LocalTime.of(0,0);
System.out.println(zeroHM);
LocalTime zeroHMS = LocalTime.of(0, 0, 0);
System.out.println(zeroHMS);
LocalTime zeroHMSN = LocalTime.of(0, 0, 0, 0);
System.out.println(zeroHMSN);
LocalTime midHMStr = LocalTime.parse("12:00");
System.out.println(midHMStr);
LocalTime midHMSStr = LocalTime.parse("12:00:00");
System.out.println(midHMSStr); //创建一个LocatDateTime实例
LocalDateTime localDateTimeNow = LocalDateTime.now();
System.out.println(localDateTimeNow);
LocalDateTime localDateTimeOf = LocalDateTime.of(2018,1, 1, 00, 00, 00, 12);
System.out.println(localDateTimeOf); //Instant : 时间戳
//默认使用 UTC 时区
Instant insNow = Instant.now();
System.out.println(insNow); OffsetDateTime insUTC8 = insNow.atOffset(ZoneOffset.ofHours(8));
System.out.println(insUTC8); System.out.println(insNow.getNano()); // 获取 UTC时区1970年1月1日午夜时分 之后 5 秒的 时间戳
Instant ofEpochSecond = Instant.ofEpochSecond(5);
System.out.println(ofEpochSecond); //Duration:用于计算两个“时间”间隔
//Period:用于计算两个“日期”间隔 LocalTime startTime = LocalTime.now();
LocalTime endTime = startTime.plusHours(1).plusMinutes(2).plusSeconds(3).plusNanos(4);
Duration duration = Duration.between(startTime, endTime);
System.out.println(duration);
System.out.println(duration.getUnits());
System.out.println(duration.getSeconds());
System.out.println(duration.getNano()); LocalDate startDay = LocalDate.now();
LocalDate endDay = startDay.plusYears(1).plusMonths(2).plusDays(3).plusWeeks(4);
Period period = Period.between(startDay, endDay);
System.out.println(period);
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
System.out.println(period.getUnits());
System.out.println(period.getChronology()); //TemporalAdjuster :时间校正器 LocalDate with = today.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println(with); //DateTimeFormatter
//预定义的标准格式
//语言环境相关的格式
//自定义的格式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss E");
String localDateTime = LocalDateTime.now().format(dateTimeFormatter);
System.out.println(localDateTime); //时区的处理
//ZonedDate、 ZonedTime、 ZonedDateTime
//其中每个时区都对应着 ID,地区ID都为 “{区域}/{城市}”的格式 Asia/Shanghai
//ZoneId:该类中包含了所有的时区信息
//getAvailableZoneIds() : 可以获取所有时区时区信息
//of(id) : 用指定的时区信息获取 ZoneId 对象
LocalDateTime zoneAsiaShanghai = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
System.out.println(zoneAsiaShanghai);
LocalDateTime zoneSystemDefault = LocalDateTime.now(ZoneId.systemDefault());
System.out.println(zoneSystemDefault); //from(Instant instant)
//toInstant() // Long - Instant
Instant instant = Instant.ofEpochMilli(System.currentTimeMillis());
System.out.println(instant); //Instant - Long
Long timestamp = Instant.now().toEpochMilli();
System.out.println(timestamp); // Date - LocalDateTime
LocalDateTime date2LocalDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
System.out.println(date2LocalDateTime); // Date - LocalDate
LocalDate date2LocalDate = date2LocalDateTime.toLocalDate();
System.out.println(date2LocalDate); // Date - LocalTime
LocalTime date2LocalTime = date2LocalDateTime.toLocalTime();
System.out.println(date2LocalTime); // LocalDateTime - Date
Date localDateTime2Date = Date.from(date2LocalDateTime.toInstant(ZoneOffset.ofHours(8)));
System.out.println(localDateTime2Date); // LocalDate - Date
Date localDate2Date = Date.from(date2LocalDate.atStartOfDay().toInstant(ZoneOffset.ofHours(8)));
System.out.println(localDate2Date); // LocalTime - Date
Date localTime2Date = Date.from(LocalDateTime.of(LocalDate.now(), date2LocalTime).toInstant(ZoneOffset.ofHours(8)));
System.out.println(localTime2Date); }
}

啦啦啦

05-04 00:05